parent
e2b6ccad8a
commit
425142d221
|
@ -6,6 +6,7 @@ fmt: generate
|
||||||
go fmt ./...
|
go fmt ./...
|
||||||
|
|
||||||
test: generate
|
test: generate
|
||||||
|
go get -t ./...
|
||||||
go test $(TEST) $(TESTARGS)
|
go test $(TEST) $(TESTARGS)
|
||||||
|
|
||||||
generate:
|
generate:
|
||||||
|
|
|
@ -12,5 +12,8 @@ install:
|
||||||
go version
|
go version
|
||||||
|
|
||||||
go env
|
go env
|
||||||
|
|
||||||
|
go get -t ./...
|
||||||
|
|
||||||
build_script:
|
build_script:
|
||||||
- cmd: go test -v ./...
|
- cmd: go test -v ./...
|
||||||
|
|
|
@ -409,7 +409,6 @@ func (d *decoder) decodeSlice(name string, node ast.Node, result reflect.Value)
|
||||||
if result.Kind() == reflect.Interface {
|
if result.Kind() == reflect.Interface {
|
||||||
result = result.Elem()
|
result = result.Elem()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the slice if it isn't nil
|
// Create the slice if it isn't nil
|
||||||
resultType := result.Type()
|
resultType := result.Type()
|
||||||
resultElemType := resultType.Elem()
|
resultElemType := resultType.Elem()
|
||||||
|
@ -443,6 +442,12 @@ func (d *decoder) decodeSlice(name string, node ast.Node, result reflect.Value)
|
||||||
|
|
||||||
// Decode
|
// Decode
|
||||||
val := reflect.Indirect(reflect.New(resultElemType))
|
val := reflect.Indirect(reflect.New(resultElemType))
|
||||||
|
|
||||||
|
// if item is an object that was decoded from ambiguous JSON and
|
||||||
|
// flattened, make sure it's expanded if it needs to decode into a
|
||||||
|
// defined structure.
|
||||||
|
item := expandObject(item, val)
|
||||||
|
|
||||||
if err := d.decode(fieldName, item, val); err != nil {
|
if err := d.decode(fieldName, item, val); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -455,6 +460,57 @@ func (d *decoder) decodeSlice(name string, node ast.Node, result reflect.Value)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// expandObject detects if an ambiguous JSON object was flattened to a List which
|
||||||
|
// should be decoded into a struct, and expands the ast to properly deocode.
|
||||||
|
func expandObject(node ast.Node, result reflect.Value) ast.Node {
|
||||||
|
item, ok := node.(*ast.ObjectItem)
|
||||||
|
if !ok {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
|
||||||
|
elemType := result.Type()
|
||||||
|
|
||||||
|
// our target type must be a struct
|
||||||
|
switch elemType.Kind() {
|
||||||
|
case reflect.Ptr:
|
||||||
|
switch elemType.Elem().Kind() {
|
||||||
|
case reflect.Struct:
|
||||||
|
//OK
|
||||||
|
default:
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
case reflect.Struct:
|
||||||
|
//OK
|
||||||
|
default:
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
|
||||||
|
// A list value will have a key and field name. If it had more fields,
|
||||||
|
// it wouldn't have been flattened.
|
||||||
|
if len(item.Keys) != 2 {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
|
||||||
|
keyToken := item.Keys[0].Token
|
||||||
|
item.Keys = item.Keys[1:]
|
||||||
|
|
||||||
|
// we need to un-flatten the ast enough to decode
|
||||||
|
newNode := &ast.ObjectItem{
|
||||||
|
Keys: []*ast.ObjectKey{
|
||||||
|
&ast.ObjectKey{
|
||||||
|
Token: keyToken,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Val: &ast.ObjectType{
|
||||||
|
List: &ast.ObjectList{
|
||||||
|
Items: []*ast.ObjectItem{item},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return newNode
|
||||||
|
}
|
||||||
|
|
||||||
func (d *decoder) decodeString(name string, node ast.Node, result reflect.Value) error {
|
func (d *decoder) decodeString(name string, node ast.Node, result reflect.Value) error {
|
||||||
switch n := node.(type) {
|
switch n := node.(type) {
|
||||||
case *ast.LiteralType:
|
case *ast.LiteralType:
|
||||||
|
@ -606,6 +662,7 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
|
||||||
// match (only object with the field), then we decode it exactly.
|
// match (only object with the field), then we decode it exactly.
|
||||||
// If it is a prefix match, then we decode the matches.
|
// If it is a prefix match, then we decode the matches.
|
||||||
filter := list.Filter(fieldName)
|
filter := list.Filter(fieldName)
|
||||||
|
|
||||||
prefixMatches := filter.Children()
|
prefixMatches := filter.Children()
|
||||||
matches := filter.Elem()
|
matches := filter.Elem()
|
||||||
if len(matches.Items) == 0 && len(prefixMatches.Items) == 0 {
|
if len(matches.Items) == 0 && len(prefixMatches.Items) == 0 {
|
||||||
|
|
|
@ -1176,10 +1176,10 @@
|
||||||
"revision": "7e3c02b30806fa5779d3bdfc152ce4c6f40e7b38"
|
"revision": "7e3c02b30806fa5779d3bdfc152ce4c6f40e7b38"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "fa9G5tEr4oJJc3vtgn/B0NWZXfA=",
|
"checksumSHA1": "8OPDk+bKyRGJoKcS4QNw9F7dpE8=",
|
||||||
"path": "github.com/hashicorp/hcl",
|
"path": "github.com/hashicorp/hcl",
|
||||||
"revision": "99df0eb941dd8ddbc83d3f3605a34f6a686ac85e",
|
"revision": "ef8133da8cda503718a74741312bf50821e6de79",
|
||||||
"revisionTime": "2016-09-02T16:52:19Z"
|
"revisionTime": "2016-09-16T13:01:00Z"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "67DfevLBglV52Y2eAuhFc/xQni0=",
|
"checksumSHA1": "67DfevLBglV52Y2eAuhFc/xQni0=",
|
||||||
|
|
Loading…
Reference in New Issue