Skip to content

Commit

Permalink
fix tag encode in mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
RangelReale committed Oct 6, 2023
1 parent 0640a15 commit 352ab5d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,8 @@ func (n *MappingValueNode) toString() string {
return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String())
} else if _, ok := n.Value.(*AliasNode); ok {
return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String())
} else if _, ok := n.Value.(*TagNode); ok {
return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String())
}
if keyComment != nil {
return fmt.Sprintf(
Expand Down
36 changes: 36 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1630,3 +1630,39 @@ b:
t.Fatalf("failed to encode. expected %s but got %s", expected, got)
}
}

type tagMarshaler struct{}

func (b *tagMarshaler) MarshalYAML() ([]byte, error) {
v, err := yaml.Marshal("test")
if err != nil {
return nil, err
}
return []byte(fmt.Sprintf("%s %s", "!!timestamp", string(v))), nil
}

func TestBytesMarshalerWithTag(t *testing.T) {
b, err := yaml.Marshal(map[string]interface{}{
"a": map[string]interface{}{
"b": map[string]interface{}{
"c": &tagMarshaler{},
"d": []*tagMarshaler{&tagMarshaler{}, &tagMarshaler{}},
},
},
})
if err != nil {
t.Fatal(err)
}
expected := `
a:
b:
c: !!timestamp test
d:
- !!timestamp test
- !!timestamp test
`
got := "\n" + string(b)
if expected != got {
t.Fatalf("failed to encode. expected %s but got %s", expected, got)
}
}

0 comments on commit 352ab5d

Please sign in to comment.