Replace automatic YAML decoding with opt-in YAML decoding

Base codecs no longer automically handle YAML.  Instead, clients
must convert to JSON first via yaml.ToJSON and runtime.YAMLDecoder.
This commit is contained in:
Clayton Coleman
2015-03-16 23:43:59 -04:00
parent 71abc99dbe
commit 6918a4d32e
7 changed files with 49 additions and 40 deletions

View File

@@ -17,10 +17,9 @@ limitations under the License.
package conversion
import (
"encoding/json"
"fmt"
"reflect"
"github.com/ghodss/yaml"
)
// MetaFactory is used to store and retrieve the version and kind
@@ -33,13 +32,13 @@ type MetaFactory interface {
Interpret(data []byte) (version, kind string, err error)
}
// DefaultMetaFactory is a default factory for versioning objects in JSON/YAML. The object
// DefaultMetaFactory is a default factory for versioning objects in JSON. The object
// in memory and in the default JSON serialization will use the "kind" and "apiVersion"
// fields.
var DefaultMetaFactory = SimpleMetaFactory{KindField: "Kind", VersionField: "APIVersion"}
// SimpleMetaFactory provides default methods for retrieving the type and version of objects
// that are identified with an "apiVersion" and "kind" fields in their JSON/YAML
// that are identified with an "apiVersion" and "kind" fields in their JSON
// serialization. It may be parameterized with the names of the fields in memory, or an
// optional list of base structs to search for those fields in memory.
type SimpleMetaFactory struct {
@@ -51,16 +50,14 @@ type SimpleMetaFactory struct {
BaseFields []string
}
// Interpret will return the APIVersion and Kind of the JSON/YAML wire-format
// Interpret will return the APIVersion and Kind of the JSON wire-format
// encoding of an object, or an error.
func (SimpleMetaFactory) Interpret(data []byte) (version, kind string, err error) {
findKind := struct {
APIVersion string `json:"apiVersion,omitempty"`
Kind string `json:"kind,omitempty"`
}{}
// yaml is a superset of json, so we use it to decode here. That way,
// we understand both.
err = yaml.Unmarshal(data, &findKind)
err = json.Unmarshal(data, &findKind)
if err != nil {
return "", "", fmt.Errorf("couldn't get version/kind: %v", err)
}