diff --git a/jsonq.go b/jsonq.go index 6845126..b423ab4 100644 --- a/jsonq.go +++ b/jsonq.go @@ -505,6 +505,15 @@ func (j *JSONQ) Only(properties ...string) interface{} { return j.prepare().only(properties...) } +// OnlyR collects the properties from a list of object and return as Result instance +func (j *JSONQ) OnlyR(properties ...string) (*Result, error) { + v := j.Only(properties...) + if err := j.Error(); err != nil { + return nil, err + } + return NewResult(v), nil +} + // Pluck build an array of vlaues form a property of a list of objects func (j *JSONQ) Pluck(property string) interface{} { j.prepare() @@ -527,6 +536,15 @@ func (j *JSONQ) Pluck(property string) interface{} { return result } +// PluckR build an array of vlaues form a property of a list of objects and return as Result instance +func (j *JSONQ) PluckR(property string) (*Result, error) { + v := j.Pluck(property) + if err := j.Error(); err != nil { + return nil, err + } + return NewResult(v), nil +} + // reset resets the current state of JSONQ instance func (j *JSONQ) reset() *JSONQ { j.raw = nil @@ -558,6 +576,15 @@ func (j *JSONQ) Get() interface{} { return j.jsonContent } +// GetR return the query results as Result instance +func (j *JSONQ) GetR() (*Result, error) { + v := j.Get() + if err := j.Error(); err != nil { + return nil, err + } + return NewResult(v), nil +} + // First returns the first element of a list func (j *JSONQ) First() interface{} { j.prepare() @@ -569,6 +596,15 @@ func (j *JSONQ) First() interface{} { return empty } +// FirstR returns the first element of a list as Result instance +func (j *JSONQ) FirstR() (*Result, error) { + v := j.First() + if err := j.Error(); err != nil { + return nil, err + } + return NewResult(v), nil +} + // Last returns the last element of a list func (j *JSONQ) Last() interface{} { j.prepare() @@ -580,6 +616,15 @@ func (j *JSONQ) Last() interface{} { return empty } +// LastR returns the last element of a list as Result instance +func (j *JSONQ) LastR() (*Result, error) { + v := j.Last() + if err := j.Error(); err != nil { + return nil, err + } + return NewResult(v), nil +} + // Nth returns the nth element of a list func (j *JSONQ) Nth(index int) interface{} { if index == 0 { @@ -606,11 +651,29 @@ func (j *JSONQ) Nth(index int) interface{} { return empty } +// NthR returns the nth element of a list as Result instance +func (j *JSONQ) NthR(index int) (*Result, error) { + v := j.Nth(index) + if err := j.Error(); err != nil { + return nil, err + } + return NewResult(v), nil +} + // Find returns the result of a exact matching path func (j *JSONQ) Find(path string) interface{} { return j.From(path).Get() } +// FindR returns the result as Result instance from the exact matching path +func (j *JSONQ) FindR(path string) (*Result, error) { + v := j.Find(path) + if err := j.Error(); err != nil { + return nil, err + } + return NewResult(v), nil +} + // Count returns the number of total items. // This could be a length of list/array/map func (j *JSONQ) Count() int { diff --git a/jsonq_test.go b/jsonq_test.go index 16ffd37..c77d195 100644 --- a/jsonq_test.go +++ b/jsonq_test.go @@ -769,6 +769,24 @@ func TestJSONQ_Only_with_distinct(t *testing.T) { assertJSON(t, out, expected) } +func TestJSONQ_OnlyR(t *testing.T) { + jq := New().JSONString(jsonStr). + From("vendor.items") + result, err := jq.OnlyR("name", "price") + if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil { + t.Error("failed to match Result type") + } +} + +func TestJSONQ_OnlyR_error(t *testing.T) { + jq := New().JSONString(jsonStr). + From("invalid_path") + result, err := jq.OnlyR("name", "price") + if result != nil && err == nil { + t.Error("failed to catch error") + } +} + func TestJSONQ_First_expecting_result(t *testing.T) { jq := New().JSONString(jsonStr). From("vendor.items") @@ -794,6 +812,24 @@ func TestJSONQ_First_distinct_expecting_result(t *testing.T) { assertJSON(t, out, expected, "First with distinct & where expecting result result") } +func TestJSONQ_FirstR(t *testing.T) { + jq := New().JSONString(jsonStr). + From("vendor.items").Distinct("price").Where("price", "=", 850) + result, err := jq.FirstR() + if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil { + t.Error("failed to match Result type") + } +} + +func TestJSONQ_FirstR_error(t *testing.T) { + jq := New().JSONString(jsonStr). + From("invalid").Distinct("price").Where("price", "=", 850) + result, err := jq.FirstR() + if result != nil && err == nil { + t.Error("failed to catch error") + } +} + func TestJSONQ_Last_expecting_result(t *testing.T) { jq := New().JSONString(jsonStr). From("vendor.items") @@ -819,6 +855,24 @@ func TestJSONQ_Last_distinct_expecting_result(t *testing.T) { assertJSON(t, out, expected, "Last with distinct & where expecting result result") } +func TestJSONQ_LastR(t *testing.T) { + jq := New().JSONString(jsonStr). + From("vendor.items").Distinct("price").Where("price", "=", 850) + result, err := jq.LastR() + if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil { + t.Error("failed to match Result type") + } +} + +func TestJSONQ_LastR_error(t *testing.T) { + jq := New().JSONString(jsonStr). + From("invalid_path").Distinct("price").Where("price", "=", 850) + result, err := jq.LastR() + if result != nil && err == nil { + t.Error("failed to catch error") + } +} + func TestJSONQ_Nth_expecting_result(t *testing.T) { jq := New().JSONString(jsonStr). From("vendor.items") @@ -894,6 +948,24 @@ func TestJSONQ_Nth_distinct_expecting_result(t *testing.T) { assertJSON(t, out, expected, "Last with distinct & where expecting result result") } +func TestJSONQ_NthR(t *testing.T) { + jq := New().JSONString(jsonStr). + From("vendor.items").Distinct("price").Where("price", "=", 850) + result, err := jq.NthR(1) + if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil { + t.Error("failed to match Result type") + } +} + +func TestJSONQ_NthR_error(t *testing.T) { + jq := New().JSONString(jsonStr). + From("invalid_path").Distinct("price").Where("price", "=", 850) + result, err := jq.NthR(1) + if result != nil && err == nil { + t.Error("failed to catch error") + } +} + func TestJSONQ_Find_simple_property(t *testing.T) { jq := New().JSONString(jsonStr) out := jq.Find("name") @@ -908,6 +980,22 @@ func TestJSONQ_Find_nested_property(t *testing.T) { assertJSON(t, out, expected, "Find expecting a nested object") } +func TestJSONQ_FindR(t *testing.T) { + jq := New().JSONString(jsonStr) + result, err := jq.FindR("vendor.items.[0]") + if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil { + t.Error("failed to match Result type") + } +} + +func TestJSONQ_FindR_error(t *testing.T) { + jq := New().JSONString(jsonStr) + result, err := jq.FindR("invalid_path") + if result != nil && err == nil { + t.Error("failed to catch error") + } +} + func TestJSONQ_Pluck_expecting_list_of_float64(t *testing.T) { jq := New().JSONString(jsonStr). From("vendor.items") @@ -932,6 +1020,22 @@ func TestJSONQ_Pluck_expecting_with_distinct(t *testing.T) { assertJSON(t, out, expected, "Expecting distinct price with limit 3") } +func TestJSONQ_PluckR(t *testing.T) { + jq := New().JSONString(jsonStr).From("vendor.items") + result, err := jq.PluckR("price") + if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil { + t.Error("failed to match Result type") + } +} + +func TestJSONQ_PluckR_error(t *testing.T) { + jq := New().JSONString(jsonStr).From("invalid_path") + result, err := jq.PluckR("price") + if result != nil && err == nil { + t.Error("failed to catch error") + } +} + func TestJSONQ_Count_expecting_int_from_list(t *testing.T) { jq := New().JSONString(jsonStr). From("vendor.items") @@ -1226,6 +1330,24 @@ func TestJSONQ_Get_with_nested_invalid_property_in_Select_method_expecting_error assertJSON(t, out, expected, "nested Select method using alias") } +func TestJSONQ_GetR(t *testing.T) { + jq := New().JSONString(jsonStr). + From("vendor.items").Select("name") + result, err := jq.GetR() + if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil { + t.Error("failed to match Result type") + } +} + +func TestJSONQ_GetR_error(t *testing.T) { + jq := New().JSONString(jsonStr). + From("invalid_path") + result, err := jq.GetR() + if result != nil && err == nil { + t.Error("failed to catch error") + } +} + func TestJSONQ_Offset_method(t *testing.T) { jq := New().JSONString(jsonStr). From("vendor.items"). diff --git a/result.go b/result.go new file mode 100644 index 0000000..2655e13 --- /dev/null +++ b/result.go @@ -0,0 +1,466 @@ +package gojsonq + +import ( + "fmt" + "reflect" + "strings" + "time" +) + +const errMessage = "gojsonq: wrong method call for %v" + +// NewResult return an instance of Result +func NewResult(v interface{}) *Result { + return &Result{value: v} +} + +// Result represent custom type +type Result struct { + value interface{} +} + +// Nil check the query has result or not +func (r *Result) Nil() bool { + return r.value == nil +} + +// Bool assert the result to boolean value +func (r *Result) Bool() (bool, error) { + switch v := r.value.(type) { + case bool: + return v, nil + default: + return false, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Time assert the result to time.Time +func (r *Result) Time(layout string) (time.Time, error) { + switch v := r.value.(type) { + case string: + return time.Parse(layout, v) + default: + return time.Time{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Duration assert the result to time.Duration +func (r *Result) Duration() (time.Duration, error) { + switch v := r.value.(type) { + case float64: + return time.Duration(v), nil + case string: + if strings.ContainsAny(v, "nsuµmh") { + return time.ParseDuration(v) + } + return time.ParseDuration(v + "ns") + default: + return time.Duration(0), fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// String assert the result to String +func (r *Result) String() (string, error) { + switch v := r.value.(type) { + case string: + return v, nil + default: + return "", fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Int assert the result to int +func (r *Result) Int() (int, error) { + switch v := r.value.(type) { + case float64: + return int(v), nil + default: + return 0, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Int8 assert the result to int8 +func (r *Result) Int8() (int8, error) { + switch v := r.value.(type) { + case float64: + return int8(v), nil + default: + return 0, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Int16 assert the result to int16 +func (r *Result) Int16() (int16, error) { + switch v := r.value.(type) { + case float64: + return int16(v), nil + default: + return 0, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Int32 assert the result to int32 +func (r *Result) Int32() (int32, error) { + switch v := r.value.(type) { + case float64: + return int32(v), nil + default: + return 0, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Int64 assert the result to int64 +func (r *Result) Int64() (int64, error) { + switch v := r.value.(type) { + case float64: + return int64(v), nil + default: + return 0, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Uint assert the result to uint +func (r *Result) Uint() (uint, error) { + switch v := r.value.(type) { + case float64: + return uint(v), nil + default: + return 0, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Uint8 assert the result to uint8 +func (r *Result) Uint8() (uint8, error) { + switch v := r.value.(type) { + case float64: + return uint8(v), nil + default: + return 0, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Uint16 assert the result to uint16 +func (r *Result) Uint16() (uint16, error) { + switch v := r.value.(type) { + case float64: + return uint16(v), nil + default: + return 0, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Uint32 assert the result to uint32 +func (r *Result) Uint32() (uint32, error) { + switch v := r.value.(type) { + case float64: + return uint32(v), nil + default: + return 0, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Uint64 assert the result to uint64 +func (r *Result) Uint64() (uint64, error) { + switch v := r.value.(type) { + case float64: + return uint64(v), nil + default: + return 0, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Float32 assert the result to float32 +func (r *Result) Float32() (float32, error) { + switch v := r.value.(type) { + case float64: + return float32(v), nil + default: + return 0, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Float64 assert the result to 64 +func (r *Result) Float64() (float64, error) { + switch v := r.value.(type) { + case float64: + return v, nil + default: + return 0, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// === slcie === + +// BoolSlice assert the result to []bool +func (r *Result) BoolSlice() ([]bool, error) { + switch v := r.value.(type) { + case []interface{}: + bb := []bool{} + for _, si := range v { + if s, ok := si.(bool); ok { + bb = append(bb, s) + } + } + return bb, nil + default: + return []bool{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// TimeSlice assert the result to []time.Time +func (r *Result) TimeSlice(layout string) ([]time.Time, error) { + switch v := r.value.(type) { + case []interface{}: + tt := []time.Time{} + for _, si := range v { + if s, ok := si.(string); ok { + ts, err := time.Parse(layout, s) + if err != nil { + return tt, err + } + tt = append(tt, ts) + } + } + return tt, nil + default: + return []time.Time{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// DurationSlice assert the result to []time.Duration +func (r *Result) DurationSlice() ([]time.Duration, error) { + switch v := r.value.(type) { + case []interface{}: + dd := []time.Duration{} + for _, si := range v { + if s, ok := si.(string); ok { + var d time.Duration + var err error + if strings.ContainsAny(s, "nsuµmh") { + d, err = time.ParseDuration(s) + } else { + d, err = time.ParseDuration(s + "ns") + } + if err != nil { + return dd, err + } + dd = append(dd, d) + } + + if v, ok := si.(float64); ok { + dd = append(dd, time.Duration(v)) + } + } + return dd, nil + default: + return []time.Duration{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// StringSlice assert the result to []string +func (r *Result) StringSlice() ([]string, error) { + switch v := r.value.(type) { + case []interface{}: + ss := []string{} + for _, si := range v { + if s, ok := si.(string); ok { + ss = append(ss, s) + } + } + return ss, nil + default: + return []string{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// IntSlice assert the result to []int +func (r *Result) IntSlice() ([]int, error) { + switch v := r.value.(type) { + case []interface{}: + ii := []int{} + for _, si := range v { + if s, ok := si.(float64); ok { + ii = append(ii, int(s)) + } + } + return ii, nil + default: + return []int{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Int8Slice assert the result to []int8 +func (r *Result) Int8Slice() ([]int8, error) { + switch v := r.value.(type) { + case []interface{}: + ii := []int8{} + for _, si := range v { + if s, ok := si.(float64); ok { + ii = append(ii, int8(s)) + } + } + return ii, nil + default: + return []int8{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Int16Slice assert the result to []int16 +func (r *Result) Int16Slice() ([]int16, error) { + switch v := r.value.(type) { + case []interface{}: + ii := []int16{} + for _, si := range v { + if s, ok := si.(float64); ok { + ii = append(ii, int16(s)) + } + } + return ii, nil + default: + return []int16{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Int32Slice assert the result to []int32 +func (r *Result) Int32Slice() ([]int32, error) { + switch v := r.value.(type) { + case []interface{}: + ii := []int32{} + for _, si := range v { + if s, ok := si.(float64); ok { + ii = append(ii, int32(s)) + } + } + return ii, nil + default: + return []int32{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Int64Slice assert the result to []int64 +func (r *Result) Int64Slice() ([]int64, error) { + switch v := r.value.(type) { + case []interface{}: + ii := []int64{} + for _, si := range v { + if s, ok := si.(float64); ok { + ii = append(ii, int64(s)) + } + } + return ii, nil + default: + return []int64{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// UintSlice assert the result to []uint +func (r *Result) UintSlice() ([]uint, error) { + switch v := r.value.(type) { + case []interface{}: + uu := []uint{} + for _, si := range v { + if s, ok := si.(float64); ok { + uu = append(uu, uint(s)) + } + } + return uu, nil + default: + return []uint{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Uint8Slice assert the result to []uint8 +func (r *Result) Uint8Slice() ([]uint8, error) { + switch v := r.value.(type) { + case []interface{}: + uu := []uint8{} + for _, si := range v { + if s, ok := si.(float64); ok { + uu = append(uu, uint8(s)) + } + } + return uu, nil + default: + return []uint8{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Uint16Slice assert the result to []uint16 +func (r *Result) Uint16Slice() ([]uint16, error) { + switch v := r.value.(type) { + case []interface{}: + uu := []uint16{} + for _, si := range v { + if s, ok := si.(float64); ok { + uu = append(uu, uint16(s)) + } + } + return uu, nil + default: + return []uint16{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Uint32Slice assert the result to []uint32 +func (r *Result) Uint32Slice() ([]uint32, error) { + switch v := r.value.(type) { + case []interface{}: + uu := []uint32{} + for _, si := range v { + if s, ok := si.(float64); ok { + uu = append(uu, uint32(s)) + } + } + return uu, nil + default: + return []uint32{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Uint64Slice assert the result to []uint64 +func (r *Result) Uint64Slice() ([]uint64, error) { + switch v := r.value.(type) { + case []interface{}: + uu := []uint64{} + for _, si := range v { + if s, ok := si.(float64); ok { + uu = append(uu, uint64(s)) + } + } + return uu, nil + default: + return []uint64{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Float32Slice assert the result to []float32 +func (r *Result) Float32Slice() ([]float32, error) { + switch v := r.value.(type) { + case []interface{}: + ff := []float32{} + for _, si := range v { + if s, ok := si.(float64); ok { + ff = append(ff, float32(s)) + } + } + return ff, nil + default: + return []float32{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} + +// Float64Slice assert the result to []float64 +func (r *Result) Float64Slice() ([]float64, error) { + switch v := r.value.(type) { + case []interface{}: + ff := []float64{} + for _, si := range v { + if s, ok := si.(float64); ok { + ff = append(ff, s) + } + } + return ff, nil + default: + return []float64{}, fmt.Errorf(errMessage, reflect.ValueOf(r.value).Kind()) + } +} diff --git a/result_test.go b/result_test.go new file mode 100644 index 0000000..658131e --- /dev/null +++ b/result_test.go @@ -0,0 +1,789 @@ +package gojsonq + +import ( + "reflect" + "testing" + "time" +) + +func TestNewResult(t *testing.T) { + result := NewResult("gojsonq") + if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" { + t.Error("failed to match gojsonq.Result type") + } +} + +func TestNil(t *testing.T) { + result := NewResult(nil) + if result.Nil() == false { + t.Error("failed to check Nil") + } +} +func TestBool(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect bool + errExpect bool + }{ + {tag: "bool value as expected", value: true, valExpect: true, errExpect: false}, + {tag: "invalid bool, error expected", value: 123, valExpect: false, errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).Bool() + if err != nil && !tc.errExpect { + t.Error("bool:", err) + } + if v != tc.valExpect && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestTime(t *testing.T) { + layout := "2006-01-02T15:04:05.000Z" + str := "2014-11-12T11:45:26.371Z" + tm, err := time.Parse(layout, str) + if err != nil { + t.Error("failed to parse time:", err) + } + testCases := []struct { + tag string + value interface{} + valExpect time.Time + errExpect bool + }{ + {tag: "time value as expected", value: "2014-11-12T11:45:26.371Z", valExpect: tm, errExpect: false}, + {tag: "invalid time, error expected", value: "2014-11-12", valExpect: time.Time{}, errExpect: true}, + {tag: "invalid time, error expected", value: 12322, valExpect: time.Time{}, errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).Time(layout) + if err != nil && !tc.errExpect { + t.Error("time:", err) + } + if !reflect.DeepEqual(v, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestDuration(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect time.Duration + errExpect bool + }{ + {tag: "duration value as expected", value: "10s", valExpect: time.Duration(10 * time.Second), errExpect: false}, + {tag: "duration value as expected", value: "10m", valExpect: time.Duration(10 * time.Minute), errExpect: false}, + {tag: "duration value as expected", value: float64(10), valExpect: time.Duration(10 * time.Nanosecond), errExpect: false}, // go decode number to float64 + {tag: "invalid duration, error expected", value: "1", valExpect: time.Duration(0), errExpect: true}, + {tag: "invalid duration, error expected", value: 1, valExpect: time.Duration(0), errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).Duration() + if err != nil && !tc.errExpect { + t.Error("duration:", err) + } + if !reflect.DeepEqual(v, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestString(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect string + errExpect bool + }{ + {tag: "string value as expected", value: "hello", valExpect: "hello", errExpect: false}, + {tag: "invalid string, error expected", value: 123, valExpect: "", errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).String() + if err != nil && !tc.errExpect { + t.Error("string: ", err) + } + if v != tc.valExpect && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestInt(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect int + errExpect bool + }{ + {tag: "int value as expected", value: 123.8, valExpect: 123, errExpect: false}, + {tag: "int value as expected", value: 12.3, valExpect: 12, errExpect: false}, + {tag: "invalid int, error expected", value: "123", valExpect: 0, errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).Int() + if err != nil && !tc.errExpect { + t.Error("int:", err) + } + if v != tc.valExpect && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestInt8(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect int8 + errExpect bool + }{ + {tag: "int8 value as expected", value: 123.8, valExpect: int8(123), errExpect: false}, + {tag: "int8 value as expected", value: 12.3, valExpect: int8(12), errExpect: false}, + {tag: "invalid int8, error expected", value: "123", valExpect: 0, errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).Int8() + if err != nil && !tc.errExpect { + t.Error("int8:", err) + } + if v != tc.valExpect && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestInt16(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect int16 + errExpect bool + }{ + {tag: "int16 value as expected", value: 123.8, valExpect: int16(123), errExpect: false}, + {tag: "int16 value as expected", value: 12.3, valExpect: int16(12), errExpect: false}, + {tag: "invalid int16, error expected", value: "123", valExpect: 0, errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).Int16() + if err != nil && !tc.errExpect { + t.Error("int16:", err) + } + if v != tc.valExpect && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestInt32(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect int32 + errExpect bool + }{ + {tag: "int32 value as expected", value: 123.8, valExpect: int32(123), errExpect: false}, + {tag: "int32 value as expected", value: 12.3, valExpect: int32(12), errExpect: false}, + {tag: "invalid int32, error expected", value: "123", valExpect: 0, errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).Int32() + if err != nil && !tc.errExpect { + t.Error("int32:", err) + } + if v != tc.valExpect && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestInt64(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect int64 + errExpect bool + }{ + {tag: "int64 value as expected", value: 123.8, valExpect: int64(123), errExpect: false}, + {tag: "int64 value as expected", value: 12.3, valExpect: int64(12), errExpect: false}, + {tag: "invalid int64, error expected", value: "123", valExpect: 0, errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).Int64() + if err != nil && !tc.errExpect { + t.Error("int64:", err) + } + if v != tc.valExpect && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestUint(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect uint + errExpect bool + }{ + {tag: "uint value as expected", value: 123.8, valExpect: uint(123), errExpect: false}, + {tag: "uint value as expected", value: 12.3, valExpect: uint(12), errExpect: false}, + {tag: "invalid uint, error expected", value: "123", valExpect: 0, errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).Uint() + if err != nil && !tc.errExpect { + t.Error("uint:", err) + } + if v != tc.valExpect && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestUint8(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect uint8 + errExpect bool + }{ + {tag: "uint8 value as expected", value: 123.8, valExpect: uint8(123), errExpect: false}, + {tag: "uint8 value as expected", value: 12.3, valExpect: uint8(12), errExpect: false}, + {tag: "invalid uint8, error expected", value: "123", valExpect: 0, errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).Uint8() + if err != nil && !tc.errExpect { + t.Error("uint8:", err) + } + if v != tc.valExpect && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestUint16(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect uint16 + errExpect bool + }{ + {tag: "uint16 value as expected", value: 123.8, valExpect: uint16(123), errExpect: false}, + {tag: "uint16 value as expected", value: 12.3, valExpect: uint16(12), errExpect: false}, + {tag: "invalid uint16, error expected", value: "123", valExpect: 0, errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).Uint16() + if err != nil && !tc.errExpect { + t.Error("uint16:", err) + } + if v != tc.valExpect && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestUint32(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect uint32 + errExpect bool + }{ + {tag: "uint32 value as expected", value: 123.8, valExpect: uint32(123), errExpect: false}, + {tag: "uint32 value as expected", value: 12.3, valExpect: uint32(12), errExpect: false}, + {tag: "invalid uint32, error expected", value: "123", valExpect: 0, errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).Uint32() + if err != nil && !tc.errExpect { + t.Error("uint32:", err) + } + if v != tc.valExpect && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestUint64(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect uint64 + errExpect bool + }{ + {tag: "uint64 value as expected", value: 123.8, valExpect: uint64(123), errExpect: false}, + {tag: "uint64 value as expected", value: 12.3, valExpect: uint64(12), errExpect: false}, + {tag: "invalid uint64, error expected", value: "123", valExpect: 0, errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).Uint64() + if err != nil && !tc.errExpect { + t.Error("uint64:", err) + } + if v != tc.valExpect && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestFloat32(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect float32 + errExpect bool + }{ + {tag: "float32 value as expected", value: 123.8, valExpect: float32(123.8), errExpect: false}, + {tag: "float32 value as expected", value: 12.3, valExpect: float32(12.3), errExpect: false}, + {tag: "invalid float32, error expected", value: "123", valExpect: 0, errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).Float32() + if err != nil && !tc.errExpect { + t.Error("float32:", err) + } + if v != tc.valExpect && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestFloat64(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect float64 + errExpect bool + }{ + {tag: "float64 value as expected", value: 123.8, valExpect: float64(123.8), errExpect: false}, + {tag: "float64 value as expected", value: 12.3, valExpect: float64(12.3), errExpect: true}, + {tag: "invalid float64, error expected", value: "123", valExpect: 0, errExpect: true}, + } + + for _, tc := range testCases { + v, err := NewResult(tc.value).Float64() + if err != nil && !tc.errExpect { + t.Error("float64:", err) + } + if v != tc.valExpect && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v) + } + } +} + +func TestBoolSlice(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect []bool + errExpect bool + }{ + {tag: "boolSlice value as expected", value: []interface{}{true, false}, valExpect: []bool{true, false}, errExpect: false}, + {tag: "boolSlice value as expected", value: []interface{}{false, true, true}, valExpect: []bool{false, true, true}, errExpect: false}, + {tag: "invalid boolSlice, error expected", value: []interface{}{1, 3}, valExpect: []bool{}, errExpect: false}, + {tag: "invalid boolSlice, error expected", value: []int{1, 3}, valExpect: []bool{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).BoolSlice() + if err != nil && !tc.errExpect { + t.Error("boolSlice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } +} + +func TestTimelice(t *testing.T) { + layout := "2006-01-02T15:04:05.000Z" + t1, err1 := time.Parse(layout, "2014-11-12T11:45:26.371Z") + if err1 != nil { + t.Error("failed to parse time1:", err1) + } + t2, err2 := time.Parse(layout, "2019-11-12T11:45:26.371Z") + if err2 != nil { + t.Error("failed to parse time2:", err2) + } + testCases := []struct { + tag string + value interface{} + timeLayout string + valExpect []time.Time + errExpect bool + }{ + {tag: "timeSlice value as expected", value: []interface{}{"2014-11-12T11:45:26.371Z", "2019-11-12T11:45:26.371Z"}, timeLayout: layout, valExpect: []time.Time{t1, t2}, errExpect: false}, + {tag: "invalid timeSlice layout, error expected", value: []interface{}{"2014-11-12T11:45:26.371Z", "2019-11-12T11:45:26.371Z"}, timeLayout: "invalid layout", valExpect: []time.Time{}, errExpect: true}, + {tag: "invalid timeSlice, error expected", value: []int{1, 3}, timeLayout: layout, valExpect: []time.Time{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).TimeSlice(tc.timeLayout) + if err != nil && !tc.errExpect { + t.Error("timeSlice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } + +} + +func TestDurationlice(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect []time.Duration + errExpect bool + }{ + {tag: "durationSlice value as expected", value: []interface{}{"1s", "1m"}, valExpect: []time.Duration{1 * time.Second, 1 * time.Minute}, errExpect: false}, + {tag: "durationSlice value as expected", value: []interface{}{"1", "2"}, valExpect: []time.Duration{1 * time.Nanosecond, 2 * time.Nanosecond}, errExpect: false}, + {tag: "durationSlice value as expected", value: []interface{}{float64(2), float64(3)}, valExpect: []time.Duration{2 * time.Nanosecond, 3 * time.Nanosecond}, errExpect: false}, + {tag: "invalid durationSlice, error expected", value: []interface{}{"invalid duration 1", "invalid duration 2"}, valExpect: []time.Duration{}, errExpect: true}, + {tag: "invalid durationSlice, error expected", value: []float64{3, 5}, valExpect: []time.Duration{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).DurationSlice() + if err != nil && !tc.errExpect { + t.Error("durationSlice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } +} + +func TestStringSlice(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect []string + errExpect bool + }{ + {tag: "stringSlice value as expected", value: []interface{}{"hello", "world"}, valExpect: []string{"hello", "world"}, errExpect: false}, + {tag: "stringSlice value as expected", value: []interface{}{"tom", "jerry"}, valExpect: []string{"tom", "jerry"}, errExpect: false}, + {tag: "invalid stringSlice, error expected", value: []interface{}{1, 3}, valExpect: []string{}, errExpect: false}, + {tag: "invalid stringSlice, error expected", value: []int{1, 3}, valExpect: []string{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).StringSlice() + if err != nil && !tc.errExpect { + t.Error("stringSlice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } +} + +func TestIntSlice(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect []int + errExpect bool + }{ + {tag: "intSlice value as expected", value: []interface{}{132.1, 12.99}, valExpect: []int{132, 12}, errExpect: false}, + {tag: "intSlice value as expected", value: []interface{}{float64(131), float64(12)}, valExpect: []int{131, 12}, errExpect: false}, // as golang decode number to float64 + {tag: "invalid intSlice, error expected", value: []interface{}{1, 3}, valExpect: []int{}, errExpect: false}, + {tag: "invalid intSlice, error expected", value: []int{1, 3}, valExpect: []int{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).IntSlice() + if err != nil && !tc.errExpect { + t.Error("intSlice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } +} + +func TestInt8Slice(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect []int8 + errExpect bool + }{ + {tag: "int8Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []int8{3, 12}, errExpect: false}, + {tag: "int8Slice value as expected", value: []interface{}{float64(11), float64(12)}, valExpect: []int8{11, 12}, errExpect: false}, // as golang decode number to float64 + {tag: "invalid int8Slice, error expected", value: []interface{}{1, 3}, valExpect: []int8{}, errExpect: false}, + {tag: "invalid int8Slice, error expected", value: []int{1, 3}, valExpect: []int8{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).Int8Slice() + if err != nil && !tc.errExpect { + t.Error("int8Slice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } +} + +func TestInt16Slice(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect []int16 + errExpect bool + }{ + {tag: "int16Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []int16{3, 12}, errExpect: false}, + {tag: "int16Slice value as expected", value: []interface{}{float64(11), float64(12)}, valExpect: []int16{11, 12}, errExpect: false}, // as golang decode number to float64 + {tag: "invalid int16Slice, error expected", value: []interface{}{1, 3}, valExpect: []int16{}, errExpect: false}, + {tag: "invalid int16Slice, error expected", value: []int{1, 3}, valExpect: []int16{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).Int16Slice() + if err != nil && !tc.errExpect { + t.Error("int16Slice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } +} + +func TestInt32Slice(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect []int32 + errExpect bool + }{ + {tag: "int32Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []int32{3, 12}, errExpect: false}, + {tag: "int32Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []int32{131, 132}, errExpect: false}, // as golang decode number to float64 + {tag: "invalid int32Slice, error expected", value: []interface{}{1, 3}, valExpect: []int32{}, errExpect: false}, + {tag: "invalid int32Slice, error expected", value: []int{1, 3}, valExpect: []int32{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).Int32Slice() + if err != nil && !tc.errExpect { + t.Error("int32Slice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } +} + +func TestInt64Slice(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect []int64 + errExpect bool + }{ + {tag: "int64Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []int64{3, 12}, errExpect: false}, + {tag: "int64Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []int64{131, 132}, errExpect: false}, // as golang decode number to float64 + {tag: "invalid int64Slice, error expected", value: []interface{}{1, 3}, valExpect: []int64{}, errExpect: false}, + {tag: "invalid int64Slice, error expected", value: []int{1, 3}, valExpect: []int64{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).Int64Slice() + if err != nil && !tc.errExpect { + t.Error("int64Slice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } +} + +func TestUintSlice(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect []uint + errExpect bool + }{ + {tag: "uintSlice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []uint{3, 12}, errExpect: false}, + {tag: "uintSlice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []uint{131, 132}, errExpect: false}, // as golang decode number to float64 + {tag: "invalid uintSlice, error expected", value: []interface{}{1, 3}, valExpect: []uint{}, errExpect: false}, + {tag: "invalid uintSlice, error expected", value: []int{1, 3}, valExpect: []uint{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).UintSlice() + if err != nil && !tc.errExpect { + t.Error("uintSlice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } +} + +func TestUint8Slice(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect []uint8 + errExpect bool + }{ + {tag: "uint8Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []uint8{3, 12}, errExpect: false}, + {tag: "uint8Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []uint8{131, 132}, errExpect: false}, // as golang decode number to float64 + {tag: "invalid uint8Slice, error expected", value: []interface{}{1, 3}, valExpect: []uint8{}, errExpect: false}, + {tag: "invalid uint8Slice, error expected", value: []int{1, 3}, valExpect: []uint8{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).Uint8Slice() + if err != nil && !tc.errExpect { + t.Error("uint8Slice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } +} + +func TestUint16Slice(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect []uint16 + errExpect bool + }{ + {tag: "uint16Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []uint16{3, 12}, errExpect: false}, + {tag: "uint16Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []uint16{131, 132}, errExpect: false}, // as golang decode number to float64 + {tag: "invalid uint16Slice, error expected", value: []interface{}{1, 3}, valExpect: []uint16{}, errExpect: false}, + {tag: "invalid uint16Slice, error expected", value: []int{1, 3}, valExpect: []uint16{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).Uint16Slice() + if err != nil && !tc.errExpect { + t.Error("uint16Slice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } +} + +func TestUint32Slice(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect []uint32 + errExpect bool + }{ + {tag: "uint32Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []uint32{3, 12}, errExpect: false}, + {tag: "uint32Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []uint32{131, 132}, errExpect: false}, // as golang decode number to float64 + {tag: "invalid uint32Slice, error expected", value: []interface{}{1, 3}, valExpect: []uint32{}, errExpect: false}, + {tag: "invalid uint32Slice, error expected", value: []int{1, 3}, valExpect: []uint32{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).Uint32Slice() + if err != nil && !tc.errExpect { + t.Error("uint32Slice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } +} + +func TestUint64Slice(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect []uint64 + errExpect bool + }{ + {tag: "uint64Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []uint64{3, 12}, errExpect: false}, + {tag: "uint64Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []uint64{131, 132}, errExpect: false}, // as golang decode number to float64 + {tag: "invalid uint64Slice, error expected", value: []interface{}{1, 3}, valExpect: []uint64{}, errExpect: false}, + {tag: "invalid uint64Slice, error expected", value: []int{1, 3}, valExpect: []uint64{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).Uint64Slice() + if err != nil && !tc.errExpect { + t.Error("uint64Slice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } +} + +func TestFloat32Slice(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect []float32 + errExpect bool + }{ + {tag: "float32Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []float32{3.1, 12.99}, errExpect: false}, + {tag: "float32Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []float32{131, 132}, errExpect: false}, // as golang decode number to float64 + {tag: "invalid float32Slice, error expected", value: []interface{}{1, 3}, valExpect: []float32{}, errExpect: false}, + {tag: "invalid float32Slice, error expected", value: []int{1, 3}, valExpect: []float32{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).Float32Slice() + if err != nil && !tc.errExpect { + t.Error("float32Slice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } +} + +func TestFloat64Slice(t *testing.T) { + testCases := []struct { + tag string + value interface{} + valExpect []float64 + errExpect bool + }{ + {tag: "float64Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []float64{3.1, 12.99}, errExpect: false}, + {tag: "float64Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []float64{131, 132}, errExpect: false}, // as golang decode number to float64 + {tag: "invalid float64Slice, error expected", value: []interface{}{1, 3}, valExpect: []float64{}, errExpect: false}, + {tag: "invalid float64Slice, error expected", value: []int{1, 3}, valExpect: []float64{}, errExpect: true}, + } + + for _, tc := range testCases { + vv, err := NewResult(tc.value).Float64Slice() + if err != nil && !tc.errExpect { + t.Error("float64Slice:", err) + } + if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect { + t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv) + } + } +}