Skip to content

Commit 45c59e7

Browse files
committed
Add support for YAML anchors
The YAML anchors and aliases were never shown in the YAML output. Add support for YAML anchors for Go YAML v3 based documents.
1 parent dbd70b8 commit 45c59e7

File tree

4 files changed

+53
-31
lines changed

4 files changed

+53
-31
lines changed

go.sum

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
2-
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
32
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
4-
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
53
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
64
github.com/gonvenience/bunt v1.1.1 h1:isYxOpDqbRMOSRhZtoux1tYvhhQ/AIbVDFrs24l6t0M=
75
github.com/gonvenience/bunt v1.1.1/go.mod h1:lsyhkmNpSAzhVx059BD0fQy5F29rWcS6AHb7UWNlT/s=
@@ -22,13 +20,9 @@ github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936 h1:kw1v0NlnN+GZcU8
2220
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936/go.mod h1:r1VsdOzOPt1ZSrGZWFoNhsAedKnEd6r9Np1+5blZCWk=
2321
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
2422
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
25-
github.com/onsi/ginkgo v1.10.3 h1:OoxbjfXVZyod1fmWYhI7SEyaD8B00ynP3T+D5GiyHOY=
26-
github.com/onsi/ginkgo v1.10.3/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
2723
github.com/onsi/ginkgo v1.11.0 h1:JAKSXpt1YjtLA7YpPiqO9ss6sNXEsPfSGdwN0UHqzrw=
2824
github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
2925
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
30-
github.com/onsi/gomega v1.7.1 h1:K0jcRCwNQM3vFGh1ppMtDh/+7ApJrjldlX8fA0jDTLQ=
31-
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
3226
github.com/onsi/gomega v1.8.1 h1:C5Dqfs/LeauYDX0jJXIe2SWmwCbGzx9yF8C8xy3Lh34=
3327
github.com/onsi/gomega v1.8.1/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA=
3428
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
@@ -39,7 +33,6 @@ golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8U
3933
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
4034
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
4135
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
42-
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
4336
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
4437
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
4538
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -50,7 +43,6 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
5043
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
5144
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
5245
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
53-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
5446
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
5547
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
5648
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=

output.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var DefaultColorSchema = map[string]colorful.Color{
4545
"binaryColor": bunt.Aqua,
4646
"emptyStructures": bunt.PaleGoldenrod,
4747
"commentColor": bunt.DimGray,
48+
"anchorColor": bunt.CornflowerBlue,
4849
}
4950

5051
// OutputProcessor provides the functionality to output neat YAML strings using

