Use go-yaml when marshalled type has yaml tags

By using go-yaml directly, the properties in the document will match
the order of the fields in the coresponding go type.
This commit is contained in:
Matt Ellis 2017-10-17 11:38:03 -07:00
parent 9994c9c7b9
commit 906f191e45
1 changed files with 30 additions and 0 deletions
pkg/encoding

View File

@ -5,8 +5,10 @@ package encoding
import (
"encoding/json"
"path/filepath"
"reflect"
"github.com/ghodss/yaml"
goyaml "gopkg.in/yaml.v2"
)
var JSONExt = ".json"
@ -87,11 +89,39 @@ func (m *yamlMarshaler) IsYAMLLike() bool {
}
func (m *yamlMarshaler) Marshal(v interface{}) ([]byte, error) {
if hasYamlTags(reflect.TypeOf(v)) {
return goyaml.Marshal(v)
}
return yaml.Marshal(v)
}
func (m *yamlMarshaler) Unmarshal(data []byte, v interface{}) error {
// IDEA: use a "strict" marshaler, so that we can warn on unrecognized keys (avoiding silly mistakes). We should
// set aside an officially sanctioned area in the metadata for extensibility by 3rd parties.
if hasYamlTags(reflect.TypeOf(v)) {
return goyaml.Unmarshal(data, v)
}
return yaml.Unmarshal(data, v)
}
// hasYamlTags checks to see if all fields of a struct have yaml tags (and hence it would be safe) to use go-yaml directly.
func hasYamlTags(t reflect.Type) bool {
if t.Kind() == reflect.Ptr {
return hasYamlTags(t.Elem())
}
if t.Kind() != reflect.Struct {
return false
}
allOk := true
for i := 0; i < t.NumField(); i++ {
_, has := t.Field(i).Tag.Lookup("yaml")
allOk = allOk && has
}
return allOk
}