Skip to content
This repository was archived by the owner on Mar 20, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -55,6 +60,7 @@ type fieldMeta struct {
name string
key bool
squash bool
forceSquash bool
unusedKeys bool
decodedFields bool
omit bool
Expand Down Expand Up @@ -266,12 +272,12 @@ 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...)
if childKeys != nil {
keys = childKeys
keys = append(keys, childKeys...)
}
continue
}
Expand Down Expand Up @@ -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:
Expand Down