Skip to content

Commit

Permalink
encoder: add option to drop merge tag (#14)
Browse files Browse the repository at this point in the history
The !!merge tag is superfluous for most people. Need an option to drop
it when it's unnecessary.
  • Loading branch information
braydonk committed Mar 30, 2023
1 parent 82670ba commit 875dd3d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
17 changes: 11 additions & 6 deletions encode.go
Expand Up @@ -29,12 +29,13 @@ import (
)

type encoder struct {
emitter yaml_emitter_t
event yaml_event_t
out []byte
flow bool
indent int
doneInit bool
emitter yaml_emitter_t
event yaml_event_t
out []byte
flow bool
indent int
doneInit bool
optDropMergeTag bool
}

func newEncoder() *encoder {
Expand Down Expand Up @@ -468,6 +469,10 @@ func (e *encoder) node(node *Node, tail string) {
}
}

if tag == mergeTag && e.optDropMergeTag {
tag = ""
}

switch node.Kind {
case DocumentNode:
yaml_document_start_event_initialize(&e.event, nil, nil, true)
Expand Down
11 changes: 11 additions & 0 deletions formattest/encode_test.go
Expand Up @@ -51,3 +51,14 @@ func TestBlockScalar(t *testing.T) {
},
}.Run(t)
}

func TestDropMergeTag(t *testing.T) {
formatTestCase{
name: "drop merge tag",
folder: "drop_merge_tag",
configureDecoder: noopDecoder,
configureEncoder: func(enc *yaml.Encoder) {
enc.SetDropMergeTag(true)
},
}.Run(t)
}
3 changes: 3 additions & 0 deletions formattest/testdata/drop_merge_tag/expected.yaml
@@ -0,0 +1,3 @@
a: &a
b:
<<: *a
3 changes: 3 additions & 0 deletions formattest/testdata/drop_merge_tag/input.yaml
@@ -0,0 +1,3 @@
a: &a
b:
<<: *a
5 changes: 5 additions & 0 deletions yaml.go
Expand Up @@ -318,6 +318,11 @@ func (e *Encoder) SetIndentlessBlockSequence(indentlessBlockSequence bool) {
yaml_emitter_set_indentless_block_sequence(&e.encoder.emitter, indentlessBlockSequence)
}

// SetDropMergeTag sets optDropMergeTag on the encoder.
func (e *Encoder) SetDropMergeTag(dropMergeTag bool) {
e.encoder.optDropMergeTag = dropMergeTag
}

// Close closes the encoder by writing any remaining data.
// It does not write a stream terminating string "...".
func (e *Encoder) Close() (err error) {
Expand Down

0 comments on commit 875dd3d

Please sign in to comment.