Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Care map node #334

Merged
merged 1 commit into from Dec 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions encode.go
Expand Up @@ -481,8 +481,8 @@ func (e *Encoder) encodeMapItem(ctx context.Context, item MapItem, column int) (
if err != nil {
return nil, errors.Wrapf(err, "failed to encode MapItem")
}
if m, ok := value.(*ast.MappingNode); ok {
m.AddColumn(e.indent)
if e.isMapNode(value) {
value.AddColumn(e.indent)
}
return ast.MappingValue(
token.New("", "", e.pos(column)),
Expand All @@ -503,6 +503,11 @@ func (e *Encoder) encodeMapSlice(ctx context.Context, value MapSlice, column int
return node, nil
}

func (e *Encoder) isMapNode(node ast.Node) bool {
_, ok := node.(ast.MapNode)
return ok
}

func (e *Encoder) encodeMap(ctx context.Context, value reflect.Value, column int) ast.Node {
node := ast.Mapping(token.New("", "", e.pos(column)), e.isFlowStyle)
keys := make([]interface{}, len(value.MapKeys()))
Expand All @@ -519,7 +524,7 @@ func (e *Encoder) encodeMap(ctx context.Context, value reflect.Value, column int
if err != nil {
return nil
}
if value.Type() == ast.MappingType || value.Type() == ast.MappingValueType {
if e.isMapNode(value) {
value.AddColumn(e.indent)
}
node.Values = append(node.Values, ast.MappingValue(
Expand Down Expand Up @@ -642,7 +647,7 @@ func (e *Encoder) encodeStruct(ctx context.Context, value reflect.Value, column
if err != nil {
return nil, errors.Wrapf(err, "failed to encode value")
}
if _, ok := value.(ast.MapNode); ok {
if e.isMapNode(value) {
value.AddColumn(e.indent)
}
var key ast.MapKeyNode = e.encodeString(structField.RenderName, column)
Expand Down