output_yaml.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,32 +217,32 @@ func (p *OutputProcessor) neatYAMLofNode(prefix string, skipIndentOnFirstLine bo
217217
switch value.Kind {
218218
case yamlv3.MappingNode:
219219
if len(value.Content) == 0 {
220-
fmt.Fprint(p.out, " ", p.colorize("{}", "emptyStructures"), "\n")
220+
fmt.Fprint(p.out, p.createAnchorDefinition(value), " ", p.colorize("{}", "emptyStructures"), "\n")
221221
} else {
222-
fmt.Fprint(p.out, "\n")
222+
fmt.Fprint(p.out, p.createAnchorDefinition(value), "\n")
223223
if err := p.neatYAMLofNode(prefix+p.prefixAdd(), false, value); err != nil {
224224
return err
225225
}
226226
}
227227

228228
case yamlv3.SequenceNode:
229229
if len(value.Content) == 0 {
230-
fmt.Fprint(p.out, " ", p.colorize("[]", "emptyStructures"), "\n")
230+
fmt.Fprint(p.out, p.createAnchorDefinition(value), " ", p.colorize("[]", "emptyStructures"), "\n")
231231
} else {
232-
fmt.Fprint(p.out, "\n")
232+
fmt.Fprint(p.out, p.createAnchorDefinition(value), "\n")
233233
if err := p.neatYAMLofNode(prefix, false, value); err != nil {
234234
return err
235235
}
236236
}
237237

238238
case yamlv3.ScalarNode:
239-
fmt.Fprint(p.out, " ")
239+
fmt.Fprint(p.out, p.createAnchorDefinition(value), " ")
240240
if err := p.neatYAMLofNode(prefix+p.prefixAdd(), false, value); err != nil {
241241
return err
242242
}
243243

244-
default:
245-
return fmt.Errorf("kind %v in mapping context as value is not implemented yet", value.Kind)
244+
case yamlv3.AliasNode:
245+
fmt.Fprintf(p.out, " %s\n", p.colorize("*"+value.Value, "anchorColor"))
246246
}
247247

248248
if len(key.FootComment) > 0 {
@@ -310,3 +310,11 @@ func (p *OutputProcessor) neatYAMLofNode(prefix string, skipIndentOnFirstLine bo
310310

311311
return nil
312312
}
313+
314+
func (p *OutputProcessor) createAnchorDefinition(node *yamlv3.Node) string {
315+
if len(node.Anchor) != 0 {
316+
return fmt.Sprint(" ", p.colorize("&"+node.Anchor, "anchorColor"))
317+
}
318+
319+
return ""
320+
}

output_yaml_test.go

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -165,80 +165,101 @@ empty-scalar: null
165165
Context("create YAML output (go-yaml v3)", func() {
166166
It("should create YAML output based on YAML node structure", func() {
167167
example := []byte(`---
168-
# before document
168+
# start of document
169169
170170
# before map
171171
map: # at map definition
172172
key: value # value
173-
# after map
174173
175-
# before scalar
174+
# before scalars
176175
scalars: # at scalar definition
177176
boolean: true # true
178177
number: 42 # 42
179178
float: 47.11
180179
string: foobar
181180
data: !!binary Zm9vYmFyCg==
182-
# after scaler
183181
184182
# before list
185183
list: # at list definition
186184
- one # one
187185
- two # two
188-
# after list
189186
190187
# before multiline
191188
multiline: |
192189
This is
193190
a multi
194191
line te
195192
xt.
196-
# after multiline
197193
198194
# before zeros
199195
zeros:
200196
map: {}
201197
list: []
202198
scalar: nil
203-
# after zeros
204199
205-
# after document
200+
# before anchors
201+
anchors:
202+
scalar: &scalaranchor 42
203+
same-scalar: *scalaranchor
204+
list: &listanchor
205+
- one
206+
- two
207+
same-list: *listanchor
208+
empty-list: &emptylist []
209+
same-empty-list: *emptylist
210+
map: &mapanchor
211+
key: value
212+
same-map: *mapanchor
213+
empty-map: &emptymap {}
214+
same-empty-map: *emptymap
215+
216+
# end of document
206217
`)
207218

208219
expected := `---
209-
# before document
220+
# start of document
210221
211222
# before map
212223
map:
213224
key: value # value
214-
# after map
215-
# before scalar
225+
# before scalars
216226
scalars:
217227
boolean: true # true
218228
number: 42 # 42
219229
float: 47.11
220230
string: foobar
221231
data: Zm9vYmFyCg==
222-
# after scaler
223232
# before list
224233
list:
225234
- one # one
226235
- two # two
227-
# after list
228236
# before multiline
229237
multiline: |
230238
This is
231239
a multi
232240
line te
233241
xt.
234-
# after multiline
235242
# before zeros
236243
zeros:
237244
map: {}
238245
list: []
239246
scalar: nil
240-
# after zeros
241-
# after document
247+
# before anchors
248+
anchors:
249+
scalar: &scalaranchor 42
250+
same-scalar: *scalaranchor
251+
list: &listanchor
252+
- one
253+
- two
254+
same-list: *listanchor
255+
empty-list: &emptylist []
256+
same-empty-list: *emptylist
257+
map: &mapanchor
258+
key: value
259+
same-map: *mapanchor
260+
empty-map: &emptymap {}
261+
same-empty-map: *emptymap
262+
# end of document
242263
`
243264

244265
var node yamlv3.Node

0 commit comments

Comments
 (0)