Skip to content

Commit

Permalink
fix : remove parameter selected, change to return
Browse files Browse the repository at this point in the history
  • Loading branch information
teranixbq authored May 18, 2024
2 parents e466cfd + f229a45 commit 84411f5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
55 changes: 38 additions & 17 deletions jsonquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gfunc
import (
"encoding/json"
"errors"
"reflect"
)

func (q *Query) Find(data interface{}) error {
Expand All @@ -20,11 +19,22 @@ func (q *Query) Find(data interface{}) error {
return nil
}

func (q *Query) FindBy(by, field string, dataList []interface{}, Selected interface{}) error {

// Parameter dataList is []interface{}, so you need to use for loop to append the data e.g :
//
// findData := []data{}
// var datalist []interface{}
//
// q, errJson := NewJsonFile("your_json_file.json")
// for _, item := range findData {
// datalist = append(datalist, item)
// }
//
// result,err := q.FindBy("3", "id", datalist)
func (q *Query) FindBy(by, field string, dataList []interface{}) (interface{}, error) {
var Selected interface{}
err := q.Find(&dataList)
if err != nil {
return err
return nil, err
}

var all []interface{}
Expand All @@ -33,32 +43,43 @@ func (q *Query) FindBy(by, field string, dataList []interface{}, Selected interf
for _, v := range all {
m, err := json.Marshal(v)
if err != nil {
return err
return nil, err
}

var tempMap map[string]interface{}
if err := json.Unmarshal(m, &tempMap); err != nil {
return err
return nil, err
}

value, found := tempMap[field]
if !found {
continue
}

// if value == by {
// reflect.ValueOf(Selected).Elem().Set(reflect.ValueOf(v))
// return nil,nil
// }

if value == by {
reflect.ValueOf(Selected).Elem().Set(reflect.ValueOf(v))
return nil
Selected = v
break
}

}

return errors.New("data not found")
if Selected == nil {
return nil, errors.New("data not found")
}

return Selected, nil
}

func (q *Query) FindAllBy(by, field string, dataList []interface{}, Selected *[]interface{}) error {
func (q *Query) FindAllBy(by, field string, dataList []interface{}) ([]interface{}, error) {
var Selected []interface{}
err := q.Find(&dataList)
if err != nil {
return err
return nil, err
}

var all []interface{}
Expand All @@ -67,12 +88,12 @@ func (q *Query) FindAllBy(by, field string, dataList []interface{}, Selected *[]
for _, v := range all {
m, err := json.Marshal(v)
if err != nil {
return err
return nil, err
}

var tempMap map[string]interface{}
if err := json.Unmarshal(m, &tempMap); err != nil {
return err
return nil, err
}

value, found := tempMap[field]
Expand All @@ -81,13 +102,13 @@ func (q *Query) FindAllBy(by, field string, dataList []interface{}, Selected *[]
}

if value == by {
*Selected = append(*Selected, v)
Selected = append(Selected, v)
}
}

if len(*Selected) == 0 {
return errors.New("data not found")
if len(Selected) == 0 {
return nil, errors.New("data not found")
}

return nil
return Selected, nil
}
13 changes: 6 additions & 7 deletions jsonquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,33 @@ func TestFind(t *testing.T) {
func TestFindBy(t *testing.T) {
findData := []data{}
var datalist []interface{}
var selectedData interface{}

q, errJson := NewJsonFile("_example/example.json")
for _, item := range findData {
datalist = append(datalist, item)
}

err := q.FindBy("3", "id", datalist, &selectedData)
result, err := q.FindBy("3", "id", datalist)

assert.NoError(t, errJson)
assert.NoError(t, err)
assert.NotNil(t, selectedData)
assert.NotEmpty(t, selectedData)

assert.NotNil(t, result)
assert.NotEmpty(t, result)
}

func TestFindAllBy(t *testing.T) {
findData := []data{}
var datalist []interface{}
var selectedData []interface{}

q, errJson := NewJsonFile("_example/example.json")
for _, item := range findData {
datalist = append(datalist, item)
}
err := q.FindAllBy("Fruit", "category", datalist, &selectedData)
result, err := q.FindAllBy("Fruit", "category", datalist)

assert.NoError(t, errJson)
assert.NoError(t, err)
assert.NotNil(t, findData)
assert.NotNil(t, result)
assert.NotEmpty(t, result)
}

0 comments on commit 84411f5

Please sign in to comment.