Skip to content

Commit

Permalink
Merge pull request #45 from thedevsaddam/feature/offset-method
Browse files Browse the repository at this point in the history
Add Offset method #42
  • Loading branch information
thedevsaddam authored Apr 3, 2019
2 parents ac2d82a + c46d723 commit 849a7b7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
29 changes: 28 additions & 1 deletion jsonq.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ type JSONQ struct {
queryIndex int // contains number of orWhere query call
queries [][]query // nested queries
attributes []string // select attributes that will be available in final resuls
limitRecords int // number of records that willbe available in final result
offsetRecords int // number of records that will be skipped in final result
limitRecords int // number of records that will be available in final result
distinctProperty string // contain the distinct attribute name
errors []error // contains all the errors when processing
}
Expand Down Expand Up @@ -155,6 +156,28 @@ func (j *JSONQ) Select(properties ...string) *JSONQ {
return j
}

// Offset skips the number of records in result
func (j *JSONQ) Offset(offset int) *JSONQ {
j.offsetRecords = offset
return j
}

// offset skips the records from result
func (j *JSONQ) offset() *JSONQ {
if list, ok := j.jsonContent.([]interface{}); ok {
if j.offsetRecords <= 0 {
j.addError(fmt.Errorf("%d is invalid offset", j.offsetRecords))
return j
}
if len(list) > j.offsetRecords {
j.jsonContent = list[j.offsetRecords:]
} else {
j.jsonContent = make([]interface{}, 0)
}
}
return j
}

// Limit limits the number of records in result
func (j *JSONQ) Limit(limit int) *JSONQ {
j.limitRecords = limit
Expand Down Expand Up @@ -508,6 +531,7 @@ func (j *JSONQ) reset() *JSONQ {
j.queries = make([][]query, 0)
j.attributes = make([]string, 0)
j.queryIndex = 0
j.offsetRecords = 0
j.limitRecords = 0
j.distinctProperty = ""
return j
Expand All @@ -524,6 +548,9 @@ func (j *JSONQ) Get() interface{} {
if j.distinctProperty != "" {
j.distinct()
}
if j.offsetRecords != 0 {
j.offset()
}
if j.limitRecords != 0 {
j.limit()
}
Expand Down
43 changes: 43 additions & 0 deletions jsonq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ func TestJSONQ_Select(t *testing.T) {
}
}

func TestJSONQ_Offset(t *testing.T) {
jq := New().Offset(3)
if jq.offsetRecords != 3 {
t.Error("failed to set offset records value")
}
}

func TestJSONQ_Limit(t *testing.T) {
jq := New().Limit(12)
if jq.limitRecords != 12 {
Expand Down Expand Up @@ -1219,6 +1226,33 @@ func TestJSONQ_Get_with_nested_invalid_property_in_Select_method_expecting_error
assertJSON(t, out, expected, "nested Select method using alias")
}

func TestJSONQ_Offset_method(t *testing.T) {
jq := New().JSONString(jsonStr).
From("vendor.items").
Offset(4)
out := jq.Get()
expected := `[{"id":5,"key":2300,"name":"HP core i5","price":850},{"id":6,"name":"HP core i7","price":950},{"id":null,"name":"HP core i3 SSD","price":850}]`
assertJSON(t, out, expected, "failed to offset records")
}

func TestJSONQ_Offset_Where_method(t *testing.T) {
jq := New().JSONString(jsonStr).
From("vendor.items").
Offset(4).WhereNotNil("id")
out := jq.Get()
expected := `[{"id":5,"key":2300,"name":"HP core i5","price":850},{"id":6,"name":"HP core i7","price":950}]`
assertJSON(t, out, expected, "failed to limit records")
}

func TestJSONQ_Offset_greater_than_the_original_value_with_Where_method(t *testing.T) {
jq := New().JSONString(jsonStr).
From("vendor.items").
Offset(40).WhereNotNil("id")
out := jq.Get()
expected := `[]`
assertJSON(t, out, expected, "failed to offset records when offset value is greater than the length of orignal result")
}

func TestJSONQ_Limit_method(t *testing.T) {
jq := New().JSONString(jsonStr).
From("vendor.items").
Expand All @@ -1237,6 +1271,15 @@ func TestJSONQ_Limit_Where_method(t *testing.T) {
assertJSON(t, out, expected, "failed to limit records")
}

func TestJSONQ_Offset_invalid_number_should_return_error(t *testing.T) {
jq := New().JSONString(jsonStr).
From("vendor.items").
Offset(-2)
jq.Get()
if jq.Error() == nil {
t.Error("failed to catch invalid offset error")
}
}
func TestJSONQ_Limit_invalid_number_should_return_error(t *testing.T) {
jq := New().JSONString(jsonStr).
From("vendor.items").
Expand Down

0 comments on commit 849a7b7

Please sign in to comment.