Skip to content

Commit 53ac464

Browse files
committed
Update FromInterface & Result As method
1 parent 7213980 commit 53ac464

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

jsonq.go

+12
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ func (j *JSONQ) From(node string) *JSONQ {
150150
return j
151151
}
152152

153+
// FromInterface reads the content from valid map[string]interface{}
154+
func (j *JSONQ) FromInterface(v interface{}) *JSONQ {
155+
switch data := v.(type) {
156+
case []interface{}, map[string]interface{}, map[string][]interface{}:
157+
j.rootJSONContent = data
158+
j.jsonContent = j.rootJSONContent
159+
default:
160+
j.addError(fmt.Errorf("invalid type [%T]", v))
161+
}
162+
return j
163+
}
164+
153165
// Select use for selection of the properties from query result
154166
func (j *JSONQ) Select(properties ...string) *JSONQ {
155167
j.attributes = append(j.attributes, properties...)

jsonq_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,24 @@ func TestJSONQ_From(t *testing.T) {
326326
assertJSON(t, out, expJSON, "accessing group by data")
327327
}
328328

329+
func TestJSONQ_FromInterface(t *testing.T) {
330+
var v map[string]interface{}
331+
err := json.Unmarshal([]byte(jsonStr), &v)
332+
if err != nil {
333+
t.Error(err)
334+
}
335+
jq := New().FromInterface(v)
336+
if jq.rootJSONContent == nil || jq.jsonContent == nil {
337+
t.Errorf("failed to assign value using FromInterface method")
338+
}
339+
340+
var customType float64
341+
jq = New().FromInterface(customType)
342+
if jq.Error() == nil {
343+
t.Errorf("failed to set error properly for FromInterface method")
344+
}
345+
}
346+
329347
func TestJSONQ_Where_single_where(t *testing.T) {
330348
jq := New().FromString(jsonStr).
331349
From("vendor.items").

result.go

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func (r *Result) As(v interface{}) error {
5757
"*[]float32": "Float32Slice", "*[]float64": "Float64Slice",
5858
}
5959

60+
if methodMap[method] == "" {
61+
return fmt.Errorf("gojsonq: type [%T] is not available", v)
62+
}
63+
6064
vv := reflect.ValueOf(r).MethodByName(methodMap[method]).Call(nil)
6165
if vv != nil {
6266
if vv[1].Interface() != nil {

result_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestNil(t *testing.T) {
2121
}
2222

2323
func TestAs(t *testing.T) {
24+
type flt float64
2425
testCases := []struct {
2526
tag string
2627
value interface{}
@@ -96,6 +97,23 @@ func TestAs(t *testing.T) {
9697
},
9798
errExpect: true,
9899
},
100+
{
101+
tag: "custom type error check",
102+
value: string("*nop"),
103+
newExpectedValue: func() interface{} {
104+
var a flt
105+
return &a
106+
},
107+
expect: func(value, i interface{}) bool {
108+
val, ok := i.(string)
109+
if !ok {
110+
return false
111+
}
112+
113+
return val == (value.(string))
114+
},
115+
errExpect: true,
116+
},
99117
}
100118

101119
for _, tc := range testCases {

0 commit comments

Comments
 (0)