Skip to content

Commit

Permalink
feat: accept context conf arrays (#26)
Browse files Browse the repository at this point in the history
* feat: accept context conf arrays

* fix: add extra line
  • Loading branch information
mariolg authored Apr 28, 2021
1 parent ea4da6c commit 394a5d3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
3 changes: 3 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func (m *gjsonMap) Get(path string) interface{} {
case gjson.Number:
return result.Float()
default:
if result.IsArray() {
return result.Array()
}
return result.String()
}
}
46 changes: 46 additions & 0 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"

"github.com/cucumber/godog"
"github.com/tidwall/gjson"
)

// ConvertTableToMap converts a godog table with 2 columns into a map[string]interface{}.
Expand Down Expand Up @@ -142,6 +143,8 @@ func ConvertTableWithHeaderToStructSlice(ctx context.Context, t *godog.Table, sl
// })
// It will be equivalent to:
// testElement := TestElement{Name: "example 1", Value: 1}
// Warning: still pending process values directly as arrays, i.e.: | addresses | ["http://localhost:8080"] |
// use by now a CONF tag, i.e.: | addresses | [CONF:elasticsearch.addresses] |
func ConvertTableWithoutHeaderToStruct(ctx context.Context, t *godog.Table, v interface{}) error {
if len(t.Rows) == 0 {
return nil
Expand Down Expand Up @@ -189,6 +192,22 @@ func assignFieldInStruct(value reflect.Value, fieldName string, fieldValue inter
f = fv.Elem()
}
fieldValueStr := fmt.Sprintf("%v", fieldValue)
if f.Kind() == reflect.Slice {
array, ok := fieldValue.([]gjson.Result)
if !ok {
return fmt.Errorf("failed setting the field '%s' with value '%s', not an array/slice", fieldName, fieldValueStr)
}
length := len(array)
var fv reflect.Value
if length > 0 {
fv = makeSlice(array[0], length)
for i, v := range array {
setSliceValue(fv.Index(i), v)
}
}
f.Set(fv)
return nil
}
if f.Kind() == reflect.String {
f.SetString(fieldValueStr)
return nil
Expand Down Expand Up @@ -235,3 +254,30 @@ func assignFieldInStruct(value reflect.Value, fieldName string, fieldValue inter
}
return nil
}

func makeSlice(element gjson.Result, length int) reflect.Value {
var rv reflect.Value
switch element.Type {
case gjson.False, gjson.True:
var b bool
rv = reflect.ValueOf(b)
case gjson.Number:
var i int
rv = reflect.ValueOf(i)
case gjson.String, gjson.JSON, gjson.Null:
var s string
rv = reflect.ValueOf(s)
}
return reflect.MakeSlice(reflect.SliceOf(rv.Type()), length, length)
}

func setSliceValue(field reflect.Value, value gjson.Result) {
switch value.Type {
case gjson.False, gjson.True:
field.Set(reflect.ValueOf(value.Bool()))
case gjson.Number:
field.Set(reflect.ValueOf(value.Int()))
case gjson.String, gjson.JSON, gjson.Null:
field.Set(reflect.ValueOf(value.String()))
}
}
5 changes: 5 additions & 0 deletions test/acceptance/environments/local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,8 @@ signPublicKey: |
# HTTP mock settings
httpMockUrl: http://localhost:9000

# elasticsearch settings
elasticsearch:
addresses:
- http://localhost:9200
2 changes: 1 addition & 1 deletion test/acceptance/features/elasticsearch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Feature: Elasticsearch client
@elasticsearch
Scenario: Create and search a document
Given the elasticsearch server
| addresses | [ http://localhost:9200 ] |
| addresses | [CONF:elasticsearch.addresses] |
When I create the elasticsearch document with index "example" and the JSON properties
| name | example |
| number | [NUMBER:1] |
Expand Down

0 comments on commit 394a5d3

Please sign in to comment.