Skip to content

Commit

Permalink
formatter: add drop_merge_tag option
Browse files Browse the repository at this point in the history
Add an option to drop the `!!merge` tag from all formatted yaml. Also
add a test for IndentlessArrays that should have already been there and
fixed the pre-commit description which should have been fixed before
too.
  • Loading branch information
braydonk committed Mar 30, 2023
1 parent 7f8bfab commit 357e4bf
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- id: yamlfmt
name: yamlfmt
description: This hook uses github.com/google/yamlfmt to format yaml files. Requires golang >1.18 to be installed.
description: This hook uses github.com/google/yamlfmt to format yaml files. Requires golang >1.20 to be installed.
entry: yamlfmt
language: golang
types: [yaml]
1 change: 1 addition & 0 deletions docs/config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ The basic formatter is a barebones formatter that simply takes the data provided
| `max_line_length` | int | 0 | Set the maximum line length (see notes below). if not set, defaults to 0 which means no limit. |
| `scan_folded_as_literal` | bool | false | Option that will preserve newlines in folded block scalars (blocks that start with `>`). |
| `indentless_arrays` | bool | false | Render `-` array items (block sequence items) without an increased indent. |
| `drop_merge_tag` | bool | false | Assume that any well formed merge using just a `<<` token will be a merge, and drop the `!!merge` tag from the formatted result. |

### Note on `max_line_length`

Expand Down
1 change: 1 addition & 0 deletions formatters/basic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
DisallowAnchors bool `mapstructure:"disallow_anchors"`
ScanFoldedAsLiteral bool `mapstructure:"scan_folded_as_literal"`
IndentlessArrays bool `mapstructure:"indentless_arrays"`
DropMergeTag bool `mapstructure:"drop_merge_tag"`
}

func DefaultConfig() *Config {
Expand Down
1 change: 1 addition & 0 deletions formatters/basic/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (f *BasicFormatter) getNewEncoder(buf *bytes.Buffer) *yaml.Encoder {
e.SetExplicitDocumentStart(f.Config.IncludeDocumentStart)
e.SetAssumeBlockAsLiteral(f.Config.ScanFoldedAsLiteral)
e.SetIndentlessBlockSequence(f.Config.IndentlessArrays)
e.SetDropMergeTag(f.Config.DropMergeTag)

return e
}
38 changes: 38 additions & 0 deletions formatters/basic/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,41 @@ func TestScanFoldedAsLiteral(t *testing.T) {
t.Fatalf("expected string to be %d lines, was %d", lines, resultLines)
}
}

func TestIndentlessArrays(t *testing.T) {
config := basic.DefaultConfig()
config.IndentlessArrays = true
f := newFormatter(config)

yml := `a:
- 1
- 2
`
result, err := f.Format([]byte(yml))
if err != nil {
t.Fatalf("expected formatting to pass, returned error: %v", err)
}
resultStr := string(result)
if resultStr != yml {
t.Fatalf("expected:\n%s\ngot:\n%s", yml, resultStr)
}
}

func TestDropMergeTag(t *testing.T) {
config := basic.DefaultConfig()
config.DropMergeTag = true
f := newFormatter(config)

yml := `a: &a
b:
<<: *a`

result, err := f.Format([]byte(yml))
if err != nil {
t.Fatalf("expected formatting to pass, returned error: %v", err)
}
resultStr := string(result)
if strings.Contains(resultStr, "!!merge") {
t.Fatalf("expected formatted result to drop merge tag, was found:\n%s", resultStr)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/RageCage64/multilinediff v0.2.0
github.com/bmatcuk/doublestar/v4 v4.6.0
github.com/braydonk/yaml v0.5.0
github.com/braydonk/yaml v0.6.0
github.com/mitchellh/mapstructure v1.5.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/braydonk/yaml v0.4.0 h1:MNjdriecuspytC31J7Tzx6O8b1yAbMSTGvRG6vLSXZc=
github.com/braydonk/yaml v0.4.0/go.mod h1:hcm3h581tudlirk8XEUPDBAimBPbmnL0Y45hCRl47N4=
github.com/braydonk/yaml v0.5.0 h1:j1SlSSD9JLRKgkmFb66hsDH4D0YFlLOLXSXDwNH9/LU=
github.com/braydonk/yaml v0.5.0/go.mod h1:hcm3h581tudlirk8XEUPDBAimBPbmnL0Y45hCRl47N4=
github.com/braydonk/yaml v0.6.0 h1:nHt2AzmV4yMhXSNROksvkTgAxzollgy7uVOKfCVXhMo=
github.com/braydonk/yaml v0.6.0/go.mod h1:hcm3h581tudlirk8XEUPDBAimBPbmnL0Y45hCRl47N4=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down

0 comments on commit 357e4bf

Please sign in to comment.