Skip to content

Commit

Permalink
Update FromInterface & Result As method
Browse files Browse the repository at this point in the history
  • Loading branch information
thedevsaddam committed Jan 13, 2020
1 parent 7213980 commit 53ac464
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
12 changes: 12 additions & 0 deletions jsonq.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ func (j *JSONQ) From(node string) *JSONQ {
return j
}

// FromInterface reads the content from valid map[string]interface{}
func (j *JSONQ) FromInterface(v interface{}) *JSONQ {
switch data := v.(type) {
case []interface{}, map[string]interface{}, map[string][]interface{}:
j.rootJSONContent = data
j.jsonContent = j.rootJSONContent
default:
j.addError(fmt.Errorf("invalid type [%T]", v))
}
return j
}

// Select use for selection of the properties from query result
func (j *JSONQ) Select(properties ...string) *JSONQ {
j.attributes = append(j.attributes, properties...)
Expand Down
18 changes: 18 additions & 0 deletions jsonq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,24 @@ func TestJSONQ_From(t *testing.T) {
assertJSON(t, out, expJSON, "accessing group by data")
}

func TestJSONQ_FromInterface(t *testing.T) {
var v map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &v)
if err != nil {
t.Error(err)
}
jq := New().FromInterface(v)
if jq.rootJSONContent == nil || jq.jsonContent == nil {
t.Errorf("failed to assign value using FromInterface method")
}

var customType float64

This comment has been minimized.

Copy link
@thedevsaddam

thedevsaddam Jan 13, 2020

Author Owner

Need to fix the typo!

jq = New().FromInterface(customType)
if jq.Error() == nil {
t.Errorf("failed to set error properly for FromInterface method")
}
}

func TestJSONQ_Where_single_where(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
Expand Down
4 changes: 4 additions & 0 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (r *Result) As(v interface{}) error {
"*[]float32": "Float32Slice", "*[]float64": "Float64Slice",
}

if methodMap[method] == "" {
return fmt.Errorf("gojsonq: type [%T] is not available", v)
}

vv := reflect.ValueOf(r).MethodByName(methodMap[method]).Call(nil)
if vv != nil {
if vv[1].Interface() != nil {
Expand Down
18 changes: 18 additions & 0 deletions result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestNil(t *testing.T) {
}

func TestAs(t *testing.T) {
type flt float64
testCases := []struct {
tag string
value interface{}
Expand Down Expand Up @@ -96,6 +97,23 @@ func TestAs(t *testing.T) {
},
errExpect: true,
},
{
tag: "custom type error check",
value: string("*nop"),
newExpectedValue: func() interface{} {
var a flt
return &a
},
expect: func(value, i interface{}) bool {
val, ok := i.(string)
if !ok {
return false
}

return val == (value.(string))
},
errExpect: true,
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 53ac464

Please sign in to comment.