From d48fe690c3c071cb5c8e3aa4d4672a32230a5e2d Mon Sep 17 00:00:00 2001 From: mo khan Date: Thu, 24 Jul 2025 17:40:45 -0600 Subject: refactor: extract job to process relationship updates in background --- .../yamlfmt/formatters/basic/anchors/check.go | 37 ------------------- .../google/yamlfmt/formatters/basic/config.go | 35 ++++++++++-------- .../google/yamlfmt/formatters/basic/features.go | 30 ++++++--------- .../yamlfmt/formatters/basic/features/check.go | 37 +++++++++++++++++++ .../formatters/basic/features/force_sequence.go | 43 ++++++++++++++++++++++ .../formatters/basic/features/yaml_feature.go | 31 ++++++++++++++++ .../google/yamlfmt/formatters/basic/formatter.go | 16 +++++++- 7 files changed, 156 insertions(+), 73 deletions(-) delete mode 100644 vendor/github.com/google/yamlfmt/formatters/basic/anchors/check.go create mode 100644 vendor/github.com/google/yamlfmt/formatters/basic/features/check.go create mode 100644 vendor/github.com/google/yamlfmt/formatters/basic/features/force_sequence.go create mode 100644 vendor/github.com/google/yamlfmt/formatters/basic/features/yaml_feature.go (limited to 'vendor/github.com/google/yamlfmt/formatters/basic') diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/anchors/check.go b/vendor/github.com/google/yamlfmt/formatters/basic/anchors/check.go deleted file mode 100644 index aef3070..0000000 --- a/vendor/github.com/google/yamlfmt/formatters/basic/anchors/check.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2022 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package anchors - -import ( - "errors" - "fmt" - - "github.com/braydonk/yaml" -) - -func Check(n yaml.Node) error { - if n.Kind == yaml.AliasNode { - return errors.New("alias node found") - } - if n.Anchor != "" { - return fmt.Errorf("node references anchor %q", n.Anchor) - } - for _, c := range n.Content { - if err := Check(*c); err != nil { - return err - } - } - return nil -} diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/config.go b/vendor/github.com/google/yamlfmt/formatters/basic/config.go index 82e67eb..2a32d05 100644 --- a/vendor/github.com/google/yamlfmt/formatters/basic/config.go +++ b/vendor/github.com/google/yamlfmt/formatters/basic/config.go @@ -18,25 +18,28 @@ import ( "runtime" "github.com/google/yamlfmt" + yamlFeatures "github.com/google/yamlfmt/formatters/basic/features" ) type Config struct { - Indent int `mapstructure:"indent"` - IncludeDocumentStart bool `mapstructure:"include_document_start"` - LineEnding yamlfmt.LineBreakStyle `mapstructure:"line_ending"` - LineLength int `mapstructure:"max_line_length"` - RetainLineBreaks bool `mapstructure:"retain_line_breaks"` - RetainLineBreaksSingle bool `mapstructure:"retain_line_breaks_single"` - DisallowAnchors bool `mapstructure:"disallow_anchors"` - ScanFoldedAsLiteral bool `mapstructure:"scan_folded_as_literal"` - IndentlessArrays bool `mapstructure:"indentless_arrays"` - DropMergeTag bool `mapstructure:"drop_merge_tag"` - PadLineComments int `mapstructure:"pad_line_comments"` - TrimTrailingWhitespace bool `mapstructure:"trim_trailing_whitespace"` - EOFNewline bool `mapstructure:"eof_newline"` - StripDirectives bool `mapstructure:"strip_directives"` - ArrayIndent int `mapstructure:"array_indent"` - IndentRootArray bool `mapstructure:"indent_root_array"` + Indent int `mapstructure:"indent"` + IncludeDocumentStart bool `mapstructure:"include_document_start"` + LineEnding yamlfmt.LineBreakStyle `mapstructure:"line_ending"` + LineLength int `mapstructure:"max_line_length"` + RetainLineBreaks bool `mapstructure:"retain_line_breaks"` + RetainLineBreaksSingle bool `mapstructure:"retain_line_breaks_single"` + DisallowAnchors bool `mapstructure:"disallow_anchors"` + ScanFoldedAsLiteral bool `mapstructure:"scan_folded_as_literal"` + IndentlessArrays bool `mapstructure:"indentless_arrays"` + DropMergeTag bool `mapstructure:"drop_merge_tag"` + PadLineComments int `mapstructure:"pad_line_comments"` + TrimTrailingWhitespace bool `mapstructure:"trim_trailing_whitespace"` + EOFNewline bool `mapstructure:"eof_newline"` + StripDirectives bool `mapstructure:"strip_directives"` + ArrayIndent int `mapstructure:"array_indent"` + IndentRootArray bool `mapstructure:"indent_root_array"` + DisableAliasKeyCorrection bool `mapstructure:"disable_alias_key_correction"` + ForceArrayStyle yamlFeatures.SequenceStyle `mapstructure:"force_array_style"` } func DefaultConfig() *Config { diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/features.go b/vendor/github.com/google/yamlfmt/formatters/basic/features.go index de736f4..fc933f4 100644 --- a/vendor/github.com/google/yamlfmt/formatters/basic/features.go +++ b/vendor/github.com/google/yamlfmt/formatters/basic/features.go @@ -15,9 +15,8 @@ package basic import ( - "github.com/braydonk/yaml" "github.com/google/yamlfmt" - "github.com/google/yamlfmt/formatters/basic/anchors" + yamlFeatures "github.com/google/yamlfmt/formatters/basic/features" "github.com/google/yamlfmt/internal/features" "github.com/google/yamlfmt/internal/hotfix" ) @@ -55,24 +54,19 @@ func ConfigureFeaturesFromConfig(config *Config) yamlfmt.FeatureList { return configuredFeatures } -// These features will directly use the `yaml.Node` type and -// as such are specific to this formatter. -type YAMLFeatureFunc func(yaml.Node) error -type YAMLFeatureList []YAMLFeatureFunc +func ConfigureYAMLFeaturesFromConfig(config *Config) yamlFeatures.YAMLFeatureList { + var featureList yamlFeatures.YAMLFeatureList -func (y YAMLFeatureList) ApplyFeatures(node yaml.Node) error { - for _, f := range y { - if err := f(node); err != nil { - return err - } + if config.DisallowAnchors { + featureList = append(featureList, yamlFeatures.Check) } - return nil -} -func ConfigureYAMLFeaturesFromConfig(config *Config) YAMLFeatureList { - var features YAMLFeatureList - if config.DisallowAnchors { - features = append(features, anchors.Check) + if config.ForceArrayStyle != "" { + featureList = append( + featureList, + yamlFeatures.FeatureForceSequenceStyle(config.ForceArrayStyle), + ) } - return features + + return featureList } diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/features/check.go b/vendor/github.com/google/yamlfmt/formatters/basic/features/check.go new file mode 100644 index 0000000..368de5c --- /dev/null +++ b/vendor/github.com/google/yamlfmt/formatters/basic/features/check.go @@ -0,0 +1,37 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package features + +import ( + "errors" + "fmt" + + "github.com/google/yamlfmt/pkg/yaml" +) + +func Check(n yaml.Node) error { + if n.Kind == yaml.AliasNode { + return errors.New("alias node found") + } + if n.Anchor != "" { + return fmt.Errorf("node references anchor %q", n.Anchor) + } + for _, c := range n.Content { + if err := Check(*c); err != nil { + return err + } + } + return nil +} diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/features/force_sequence.go b/vendor/github.com/google/yamlfmt/formatters/basic/features/force_sequence.go new file mode 100644 index 0000000..f3f956c --- /dev/null +++ b/vendor/github.com/google/yamlfmt/formatters/basic/features/force_sequence.go @@ -0,0 +1,43 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package features + +import "github.com/google/yamlfmt/pkg/yaml" + +type SequenceStyle string + +const ( + SequenceStyleBlock SequenceStyle = "block" + SequenceStyleFlow SequenceStyle = "flow" +) + +func FeatureForceSequenceStyle(style SequenceStyle) YAMLFeatureFunc { + var styleVal yaml.Style + if style == SequenceStyleFlow { + styleVal = yaml.FlowStyle + } + var forceStyle YAMLFeatureFunc + forceStyle = func(n yaml.Node) error { + var err error + for _, c := range n.Content { + if c.Kind == yaml.SequenceNode { + c.Style = styleVal + } + err = forceStyle(*c) + } + return err + } + return forceStyle +} diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/features/yaml_feature.go b/vendor/github.com/google/yamlfmt/formatters/basic/features/yaml_feature.go new file mode 100644 index 0000000..2657a53 --- /dev/null +++ b/vendor/github.com/google/yamlfmt/formatters/basic/features/yaml_feature.go @@ -0,0 +1,31 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package features + +import "github.com/google/yamlfmt/pkg/yaml" + +// These features will directly use the `yaml.Node` type and +// as such are specific to this formatter. +type YAMLFeatureFunc func(yaml.Node) error +type YAMLFeatureList []YAMLFeatureFunc + +func (y YAMLFeatureList) ApplyFeatures(node yaml.Node) error { + for _, f := range y { + if err := f(node); err != nil { + return err + } + } + return nil +} diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/formatter.go b/vendor/github.com/google/yamlfmt/formatters/basic/formatter.go index 26bcc45..1e69bdb 100644 --- a/vendor/github.com/google/yamlfmt/formatters/basic/formatter.go +++ b/vendor/github.com/google/yamlfmt/formatters/basic/formatter.go @@ -20,8 +20,9 @@ import ( "errors" "io" - "github.com/braydonk/yaml" "github.com/google/yamlfmt" + yamlFeature "github.com/google/yamlfmt/formatters/basic/features" + "github.com/google/yamlfmt/pkg/yaml" "github.com/mitchellh/mapstructure" ) @@ -30,7 +31,7 @@ const BasicFormatterType string = "basic" type BasicFormatter struct { Config *Config Features yamlfmt.FeatureList - YAMLFeatures YAMLFeatureList + YAMLFeatures yamlFeature.YAMLFeatureList } // yamlfmt.Formatter interface @@ -63,6 +64,10 @@ func (f *BasicFormatter) Format(input []byte) ([]byte, error) { documents = append(documents, docNode) } + if len(documents) == 0 { + return input, nil + } + // Run all YAML features. for _, d := range documents { if err := f.YAMLFeatures.ApplyFeatures(d); err != nil { @@ -119,6 +124,13 @@ func (f *BasicFormatter) getNewEncoder(buf *bytes.Buffer) *yaml.Encoder { } e.SetIndentRootArray(f.Config.IndentRootArray) + // Yes I know I could SetCorrectAliasKeys(!f.Config.DisableAliasKeyCorrection) + // but I know myself and I know I'll get confused and have to go look up + // the source again next time I look and forget. + if !f.Config.DisableAliasKeyCorrection { + e.SetCorrectAliasKeys(true) + } + return e } -- cgit v1.2.3