diff --git a/internal/builder/dynamic_mappings.go b/internal/builder/dynamic_mappings.go index da0026e50..beeb9a978 100644 --- a/internal/builder/dynamic_mappings.go +++ b/internal/builder/dynamic_mappings.go @@ -240,7 +240,7 @@ func formatResult(result interface{}) ([]byte, error) { if err != nil { return nil, errors.New("failed to encode") } - yamlFormatter := &formatter.YAMLFormatter{} + yamlFormatter := formatter.NewYAMLFormatter(formatter.KeysWithDotActionNone) d, _, err = yamlFormatter.Format(d) if err != nil { return nil, errors.New("failed to format") diff --git a/internal/formatter/formatter.go b/internal/formatter/formatter.go index ed16e8658..97367abac 100644 --- a/internal/formatter/formatter.go +++ b/internal/formatter/formatter.go @@ -14,14 +14,27 @@ import ( "github.com/elastic/elastic-package/internal/packages" ) +const ( + KeysWithDotActionNone int = iota + KeysWithDotActionNested +) + +type formatterOptions struct { + extension string + specVersion semver.Version + preferedKeysWithDotAction int + + failFast bool +} + type formatter func(content []byte) ([]byte, bool, error) -func newFormatter(specVersion semver.Version, ext string) formatter { - switch ext { +func newFormatter(options formatterOptions) formatter { + switch options.extension { case ".json": - return JSONFormatterBuilder(specVersion).Format + return JSONFormatterBuilder(options.specVersion).Format case ".yaml", ".yml": - return NewYAMLFormatter(specVersion).Format + return NewYAMLFormatter(options.preferedKeysWithDotAction).Format default: return nil } @@ -37,18 +50,33 @@ func Format(packageRoot string, failFast bool) error { if err != nil { return fmt.Errorf("failed to parse package format version %q: %w", manifest.SpecVersion, err) } + err = filepath.Walk(packageRoot, func(path string, info os.FileInfo, err error) error { if err != nil { return err } + options := formatterOptions{ + specVersion: *specVersion, + extension: filepath.Ext(info.Name()), + failFast: failFast, + } + if info.IsDir() && info.Name() == "ingest_pipeline" { return filepath.SkipDir } if info.IsDir() { return nil } - err = formatFile(path, failFast, *specVersion) + + // Configure handling of keys with dots. + if !specVersion.LessThan(semver.MustParse("3.0.0")) { + if info.Name() == "manifest.yml" { + options.preferedKeysWithDotAction = KeysWithDotActionNested + } + } + + err = formatFile(path, options) if err != nil { return fmt.Errorf("formatting file failed (path: %s): %w", path, err) } @@ -61,14 +89,13 @@ func Format(packageRoot string, failFast bool) error { return nil } -func formatFile(path string, failFast bool, specVersion semver.Version) error { +func formatFile(path string, options formatterOptions) error { content, err := os.ReadFile(path) if err != nil { return fmt.Errorf("reading file content failed: %w", err) } - ext := filepath.Ext(filepath.Base(path)) - format := newFormatter(specVersion, ext) + format := newFormatter(options) if format == nil { return nil // no errors returned as we have few files that will be never formatted (png, svg, log, etc.) } @@ -82,7 +109,7 @@ func formatFile(path string, failFast bool, specVersion semver.Version) error { return nil } - if failFast { + if options.failFast { return fmt.Errorf("file is not formatted (path: %s)", path) } diff --git a/internal/formatter/yaml_formatter.go b/internal/formatter/yaml_formatter.go index 28e3d8ab2..1ab5aebd5 100644 --- a/internal/formatter/yaml_formatter.go +++ b/internal/formatter/yaml_formatter.go @@ -9,18 +9,17 @@ import ( "fmt" "strings" - "github.com/Masterminds/semver/v3" "gopkg.in/yaml.v3" ) // YAMLFormatter is responsible for formatting the given YAML input. type YAMLFormatter struct { - specVersion semver.Version + keysWithDotsAction int } -func NewYAMLFormatter(specVersion semver.Version) *YAMLFormatter { +func NewYAMLFormatter(keysWithDotsAction int) *YAMLFormatter { return &YAMLFormatter{ - specVersion: specVersion, + keysWithDotsAction: keysWithDotsAction, } } @@ -33,9 +32,7 @@ func (f *YAMLFormatter) Format(content []byte) ([]byte, bool, error) { return nil, false, fmt.Errorf("unmarshalling YAML file failed: %w", err) } - if !f.specVersion.LessThan(semver.MustParse("3.0.0")) { - extendNestedObjects(&node) - } + applyActionOnKeysWithDots(&node, f.keysWithDotsAction) var b bytes.Buffer encoder := yaml.NewEncoder(&b) @@ -55,6 +52,15 @@ func (f *YAMLFormatter) Format(content []byte) ([]byte, bool, error) { return formatted, string(content) == string(formatted), nil } +func applyActionOnKeysWithDots(node *yaml.Node, action int) { + switch action { + case KeysWithDotActionNested: + extendNestedObjects(node) + case KeysWithDotActionNone: + // Nothing to do. + } +} + func extendNestedObjects(node *yaml.Node) { if node.Kind == yaml.MappingNode { extendMapNode(node) diff --git a/internal/formatter/yaml_formatter_test.go b/internal/formatter/yaml_formatter_test.go index d714e2c92..c75f357e5 100644 --- a/internal/formatter/yaml_formatter_test.go +++ b/internal/formatter/yaml_formatter_test.go @@ -7,7 +7,6 @@ package formatter import ( "testing" - "github.com/Masterminds/semver/v3" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -104,9 +103,7 @@ es.other.level: 13`, }, } - sv := semver.MustParse("3.0.0") - formatter := NewYAMLFormatter(*sv).Format - + formatter := NewYAMLFormatter(KeysWithDotActionNested).Format for _, c := range cases { t.Run(c.title, func(t *testing.T) { result, _, err := formatter([]byte(c.doc)) diff --git a/internal/packages/changelog/yaml.go b/internal/packages/changelog/yaml.go index 53a8eb36b..c5579c990 100644 --- a/internal/packages/changelog/yaml.go +++ b/internal/packages/changelog/yaml.go @@ -125,7 +125,7 @@ func formatResult(result interface{}) ([]byte, error) { if err != nil { return nil, errors.New("failed to encode") } - yamlFormatter := &formatter.YAMLFormatter{} + yamlFormatter := formatter.NewYAMLFormatter(formatter.KeysWithDotActionNone) d, _, err = yamlFormatter.Format(d) if err != nil { return nil, errors.New("failed to format")