Skip to content

Commit ab3beb6

Browse files
authored
Add support for Result. Resolves #61
* 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
1 parent ccd352d commit ab3beb6

File tree

4 files changed

+1440
-0
lines changed

4 files changed

+1440
-0
lines changed

jsonq.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,15 @@ func (j *JSONQ) Only(properties ...string) interface{} {
505505
return j.prepare().only(properties...)
506506
}
507507

508+
// OnlyR collects the properties from a list of object and return as Result instance
509+
func (j *JSONQ) OnlyR(properties ...string) (*Result, error) {
510+
v := j.Only(properties...)
511+
if err := j.Error(); err != nil {
512+
return nil, err
513+
}
514+
return NewResult(v), nil
515+
}
516+
508517
// Pluck build an array of vlaues form a property of a list of objects
509518
func (j *JSONQ) Pluck(property string) interface{} {
510519
j.prepare()
@@ -527,6 +536,15 @@ func (j *JSONQ) Pluck(property string) interface{} {
527536
return result
528537
}
529538

539+
// PluckR build an array of vlaues form a property of a list of objects and return as Result instance
540+
func (j *JSONQ) PluckR(property string) (*Result, error) {
541+
v := j.Pluck(property)
542+
if err := j.Error(); err != nil {
543+
return nil, err
544+
}
545+
return NewResult(v), nil
546+
}
547+
530548
// reset resets the current state of JSONQ instance
531549
func (j *JSONQ) reset() *JSONQ {
532550
j.raw = nil
@@ -558,6 +576,15 @@ func (j *JSONQ) Get() interface{} {
558576
return j.jsonContent
559577
}
560578

579+
// GetR return the query results as Result instance
580+
func (j *JSONQ) GetR() (*Result, error) {
581+
v := j.Get()
582+
if err := j.Error(); err != nil {
583+
return nil, err
584+
}
585+
return NewResult(v), nil
586+
}
587+
561588
// First returns the first element of a list
562589
func (j *JSONQ) First() interface{} {
563590
j.prepare()
@@ -569,6 +596,15 @@ func (j *JSONQ) First() interface{} {
569596
return empty
570597
}
571598

599+
// FirstR returns the first element of a list as Result instance
600+
func (j *JSONQ) FirstR() (*Result, error) {
601+
v := j.First()
602+
if err := j.Error(); err != nil {
603+
return nil, err
604+
}
605+
return NewResult(v), nil
606+
}
607+
572608
// Last returns the last element of a list
573609
func (j *JSONQ) Last() interface{} {
574610
j.prepare()
@@ -580,6 +616,15 @@ func (j *JSONQ) Last() interface{} {
580616
return empty
581617
}
582618

619+
// LastR returns the last element of a list as Result instance
620+
func (j *JSONQ) LastR() (*Result, error) {
621+
v := j.Last()
622+
if err := j.Error(); err != nil {
623+
return nil, err
624+
}
625+
return NewResult(v), nil
626+
}
627+
583628
// Nth returns the nth element of a list
584629
func (j *JSONQ) Nth(index int) interface{} {
585630
if index == 0 {
@@ -606,11 +651,29 @@ func (j *JSONQ) Nth(index int) interface{} {
606651
return empty
607652
}
608653

654+
// NthR returns the nth element of a list as Result instance
655+
func (j *JSONQ) NthR(index int) (*Result, error) {
656+
v := j.Nth(index)
657+
if err := j.Error(); err != nil {
658+
return nil, err
659+
}
660+
return NewResult(v), nil
661+
}
662+
609663
// Find returns the result of a exact matching path
610664
func (j *JSONQ) Find(path string) interface{} {
611665
return j.From(path).Get()
612666
}
613667

668+
// FindR returns the result as Result instance from the exact matching path
669+
func (j *JSONQ) FindR(path string) (*Result, error) {
670+
v := j.Find(path)
671+
if err := j.Error(); err != nil {
672+
return nil, err
673+
}
674+
return NewResult(v), nil
675+
}
676+
614677
// Count returns the number of total items.
615678
// This could be a length of list/array/map
616679
func (j *JSONQ) Count() int {

jsonq_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,24 @@ func TestJSONQ_Only_with_distinct(t *testing.T) {
769769
assertJSON(t, out, expected)
770770
}
771771

772+
func TestJSONQ_OnlyR(t *testing.T) {
773+
jq := New().JSONString(jsonStr).
774+
From("vendor.items")
775+
result, err := jq.OnlyR("name", "price")
776+
if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
777+
t.Error("failed to match Result type")
778+
}
779+
}
780+
781+
func TestJSONQ_OnlyR_error(t *testing.T) {
782+
jq := New().JSONString(jsonStr).
783+
From("invalid_path")
784+
result, err := jq.OnlyR("name", "price")
785+
if result != nil && err == nil {
786+
t.Error("failed to catch error")
787+
}
788+
}
789+
772790
func TestJSONQ_First_expecting_result(t *testing.T) {
773791
jq := New().JSONString(jsonStr).
774792
From("vendor.items")
@@ -794,6 +812,24 @@ func TestJSONQ_First_distinct_expecting_result(t *testing.T) {
794812
assertJSON(t, out, expected, "First with distinct & where expecting result result")
795813
}
796814

815+
func TestJSONQ_FirstR(t *testing.T) {
816+
jq := New().JSONString(jsonStr).
817+
From("vendor.items").Distinct("price").Where("price", "=", 850)
818+
result, err := jq.FirstR()
819+
if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
820+
t.Error("failed to match Result type")
821+
}
822+
}
823+
824+
func TestJSONQ_FirstR_error(t *testing.T) {
825+
jq := New().JSONString(jsonStr).
826+
From("invalid").Distinct("price").Where("price", "=", 850)
827+
result, err := jq.FirstR()
828+
if result != nil && err == nil {
829+
t.Error("failed to catch error")
830+
}
831+
}
832+
797833
func TestJSONQ_Last_expecting_result(t *testing.T) {
798834
jq := New().JSONString(jsonStr).
799835
From("vendor.items")
@@ -819,6 +855,24 @@ func TestJSONQ_Last_distinct_expecting_result(t *testing.T) {
819855
assertJSON(t, out, expected, "Last with distinct & where expecting result result")
820856
}
821857

858+
func TestJSONQ_LastR(t *testing.T) {
859+
jq := New().JSONString(jsonStr).
860+
From("vendor.items").Distinct("price").Where("price", "=", 850)
861+
result, err := jq.LastR()
862+
if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
863+
t.Error("failed to match Result type")
864+
}
865+
}
866+
867+
func TestJSONQ_LastR_error(t *testing.T) {
868+
jq := New().JSONString(jsonStr).
869+
From("invalid_path").Distinct("price").Where("price", "=", 850)
870+
result, err := jq.LastR()
871+
if result != nil && err == nil {
872+
t.Error("failed to catch error")
873+
}
874+
}
875+
822876
func TestJSONQ_Nth_expecting_result(t *testing.T) {
823877
jq := New().JSONString(jsonStr).
824878
From("vendor.items")
@@ -894,6 +948,24 @@ func TestJSONQ_Nth_distinct_expecting_result(t *testing.T) {
894948
assertJSON(t, out, expected, "Last with distinct & where expecting result result")
895949
}
896950

951+
func TestJSONQ_NthR(t *testing.T) {
952+
jq := New().JSONString(jsonStr).
953+
From("vendor.items").Distinct("price").Where("price", "=", 850)
954+
result, err := jq.NthR(1)
955+
if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
956+
t.Error("failed to match Result type")
957+
}
958+
}
959+
960+
func TestJSONQ_NthR_error(t *testing.T) {
961+
jq := New().JSONString(jsonStr).
962+
From("invalid_path").Distinct("price").Where("price", "=", 850)
963+
result, err := jq.NthR(1)
964+
if result != nil && err == nil {
965+
t.Error("failed to catch error")
966+
}
967+
}
968+
897969
func TestJSONQ_Find_simple_property(t *testing.T) {
898970
jq := New().JSONString(jsonStr)
899971
out := jq.Find("name")
@@ -908,6 +980,22 @@ func TestJSONQ_Find_nested_property(t *testing.T) {
908980
assertJSON(t, out, expected, "Find expecting a nested object")
909981
}
910982

983+
func TestJSONQ_FindR(t *testing.T) {
984+
jq := New().JSONString(jsonStr)
985+
result, err := jq.FindR("vendor.items.[0]")
986+
if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
987+
t.Error("failed to match Result type")
988+
}
989+
}
990+
991+
func TestJSONQ_FindR_error(t *testing.T) {
992+
jq := New().JSONString(jsonStr)
993+
result, err := jq.FindR("invalid_path")
994+
if result != nil && err == nil {
995+
t.Error("failed to catch error")
996+
}
997+
}
998+
911999
func TestJSONQ_Pluck_expecting_list_of_float64(t *testing.T) {
9121000
jq := New().JSONString(jsonStr).
9131001
From("vendor.items")
@@ -932,6 +1020,22 @@ func TestJSONQ_Pluck_expecting_with_distinct(t *testing.T) {
9321020
assertJSON(t, out, expected, "Expecting distinct price with limit 3")
9331021
}
9341022

1023+
func TestJSONQ_PluckR(t *testing.T) {
1024+
jq := New().JSONString(jsonStr).From("vendor.items")
1025+
result, err := jq.PluckR("price")
1026+
if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
1027+
t.Error("failed to match Result type")
1028+
}
1029+
}
1030+
1031+
func TestJSONQ_PluckR_error(t *testing.T) {
1032+
jq := New().JSONString(jsonStr).From("invalid_path")
1033+
result, err := jq.PluckR("price")
1034+
if result != nil && err == nil {
1035+
t.Error("failed to catch error")
1036+
}
1037+
}
1038+
9351039
func TestJSONQ_Count_expecting_int_from_list(t *testing.T) {
9361040
jq := New().JSONString(jsonStr).
9371041
From("vendor.items")
@@ -1226,6 +1330,24 @@ func TestJSONQ_Get_with_nested_invalid_property_in_Select_method_expecting_error
12261330
assertJSON(t, out, expected, "nested Select method using alias")
12271331
}
12281332

1333+
func TestJSONQ_GetR(t *testing.T) {
1334+
jq := New().JSONString(jsonStr).
1335+
From("vendor.items").Select("name")
1336+
result, err := jq.GetR()
1337+
if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
1338+
t.Error("failed to match Result type")
1339+
}
1340+
}
1341+
1342+
func TestJSONQ_GetR_error(t *testing.T) {
1343+
jq := New().JSONString(jsonStr).
1344+
From("invalid_path")
1345+
result, err := jq.GetR()
1346+
if result != nil && err == nil {
1347+
t.Error("failed to catch error")
1348+
}
1349+
}
1350+
12291351
func TestJSONQ_Offset_method(t *testing.T) {
12301352
jq := New().JSONString(jsonStr).
12311353
From("vendor.items").

0 commit comments

Comments
 (0)