diff --git a/go.mod b/go.mod index 4ea19c4773..f41e26bc42 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/elastic/package-storage go 1.12 require ( - github.com/elastic/package-registry v0.4.1-0.20200618094131-9b38a6660e60 + github.com/elastic/package-registry v0.4.1-0.20200618213757-98d7184cfe5b github.com/magefile/mage v1.9.0 github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.4.0 diff --git a/go.sum b/go.sum index eda4fa18dc..d8ed9aff54 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ github.com/elastic/package-registry v0.4.1-0.20200617095959-29f7f04363ed h1:XVk2 github.com/elastic/package-registry v0.4.1-0.20200617095959-29f7f04363ed/go.mod h1:oQx3Tg9ynuC6APd0o0OHud9kyPX6S6IzdJp/R4Hj1HY= github.com/elastic/package-registry v0.4.1-0.20200618094131-9b38a6660e60 h1:Piz/H5PKuy4taQZYxbp6np4otpO77J9RbjH0f+4hzys= github.com/elastic/package-registry v0.4.1-0.20200618094131-9b38a6660e60/go.mod h1:oQx3Tg9ynuC6APd0o0OHud9kyPX6S6IzdJp/R4Hj1HY= +github.com/elastic/package-registry v0.4.1-0.20200618213757-98d7184cfe5b h1:NKaNWxpZjr2V750h2Q6q52W4ZGGHdX0TOcNaFrhnk3w= +github.com/elastic/package-registry v0.4.1-0.20200618213757-98d7184cfe5b/go.mod h1:oQx3Tg9ynuC6APd0o0OHud9kyPX6S6IzdJp/R4Hj1HY= github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 h1:rp+c0RAYOWj8l6qbCUTSiRLG/iKnW3K3/QfPPuSsBt4= diff --git a/vendor/github.com/elastic/package-registry/util/dataset.go b/vendor/github.com/elastic/package-registry/util/dataset.go index d76d9df3c2..0e8200cd0f 100644 --- a/vendor/github.com/elastic/package-registry/util/dataset.go +++ b/vendor/github.com/elastic/package-registry/util/dataset.go @@ -277,17 +277,12 @@ func (d *DataSet) validateRequiredFields() error { return errors.Wrapf(err, "walking through fields files failed") } - err = validateRequiredFieldsData(allFields) - if err != nil { - return errors.Wrapf(err, "validating required fields data failed") + // Flatten all fields + for i, fields := range allFields { + allFields[i] = fields.Flatten() } - return nil -} -func validateRequiredFieldsData(allFields []MapStr) error { - allFields = flattenFieldsData(allFields) - - var err error + // Verify required keys err = requireField(allFields, "dataset.type", "constant_keyword", err) err = requireField(allFields, "dataset.name", "constant_keyword", err) err = requireField(allFields, "dataset.namespace", "constant_keyword", err) @@ -295,14 +290,6 @@ func validateRequiredFieldsData(allFields []MapStr) error { return err } -func flattenFieldsData(allFields []MapStr) []MapStr { - var modified []MapStr - for _, fields := range allFields { - modified = append(modified, fields.FlattenWithoutChildrenKey("fields")) - } - return modified -} - func requireField(allFields []MapStr, searchedName, expectedType string, validationErr error) error { if validationErr != nil { return validationErr @@ -310,7 +297,10 @@ func requireField(allFields []MapStr, searchedName, expectedType string, validat f, err := findField(allFields, searchedName) if err != nil { - return errors.Wrapf(err, "finding field failed (searchedName: %s)", searchedName) + f, err = findFieldSplit(allFields, searchedName) + if err != nil { + return errors.Wrapf(err, "finding field failed (searchedName: %s)", searchedName) + } } if f.aType != expectedType { @@ -319,6 +309,60 @@ func requireField(allFields []MapStr, searchedName, expectedType string, validat return nil } +func findFieldSplit(allFields []MapStr, searchedName string) (*fieldEntry, error) { + levels := strings.Split(searchedName, ".") + curFields := allFields + var err error + for _, part := range levels[:len(levels)-1] { + curFields, err = getFieldsArray(curFields, part) + if err != nil { + return nil, errors.Wrapf(err, "failed to find fields array") + } + } + return findField(curFields, levels[len(levels)-1]) +} + +func createMapStr(in interface{}) (MapStr, error) { + m := make(MapStr) + v, ok := in.(map[interface{}]interface{}) + if !ok { + return nil, fmt.Errorf("unable to convert %v to known type", in) + } + for k, val := range v { + m[fmt.Sprintf("%v", k)] = fmt.Sprintf("%v", val) + } + return m, nil +} + +func getFieldsArray(allFields []MapStr, searchedName string) ([]MapStr, error) { + for _, fields := range allFields { + name, err := fields.GetValue("name") + if err != nil { + return nil, errors.Wrapf(err, "cannot get value (key: name)") + } + if name == searchedName { + value, err := fields.GetValue("fields") + if err != nil { + return nil, errors.Wrapf(err, "cannot get fields") + } + + if inArray, ok := value.([]interface{}); ok { + m := make([]MapStr, 0, len(inArray)) + for _, in := range inArray { + mapStr, err := createMapStr(in) + if err != nil { + return nil, errors.Wrapf(err, "cannot create MapStr") + } + m = append(m, mapStr) + } + return m, nil + } + return nil, fmt.Errorf("fields was not []MapStr") + } + } + return nil, fmt.Errorf("field '%s' not found", searchedName) +} + func findField(allFields []MapStr, searchedName string) (*fieldEntry, error) { for _, fields := range allFields { name, err := fields.GetValue("name") diff --git a/vendor/github.com/elastic/package-registry/util/mapstr.go b/vendor/github.com/elastic/package-registry/util/mapstr.go index eafbb7d72a..c9ff22146f 100644 --- a/vendor/github.com/elastic/package-registry/util/mapstr.go +++ b/vendor/github.com/elastic/package-registry/util/mapstr.go @@ -207,30 +207,6 @@ func flatten(prefix string, in, out MapStr) MapStr { return out } -func (m MapStr) FlattenWithoutChildrenKey(childrenKey string) MapStr { - return flattenWithoutChildrenKey("", childrenKey, m, MapStr{}) -} - -func flattenWithoutChildrenKey(prefix, childrenKey string, in, out MapStr) MapStr { - for k, v := range in { - var fullKey string - if prefix == "" { - fullKey = k - } else if k == childrenKey { - fullKey = prefix - } else { - fullKey = prefix + "." + k - } - - if m, ok := tryToMapStr(v); ok { - flattenWithoutChildrenKey(fullKey, childrenKey, m, out) - } else { - out[fullKey] = v - } - } - return out -} - // toMapStr performs a type assertion on v and returns a MapStr. v can be either // a MapStr or a map[string]interface{}. If it's any other type or nil then // an error is returned. diff --git a/vendor/github.com/elastic/package-registry/util/package.go b/vendor/github.com/elastic/package-registry/util/package.go index 3be164cf5b..1ef80d8ce0 100644 --- a/vendor/github.com/elastic/package-registry/util/package.go +++ b/vendor/github.com/elastic/package-registry/util/package.go @@ -333,7 +333,11 @@ func (p *Package) validateVersionConsistency() error { } if !versionPackage.Equal(versionDir) { - return fmt.Errorf("inconsistent versions (path: %s, manifest: %s)", versionDir.String(), p.versionSemVer.String()) + manifestVer := "unknown" + if p.versionSemVer != nil { + manifestVer = p.versionSemVer.String() + } + return fmt.Errorf("inconsistent versions (path: %s, manifest: %s)", versionDir.String(), manifestVer) } return nil } diff --git a/vendor/modules.txt b/vendor/modules.txt index c8891f5e12..2f144d5a7a 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -11,7 +11,7 @@ github.com/davecgh/go-spew/spew github.com/elastic/go-ucfg github.com/elastic/go-ucfg/parse github.com/elastic/go-ucfg/yaml -# github.com/elastic/package-registry v0.4.1-0.20200618094131-9b38a6660e60 +# github.com/elastic/package-registry v0.4.1-0.20200618213757-98d7184cfe5b github.com/elastic/package-registry/util # github.com/magefile/mage v1.9.0 github.com/magefile/mage/mg