Skip to content

Commit

Permalink
Merge pull request #520 from jonathan-buttner/ecs-style-dataset
Browse files Browse the repository at this point in the history
Support validation ECS style dataset format
  • Loading branch information
jonathan-buttner authored Jun 18, 2020
2 parents 5809c67 + c8dff92 commit 8a32f33
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Do not require "config.yml". [#508](https://github.com/elastic/package-registry/pull/508)
* Validate version consistency. [#510](https://github.com/elastic/package-registry/pull/510)
* Remove package code generator. [#513](https://github.com/elastic/package-registry/pull/513)
* Added support for ecs style validation for dataset fields. [#520](https://github.com/elastic/package-registry/pull/520)

### Deprecated

Expand Down
59 changes: 58 additions & 1 deletion util/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,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 {
Expand All @@ -306,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")
Expand Down

0 comments on commit 8a32f33

Please sign in to comment.