diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index a3d0e1c..cfb4e6c 100644 --- a/.pre-commit-hooks.yaml +++ b/.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] diff --git a/docs/config-file.md b/docs/config-file.md index e41901d..dcf82ba 100644 --- a/docs/config-file.md +++ b/docs/config-file.md @@ -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` diff --git a/formatters/basic/config.go b/formatters/basic/config.go index 63f682a..9b4e12d 100644 --- a/formatters/basic/config.go +++ b/formatters/basic/config.go @@ -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 { diff --git a/formatters/basic/formatter.go b/formatters/basic/formatter.go index de4d1a0..6d1cde8 100644 --- a/formatters/basic/formatter.go +++ b/formatters/basic/formatter.go @@ -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 } diff --git a/formatters/basic/formatter_test.go b/formatters/basic/formatter_test.go index 2ca383c..d432169 100644 --- a/formatters/basic/formatter_test.go +++ b/formatters/basic/formatter_test.go @@ -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) + } +} diff --git a/go.mod b/go.mod index 509f23f..e325b54 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 985a8ea..223efc2 100644 --- a/go.sum +++ b/go.sum @@ -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=