Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formatter: add drop_merge_tag option #103

Merged
merged 1 commit into from Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-hooks.yaml
@@ -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
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
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
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
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
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
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