Skip to content

Commit 8ccc632

Browse files
committed
Add universal pointer dereference in neat YAML
When using a pointer as an argument for `ToYAMLString`, the output was not rendered correctly. The pointer was rendered as a scalar. Add check for pointers and explicit reflect based dereference.
1 parent c928094 commit 8ccc632

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

output_yaml.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ func (p *OutputProcessor) neatYAML(prefix string, skipIndentOnFirstLine bool, ob
6262
case yamlv3.Node:
6363
return p.neatYAMLofNode(prefix, skipIndentOnFirstLine, &t)
6464

65-
case *yamlv3.Node:
66-
return p.neatYAMLofNode(prefix, skipIndentOnFirstLine, t)
67-
6865
default:
6966
switch reflect.TypeOf(obj).Kind() {
67+
case reflect.Ptr:
68+
return p.neatYAML(prefix, skipIndentOnFirstLine, reflect.ValueOf(obj).Elem().Interface())
69+
7070
case reflect.Struct:
7171
return p.neatYAMLOfStruct(prefix, skipIndentOnFirstLine, t)
7272

output_yaml_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,5 +385,42 @@ dependencies:
385385
Expect(err).ToNot(HaveOccurred())
386386
Expect(output).To(Equal(expected))
387387
})
388+
389+
It("should output a pointer to a generic type struct", func() {
390+
ColorSetting = ON
391+
defer func() { ColorSetting = OFF }()
392+
393+
expected, _ := ToYAMLString(yml(`---
394+
name: foobar
395+
version: v1.0.0
396+
dependencies:
397+
- name: foo
398+
version: v0.5.0
399+
active: true
400+
- name: bar
401+
version: v0.5.0
402+
active: true
403+
`))
404+
405+
output, err := ToYAMLString(&Example{
406+
Name: "foobar",
407+
Version: "v1.0.0",
408+
Dependencies: []Dependency{
409+
{
410+
Name: "foo",
411+
Version: "v0.5.0",
412+
Active: true,
413+
},
414+
{
415+
Name: "bar",
416+
Version: "v0.5.0",
417+
Active: true,
418+
},
419+
},
420+
})
421+
422+
Expect(err).ToNot(HaveOccurred())
423+
Expect(output).To(Equal(expected))
424+
})
388425
})
389426
})

0 commit comments

Comments
 (0)