Skip to content

Commit

Permalink
Release v0.1.2
Browse files Browse the repository at this point in the history
- Json static
- Query Json
- Read JSON file
  • Loading branch information
teranixbq authored May 14, 2024
2 parents 77dc472 + c791307 commit e466cfd
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 10 deletions.
17 changes: 17 additions & 0 deletions _example/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"id": "1",
"name": "Apple",
"category": "Fruit"
},
{
"id": "2",
"name": "Carrot",
"category": "Vegetable"
},
{
"id": "3",
"name": "Orange",
"category": "Fruit"
}
]
27 changes: 27 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gfunc

import (
"fmt"
"log"
"reflect"
)

func CheckStructPointer(data interface{}) error {
if reflect.TypeOf(data).Kind() == reflect.Struct {
if reflect.TypeOf(data).Kind() != reflect.Ptr {
err := ErrMsg("data must be of type pointer if struct")
log.SetFlags(0)
log.Println(err)
}
}

return nil
}

func ErrMsg(err string) string {
const redColor = "\033[31m"
const resetColor = "\033[0m"

response := fmt.Sprintf("%serror%s: %s", redColor, resetColor, err)
return response
}
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ module github.com/teranixbq/gfunc

go 1.17

require github.com/redis/go-redis/v9 v9.5.1

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/redis/go-redis/v9 v9.5.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
28 changes: 28 additions & 0 deletions jsonfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gfunc

import (
"errors"
"os"
"strings"
)

type Query struct {
jsonfile []byte
}

func NewJsonFile(file string) (*Query, error) {
if !strings.HasSuffix(file, ".json") {
return nil, errors.New("file must be json")
}

bytefile, err := os.ReadFile(file)
if err != nil {
return nil, err
}

b := Query{
jsonfile: bytefile,
}

return &b, nil
}
93 changes: 93 additions & 0 deletions jsonquery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package gfunc

import (
"encoding/json"
"errors"
"reflect"
)

func (q *Query) Find(data interface{}) error {
errPointer := CheckStructPointer(data)
if errPointer != nil {
return errPointer
}

err := json.Unmarshal(q.jsonfile, data)
if err != nil {
return err
}

return nil
}

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

err := q.Find(&dataList)
if err != nil {
return err
}

var all []interface{}
all = append(all, dataList...)

for _, v := range all {
m, err := json.Marshal(v)
if err != nil {
return err
}

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

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

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

return errors.New("data not found")
}

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

var all []interface{}
all = append(all, dataList...)

for _, v := range all {
m, err := json.Marshal(v)
if err != nil {
return err
}

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

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

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

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

return nil
}
60 changes: 60 additions & 0 deletions jsonquery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package gfunc

import (
"testing"

"github.com/stretchr/testify/assert"
)

type data struct {
Id string `json:"id"`
Name string `json:"name"`
Category string `json:"category"`
}

func TestFind(t *testing.T) {
listData := []data{}

q, errJson := NewJsonFile("_example/example.json")
err := q.Find(&listData)

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

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)

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

}

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)

assert.NoError(t, errJson)
assert.NoError(t, err)
assert.NotNil(t, findData)
}
8 changes: 0 additions & 8 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gfunc
import (
"context"
"encoding/json"
"fmt"
"log"
"reflect"
"time"
Expand Down Expand Up @@ -74,10 +73,3 @@ func (r *Redis) GetJSON(key string, data interface{}) error {
return nil
}

func ErrMsg(err string) string {
const redColor = "\033[31m"
const resetColor = "\033[0m"

response := fmt.Sprintf("%serror%s: %s", redColor, resetColor, err)
return response
}

0 comments on commit e466cfd

Please sign in to comment.