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

Fix format with empty value #654

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
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
35 changes: 35 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3196,6 +3196,41 @@ foo:
}
}

func TestBytesUnmarshalerWithEmptyValue(t *testing.T) {
type T struct{}

unmarshaler := func(dst *T, b []byte) error {
var v any
return yaml.Unmarshal(b, &v)
}

yml := `
map: &m {}
seq: &seq []
foo: # comment
bar: *m
baz: *seq
`
m := yaml.CommentMap{}
var v T
if err := yaml.UnmarshalWithOptions(
[]byte(yml),
&v,
yaml.CommentToMap(m),
yaml.CustomUnmarshaler[T](unmarshaler),
); err != nil {
t.Fatal(err)
}
if err := yaml.UnmarshalWithOptions(
[]byte(yml),
&v,
yaml.CustomUnmarshaler[T](unmarshaler),
); err != nil {
t.Fatal(err)
}

}

func TestIssue650(t *testing.T) {
type Disk struct {
Name string `yaml:"name"`
Expand Down
11 changes: 3 additions & 8 deletions internal/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ func (f *Formatter) formatFile(file *ast.File) string {
}

func (f *Formatter) origin(tk *token.Token) string {
if tk == nil {
return ""
}
if f.existsComment {
return tk.Origin
}
Expand Down Expand Up @@ -349,10 +352,6 @@ func (f *Formatter) formatDirective(n *ast.DirectiveNode) string {
}

func (f *Formatter) formatMapping(n *ast.MappingNode) string {
if len(n.Values) == 0 {
return "{}"
}

var ret string
if n.IsFlowStyle {
ret = f.origin(n.Start)
Expand All @@ -379,10 +378,6 @@ func (f *Formatter) formatMappingKey(n *ast.MappingKeyNode) string {
}

func (f *Formatter) formatSequence(n *ast.SequenceNode) string {
if len(n.Values) == 0 {
return "[]"
}

var ret string
if n.IsFlowStyle {
ret = f.origin(n.Start)
Expand Down