From feb20ac9fbdc581ef67b5757b48d9584e906263f Mon Sep 17 00:00:00 2001 From: urso Date: Thu, 12 Jan 2017 13:32:39 +0100 Subject: [PATCH] Backport: Update go-ucfg to version 0.4.5 (#3318) --- CHANGELOG.asciidoc | 1 + glide.yaml | 2 +- .../github.com/elastic/go-ucfg/CHANGELOG.md | 23 ++++++- vendor/github.com/elastic/go-ucfg/error.go | 8 +-- .../github.com/elastic/go-ucfg/flag/util.go | 2 +- .../github.com/elastic/go-ucfg/json/json.go | 2 +- vendor/github.com/elastic/go-ucfg/merge.go | 69 ++++++++++--------- vendor/github.com/elastic/go-ucfg/reify.go | 2 +- .../message/invalid_type_top_level.golden | 1 - .../invalid_type_top_level_w_meta.golden | 1 + .../invalid_type_top_level_wo_meta.golden | 1 + .../github.com/elastic/go-ucfg/yaml/yaml.go | 6 +- 12 files changed, 74 insertions(+), 44 deletions(-) delete mode 100644 vendor/github.com/elastic/go-ucfg/testdata/error/message/invalid_type_top_level.golden create mode 100644 vendor/github.com/elastic/go-ucfg/testdata/error/message/invalid_type_top_level_w_meta.golden create mode 100644 vendor/github.com/elastic/go-ucfg/testdata/error/message/invalid_type_top_level_wo_meta.golden diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index d98c6858768d..f9c0f80d9143 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -28,6 +28,7 @@ https://github.com/elastic/beats/compare/v5.1.1...master[Check the HEAD diff] ==== Bugfixes *Affecting all Beats* +- Fix overwriting explicit empty config sections {issue}2918[2918] *Metricbeat* diff --git a/glide.yaml b/glide.yaml index 8703e40bb831..1adbdb3b6c5f 100644 --- a/glide.yaml +++ b/glide.yaml @@ -83,7 +83,7 @@ import: - package: github.com/dustin/go-humanize version: 499693e27ee0d14ffab67c31ad065fdb3d34ea75 - package: github.com/elastic/go-ucfg - version: v0.4.3 + version: v0.4.5 - package: github.com/armon/go-socks5 version: 3a873e99f5400ad7706e464e905ffcc94b3ff247 - package: github.com/pkg/errors diff --git a/vendor/github.com/elastic/go-ucfg/CHANGELOG.md b/vendor/github.com/elastic/go-ucfg/CHANGELOG.md index 5af3edf402f5..de0633e29223 100644 --- a/vendor/github.com/elastic/go-ucfg/CHANGELOG.md +++ b/vendor/github.com/elastic/go-ucfg/CHANGELOG.md @@ -14,6 +14,25 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +## [0.4.5] + +### Changed +- merging sub-configs enforces strict variable expansion #85 + +### Fixed +- fix merging nil sub-configs #85 + +## [0.4.4] + +### Added +- Add support for pure array config files #82 + +### Changed +- Invalid top-level types return non-critical error (no stack-trace) on merge #82 + +### Fixed +- Fix panic when merging or creating a config from nil interface value #82 + ## [0.4.3] ### Changed @@ -149,7 +168,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Introduced CHANGELOG.md for documenting changes to ucfg. -[Unreleased]: https://github.com/elastic/go-ucfg/compare/v0.4.3...HEAD +[Unreleased]: https://github.com/elastic/go-ucfg/compare/v0.4.5...HEAD +[0.4.5]: https://github.com/elastic/go-ucfg/compare/v0.4.4...v0.4.5 +[0.4.4]: https://github.com/elastic/go-ucfg/compare/v0.4.3...v0.4.4 [0.4.3]: https://github.com/elastic/go-ucfg/compare/v0.4.2...v0.4.3 [0.4.2]: https://github.com/elastic/go-ucfg/compare/v0.4.1...v0.4.2 [0.4.1]: https://github.com/elastic/go-ucfg/compare/v0.4.0...v0.4.1 diff --git a/vendor/github.com/elastic/go-ucfg/error.go b/vendor/github.com/elastic/go-ucfg/error.go index e8ca01c5ecf8..5c8e8114c796 100644 --- a/vendor/github.com/elastic/go-ucfg/error.go +++ b/vendor/github.com/elastic/go-ucfg/error.go @@ -186,11 +186,11 @@ func raiseIndexOutOfBounds(opts *options, value value, idx int) Error { return raisePathErr(reason, value.meta(), message, ctx.path(".")) } -func raiseInvalidTopLevelType(v interface{}) Error { - // most likely developers fault +func raiseInvalidTopLevelType(v interface{}, meta *Meta) Error { + // could be developers or user fault t := chaseTypePointers(chaseValue(reflect.ValueOf(v)).Type()) - message := fmt.Sprintf("can not use go type '%v' for merging/unpacking configurations", t) - return raiseCritical(ErrTypeMismatch, message) + message := fmt.Sprintf("type '%v' is not supported on top level of config, only dictionary or list", t) + return raiseErr(ErrTypeMismatch, messageMeta(message, meta)) } func raiseKeyInvalidTypeUnpack(t reflect.Type, from *Config) Error { diff --git a/vendor/github.com/elastic/go-ucfg/flag/util.go b/vendor/github.com/elastic/go-ucfg/flag/util.go index ae1efa8f5fc3..07cc5eab6d90 100644 --- a/vendor/github.com/elastic/go-ucfg/flag/util.go +++ b/vendor/github.com/elastic/go-ucfg/flag/util.go @@ -35,7 +35,7 @@ func (v *FlagValue) String() string { if v.collector == nil { return "" } - + return toString(v.Config(), v.collector.GetOptions(), v.onError) } diff --git a/vendor/github.com/elastic/go-ucfg/json/json.go b/vendor/github.com/elastic/go-ucfg/json/json.go index 96de03ab03eb..2841103bc31f 100644 --- a/vendor/github.com/elastic/go-ucfg/json/json.go +++ b/vendor/github.com/elastic/go-ucfg/json/json.go @@ -8,7 +8,7 @@ import ( ) func NewConfig(in []byte, opts ...ucfg.Option) (*ucfg.Config, error) { - var m map[string]interface{} + var m interface{} if err := json.Unmarshal(in, &m); err != nil { return nil, err } diff --git a/vendor/github.com/elastic/go-ucfg/merge.go b/vendor/github.com/elastic/go-ucfg/merge.go index f6611633abbb..34d149faf65a 100644 --- a/vendor/github.com/elastic/go-ucfg/merge.go +++ b/vendor/github.com/elastic/go-ucfg/merge.go @@ -48,6 +48,11 @@ import ( // well. Passing cyclic structures to Merge will result in an infinite recursive // loop. func (c *Config) Merge(from interface{}, options ...Option) error { + // from is empty in case of empty config file + if from == nil { + return nil + } + opts := makeOptions(options) other, err := normalize(opts, from) @@ -71,27 +76,13 @@ func mergeConfigDict(opts *options, to, from *Config) Error { field: k, } - old, ok := to.fields.get(k) - if !ok { - to.fields.set(k, v.cpy(ctx)) - continue - } - - subOld, err := old.toConfig(opts) - if err != nil { - to.fields.set(k, v.cpy(ctx)) - continue - } - - subFrom, err := v.toConfig(opts) + old, _ := to.fields.get(k) + merged, err := mergeValues(opts, old, v) if err != nil { - to.fields.set(k, v.cpy(ctx)) - continue - } - - if err := mergeConfig(opts, subOld, subFrom); err != nil { return err } + + to.fields.set(k, merged.cpy(ctx)) } return nil } @@ -109,23 +100,13 @@ func mergeConfigArr(opts *options, to, from *Config) Error { field: fmt.Sprintf("%v", i), } - v := from.fields.array()[i] - old := to.fields.array()[i] - subOld, err := old.toConfig(opts) - if err != nil { - to.fields.setAt(i, cfgSub{to}, v.cpy(ctx)) - continue - } - - subFrom, err := v.toConfig(opts) + v := from.fields.array()[i] + merged, err := mergeValues(opts, old, v) if err != nil { - to.fields.setAt(i, cfgSub{to}, v.cpy(ctx)) - } - - if err := mergeConfig(opts, subOld, subFrom); err != nil { return err } + to.fields.setAt(i, cfgSub{to}, merged.cpy(ctx)) } end := len(from.fields.array()) @@ -146,6 +127,30 @@ func mergeConfigArr(opts *options, to, from *Config) Error { return nil } +func mergeValues(opts *options, old, v value) (value, Error) { + if old == nil { + return v, nil + } + + // check if new and old value evaluate to sub-configurations. If one is no + // sub-configuration, use new value only. + subOld, err := old.toConfig(opts) + if err != nil { + return v, nil + } + subV, err := v.toConfig(opts) + if err != nil { + return v, nil + } + + // merge new and old evaluated sub-configurations and return subOld for + // reassigning to old key in case of subOld being generated dynamically + if err := mergeConfig(opts, subOld, subV); err != nil { + return nil, err + } + return cfgSub{subOld}, nil +} + // convert from into normalized *Config checking for errors // before merging generated(normalized) config with current config func normalize(opts *options, from interface{}) (*Config, Error) { @@ -179,7 +184,7 @@ func normalize(opts *options, from interface{}) (*Config, Error) { } - return nil, raiseInvalidTopLevelType(from) + return nil, raiseInvalidTopLevelType(from, opts.meta) } func normalizeMap(opts *options, from reflect.Value) (*Config, Error) { diff --git a/vendor/github.com/elastic/go-ucfg/reify.go b/vendor/github.com/elastic/go-ucfg/reify.go index 752e7c4b5f74..493f0da2e772 100644 --- a/vendor/github.com/elastic/go-ucfg/reify.go +++ b/vendor/github.com/elastic/go-ucfg/reify.go @@ -142,7 +142,7 @@ func reifyInto(opts *options, to reflect.Value, from *Config) Error { return nil } - return raiseInvalidTopLevelType(to.Interface()) + return raiseInvalidTopLevelType(to.Interface(), opts.meta) } func reifyMap(opts *options, to reflect.Value, from *Config) Error { diff --git a/vendor/github.com/elastic/go-ucfg/testdata/error/message/invalid_type_top_level.golden b/vendor/github.com/elastic/go-ucfg/testdata/error/message/invalid_type_top_level.golden deleted file mode 100644 index b67c2ce8e58a..000000000000 --- a/vendor/github.com/elastic/go-ucfg/testdata/error/message/invalid_type_top_level.golden +++ /dev/null @@ -1 +0,0 @@ -(assert) can not use go type 'string' for merging/unpacking configurations \ No newline at end of file diff --git a/vendor/github.com/elastic/go-ucfg/testdata/error/message/invalid_type_top_level_w_meta.golden b/vendor/github.com/elastic/go-ucfg/testdata/error/message/invalid_type_top_level_w_meta.golden new file mode 100644 index 000000000000..4c1dfdaf7565 --- /dev/null +++ b/vendor/github.com/elastic/go-ucfg/testdata/error/message/invalid_type_top_level_w_meta.golden @@ -0,0 +1 @@ +type 'string' is not supported on top level of config, only dictionary or list (source:'test.source') \ No newline at end of file diff --git a/vendor/github.com/elastic/go-ucfg/testdata/error/message/invalid_type_top_level_wo_meta.golden b/vendor/github.com/elastic/go-ucfg/testdata/error/message/invalid_type_top_level_wo_meta.golden new file mode 100644 index 000000000000..2325f15e7e56 --- /dev/null +++ b/vendor/github.com/elastic/go-ucfg/testdata/error/message/invalid_type_top_level_wo_meta.golden @@ -0,0 +1 @@ +type 'string' is not supported on top level of config, only dictionary or list \ No newline at end of file diff --git a/vendor/github.com/elastic/go-ucfg/yaml/yaml.go b/vendor/github.com/elastic/go-ucfg/yaml/yaml.go index d5449e0e1108..f5eaefeb86c3 100644 --- a/vendor/github.com/elastic/go-ucfg/yaml/yaml.go +++ b/vendor/github.com/elastic/go-ucfg/yaml/yaml.go @@ -3,15 +3,17 @@ package yaml import ( "io/ioutil" - "github.com/elastic/go-ucfg" "gopkg.in/yaml.v2" + + "github.com/elastic/go-ucfg" ) func NewConfig(in []byte, opts ...ucfg.Option) (*ucfg.Config, error) { - var m map[string]interface{} + var m interface{} if err := yaml.Unmarshal(in, &m); err != nil { return nil, err } + return ucfg.NewFrom(m, opts...) }