mirror of
https://github.com/goccy/go-yaml.git
synced 2025-12-08 06:09:57 +00:00
fix format with empty value (#654)
This commit is contained in:
parent
548e1b8de3
commit
2ac8cffa27
2 changed files with 38 additions and 8 deletions
|
|
@ -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) {
|
func TestIssue650(t *testing.T) {
|
||||||
type Disk struct {
|
type Disk struct {
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
|
|
|
||||||
|
|
@ -284,6 +284,9 @@ func (f *Formatter) formatFile(file *ast.File) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Formatter) origin(tk *token.Token) string {
|
func (f *Formatter) origin(tk *token.Token) string {
|
||||||
|
if tk == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
if f.existsComment {
|
if f.existsComment {
|
||||||
return tk.Origin
|
return tk.Origin
|
||||||
}
|
}
|
||||||
|
|
@ -349,10 +352,6 @@ func (f *Formatter) formatDirective(n *ast.DirectiveNode) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Formatter) formatMapping(n *ast.MappingNode) string {
|
func (f *Formatter) formatMapping(n *ast.MappingNode) string {
|
||||||
if len(n.Values) == 0 {
|
|
||||||
return "{}"
|
|
||||||
}
|
|
||||||
|
|
||||||
var ret string
|
var ret string
|
||||||
if n.IsFlowStyle {
|
if n.IsFlowStyle {
|
||||||
ret = f.origin(n.Start)
|
ret = f.origin(n.Start)
|
||||||
|
|
@ -379,10 +378,6 @@ func (f *Formatter) formatMappingKey(n *ast.MappingKeyNode) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Formatter) formatSequence(n *ast.SequenceNode) string {
|
func (f *Formatter) formatSequence(n *ast.SequenceNode) string {
|
||||||
if len(n.Values) == 0 {
|
|
||||||
return "[]"
|
|
||||||
}
|
|
||||||
|
|
||||||
var ret string
|
var ret string
|
||||||
if n.IsFlowStyle {
|
if n.IsFlowStyle {
|
||||||
ret = f.origin(n.Start)
|
ret = f.origin(n.Start)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue