Skip to content
This repository has been archived by the owner on Jan 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #23 from adambabik/fix/enum-parameter-value
Browse files Browse the repository at this point in the history
fix parsing values of enum parameters
  • Loading branch information
subosito authored Jul 11, 2017
2 parents 20f3d04 + e9bd853 commit 30f69be
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
27 changes: 27 additions & 0 deletions api/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
)

var ErrIsNotSlice = errors.New("is not a slice")

type Element struct {
object interface{}
}
Expand Down Expand Up @@ -96,6 +98,31 @@ func (b *Element) ChildrenMap() (map[string]*Element, error) {
return nil, errors.New("is not an object")
}

func (b *Element) FlatChildren() ([]*Element, error) {
val := b.Value()

switch val.Kind() {
case reflect.Slice:
children := make([]*Element, 0, val.Len())

for i := 0; i < val.Len(); i++ {
childVal := val.Index(i).Elem()

if nestedChildren, err := (&Element{childVal.Interface()}).FlatChildren(); err == nil {
children = append(children, nestedChildren...)
} else if err == ErrIsNotSlice {
children = append(children, &Element{childVal.Interface()})
} else {
return nil, err
}
}

return children, nil
}

return nil, ErrIsNotSlice
}

func (b *Element) hierarchy(key string) []string {
return strings.Split(key, ".")
}
Expand Down
15 changes: 14 additions & 1 deletion api/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,23 @@ func extractHrefs(child *Element) (h Href) {
}

for _, content := range contents {
value := content.Path("content.value.content").String()

if content.Path("content.value.element").String() == "enum" {
// Enum values are stored in a different location.
// As `attributes.samples` is an array of arrays
// and Parameter does not support multiple values,
// flattening the array and selecting the first elem works fine.
samples, err := content.Path("content.value.attributes.samples").FlatChildren()
if err == nil && len(samples) > 0 {
value = samples[0].Path("content").String()
}
}

v := &Parameter{
Required: isContains("attributes.typeAttributes", "required", content),
Key: content.Path("content.key.content").String(),
Value: content.Path("content.value.content").String(),
Value: value,
Kind: content.Path("meta.title").String(),
Description: content.Path("meta.description").String(),
}
Expand Down

0 comments on commit 30f69be

Please sign in to comment.