Skip to content

Commit 7213980

Browse files
committed
Update As method
1 parent e6212fc commit 7213980

File tree

4 files changed

+81
-31
lines changed

4 files changed

+81
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
![gojsonq-logo](gojsonq.png)
22

33
[![Build Status](https://travis-ci.org/thedevsaddam/gojsonq.svg?branch=master)](https://travis-ci.org/thedevsaddam/gojsonq)
4-
[![Project status](https://img.shields.io/badge/version-v2.3-green.svg)](https://github.com/thedevsaddam/gojsonq/releases)
4+
[![Project status](https://img.shields.io/badge/version-v2.4-green.svg)](https://github.com/thedevsaddam/gojsonq/releases)
55
[![Go Report Card](https://goreportcard.com/badge/github.com/thedevsaddam/gojsonq)](https://goreportcard.com/report/github.com/thedevsaddam/gojsonq)
66
[![Coverage Status](https://coveralls.io/repos/github/thedevsaddam/gojsonq/badge.svg?branch=master)](https://coveralls.io/github/thedevsaddam/gojsonq?branch=master)
77
[![GoDoc](https://godoc.org/github.com/thedevsaddam/gojsonq?status.svg)](https://godoc.org/github.com/thedevsaddam/gojsonq)

option_test.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,58 @@ import (
44
"testing"
55
)
66

7-
func TestSetDecoder(t *testing.T) {
7+
func TestWithDecoder(t *testing.T) {
88
jq := New(WithDecoder(&cDecoder{}))
99
if jq.option.decoder == nil {
1010
t.Error("failed to set decoder as option")
1111
}
1212
}
1313

14-
func TestSetDecoder_with_nil_expecting_an_error(t *testing.T) {
14+
func TestWithDecoder_with_nil_expecting_an_error(t *testing.T) {
1515
jq := New(WithDecoder(nil))
16+
if jq.Error() == nil {
17+
t.Error("failed to catch nil in WithDecoder")
18+
}
19+
}
20+
21+
func TestWithSeparator(t *testing.T) {
22+
jq := New(WithSeparator("->"))
23+
if jq.option.separator != "->" {
24+
t.Error("failed to set separator as option")
25+
}
26+
}
27+
28+
func TestWithSeparator_with_nil_expecting_an_error(t *testing.T) {
29+
jq := New(WithSeparator(""))
30+
if jq.Error() == nil {
31+
t.Error("failed to catch nil in WithSeparator")
32+
}
33+
}
34+
35+
// to increase the code coverage; will remove in major release
36+
func TestSetDecoder(t *testing.T) {
37+
jq := New(SetDecoder(&cDecoder{}))
38+
if jq.option.decoder == nil {
39+
t.Error("failed to set decoder as option")
40+
}
41+
}
42+
43+
func TestSetDecoder_with_nil_expecting_an_error(t *testing.T) {
44+
jq := New(SetDecoder(nil))
1645
if jq.Error() == nil {
1746
t.Error("failed to catch nil in SetDecoder")
1847
}
1948
}
2049

2150
func TestSetSeparator(t *testing.T) {
22-
jq := New(WithSeparator("->"))
51+
jq := New(SetSeparator("->"))
2352
if jq.option.separator != "->" {
2453
t.Error("failed to set separator as option")
2554
}
2655
}
2756

2857
func TestSetSeparator_with_nil_expecting_an_error(t *testing.T) {
29-
jq := New(WithSeparator(""))
58+
jq := New(SetSeparator(""))
3059
if jq.Error() == nil {
3160
t.Error("failed to catch nil in SetSeparator")
3261
}

result.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
const errMessage = "gojsonq: wrong method call for %v"
1111

12+
// Available named error values
1213
var (
1314
ErrExpectsPointer = fmt.Errorf("gojsonq: failed to unmarshal, expects pointer")
1415
ErrImmutable = fmt.Errorf("gojsonq: failed to unmarshal, target is not mutable")
@@ -30,7 +31,7 @@ func (r *Result) Nil() bool {
3031
return r.value == nil
3132
}
3233

33-
// As sets the value of Result to v
34+
// As sets the value of Result to v; It does not support methods with argument available in Result
3435
func (r *Result) As(v interface{}) error {
3536
if r.value != nil {
3637
rv := reflect.ValueOf(v)
@@ -43,11 +44,26 @@ func (r *Result) As(v interface{}) error {
4344
return ErrImmutable
4445
}
4546

46-
value := reflect.ValueOf(r.value)
47-
if !reflect.TypeOf(r.value).AssignableTo(reflect.TypeOf(v).Elem()) {
48-
return ErrTypeMismatch
47+
method := rv.Type().String()
48+
methodMap := map[string]string{
49+
"*string": "String", "*bool": "Bool", "*time.Duration": "Duration",
50+
"*int": "Int", "*int8": "Int8", "*int16": "Int16", "*int32": "Int32",
51+
"*uint": "Uint", "*uint8": "Uint8", "*uint16": "Uint16", "*uint32": "Uint32",
52+
"*float32": "Float32", "*float64": "Float64",
53+
54+
"*[]string": "StringSlice", "*[]bool": "BoolSlice", "*[]time.Duration": "DurationSlice",
55+
"*[]int": "IntSlice", "*[]int8": "Int8Slice", "*[]int16": "Int16Slice", "*[]int32": "Int32Slice",
56+
"*[]uint": "UintSlice", "*[]uint8": "Uint8Slice", "*[]uint16": "Uint16Slice", "*[]uint32": "Uint32Slice",
57+
"*[]float32": "Float32Slice", "*[]float64": "Float64Slice",
58+
}
59+
60+
vv := reflect.ValueOf(r).MethodByName(methodMap[method]).Call(nil)
61+
if vv != nil {
62+
if vv[1].Interface() != nil {
63+
return ErrTypeMismatch
64+
}
65+
rv.Elem().Set(vv[0])
4966
}
50-
rv.Elem().Set(value)
5167
}
5268
return nil
5369
}

result_test.go

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,65 +29,70 @@ func TestAs(t *testing.T) {
2929
errExpect bool
3030
}{
3131
{
32-
tag: "as int32",
33-
value: int32(1),
32+
tag: "float64 as int", // golang unmarshal number to float64
33+
value: float64(1),
3434
newExpectedValue: func() interface{} {
35-
var a int32
35+
var a int
3636
return &a
3737
},
3838
expect: func(value, i interface{}) bool {
39-
val, ok := i.(int32)
39+
val, ok := i.(int)
4040
if !ok {
4141
return false
4242
}
4343

44-
return val == (value.(int32))
44+
return val == (value.(int))
4545
},
4646
errExpect: false,
4747
},
4848
{
49-
tag: "int32 as string",
50-
value: int32(1),
49+
tag: "float64 as uint",
50+
value: float64(1),
5151
newExpectedValue: func() interface{} {
52-
var a string
52+
var a uint
5353
return &a
5454
},
5555
expect: func(value, i interface{}) bool {
56-
return false
56+
val, ok := i.(uint)
57+
if !ok {
58+
return false
59+
}
60+
61+
return val == (value.(uint))
5762
},
58-
errExpect: true,
63+
errExpect: false,
5964
},
6065
{
61-
tag: "int32 as int64",
62-
value: int32(1),
66+
tag: "float64 assign to non ptr",
67+
value: float64(1),
6368
newExpectedValue: func() interface{} {
64-
var a int64
65-
return &a
69+
var a float64
70+
return a
6671
},
6772
expect: func(value, i interface{}) bool {
68-
val, ok := i.(int64)
73+
val, ok := i.(float64)
6974
if !ok {
7075
return false
7176
}
7277

73-
return val == (value.(int64))
78+
return val == (value.(float64))
7479
},
7580
errExpect: true,
7681
},
7782
{
78-
tag: "int32 as int16",
79-
value: int32(1),
83+
tag: "string assign to int",
84+
value: string("*nop"),
8085
newExpectedValue: func() interface{} {
81-
var a int16
86+
var a float64
8287
return &a
8388
},
8489
expect: func(value, i interface{}) bool {
85-
val, ok := i.(int16)
90+
val, ok := i.(string)
8691
if !ok {
8792
return false
8893
}
8994

90-
return val == (value.(int16))
95+
return val == (value.(string))
9196
},
9297
errExpect: true,
9398
},

0 commit comments

Comments
 (0)