Skip to content

Commit

Permalink
Add support for Result. Resolves #61
Browse files Browse the repository at this point in the history
* Add Result type and different static type method

* Add FindR, GetR, FirstR, LastR, NthR, OnlyR, PluckR methods

* Rename Value -> value

* Add Nil method; Update doc block

* Update redundant error message as const

* Update FindR, GetR, FirstR, LastR, NthR, PluckR, OnlyR methods

* Update error message
  • Loading branch information
thedevsaddam committed Sep 23, 2019
1 parent ccd352d commit ab3beb6
Show file tree
Hide file tree
Showing 4 changed files with 1,440 additions and 0 deletions.
63 changes: 63 additions & 0 deletions jsonq.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
122 changes: 122 additions & 0 deletions jsonq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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").
Expand Down
Loading

0 comments on commit ab3beb6

Please sign in to comment.