From 6f464171347edd8d1e6f9490eed0777646f0a511 Mon Sep 17 00:00:00 2001 From: Christoffer Date: Fri, 17 Feb 2023 10:24:11 +0100 Subject: [PATCH 1/2] Add forceSquash tag --- nodes.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/nodes.go b/nodes.go index 47a3aee..93e4597 100644 --- a/nodes.go +++ b/nodes.go @@ -27,6 +27,11 @@ const ( // the key for the value. SquashTag string = "squash" + // ForceSquashTag is attached to a struct or a map and indicates + // to the encoder to always lift the fields of that value into the + // parent block's scope transparently. + ForceSquashTag string = "forceSquash" + // UnusedKeysTag is a flag that indicates any unused keys found by the // decoder are stored in this field of type []string. This has the same // behavior as the OmitTag and is not encoded. @@ -55,6 +60,7 @@ type fieldMeta struct { name string key bool squash bool + forceSquash bool unusedKeys bool decodedFields bool omit bool @@ -266,7 +272,7 @@ func encodeStruct(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) { } // this field is anonymous and should be squashed into the parent struct's fields - if meta.anonymous && meta.squash { + if (meta.anonymous && meta.squash) || meta.forceSquash { switch val := val.(type) { case *ast.ObjectType: list.Items = append(list.Items, val.List.Items...) @@ -371,6 +377,8 @@ func extractFieldMeta(f reflect.StructField) (meta fieldMeta) { meta.key = true case SquashTag: meta.squash = true + case ForceSquashTag: + meta.forceSquash = true case DecodedFieldsTag: meta.decodedFields = true case UnusedKeysTag: From 9f36fadc8c88e646eaec4b51c75fba00f38c676b Mon Sep 17 00:00:00 2001 From: Christoffer Date: Fri, 17 Feb 2023 10:24:20 +0100 Subject: [PATCH 2/2] Append child keys instead of overwriting support 3+ levels of keys --- nodes.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodes.go b/nodes.go index 93e4597..0d903c5 100644 --- a/nodes.go +++ b/nodes.go @@ -277,7 +277,7 @@ func encodeStruct(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) { case *ast.ObjectType: list.Items = append(list.Items, val.List.Items...) if childKeys != nil { - keys = childKeys + keys = append(keys, childKeys...) } continue }