Skip to content

Commit

Permalink
added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sinhashubham95 committed Jan 9, 2021
1 parent c4ce0e4 commit 7f0eed2
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 35 deletions.
46 changes: 21 additions & 25 deletions jsonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (j *Jsonic) GetInt(path string) (int, error) {
if err != nil {
return 0, err
}
if i, ok := val.(int); ok {
return i, nil
if i, ok := val.(float64); ok {
return int(i), nil
}
return 0, ErrInvalidType
}
Expand All @@ -75,8 +75,8 @@ func (j *Jsonic) GetInt64(path string) (int64, error) {
if err != nil {
return 0, err
}
if i, ok := val.(int64); ok {
return i, nil
if i, ok := val.(float64); ok {
return int64(i), nil
}
return 0, ErrInvalidType
}
Expand All @@ -87,8 +87,8 @@ func (j *Jsonic) GetFloat(path string) (float32, error) {
if err != nil {
return 0, err
}
if f, ok := val.(float32); ok {
return f, nil
if f, ok := val.(float64); ok {
return float32(f), nil
}
return 0, ErrInvalidType
}
Expand Down Expand Up @@ -149,8 +149,8 @@ func (j *Jsonic) GetIntArray(path string) ([]int, error) {
}
iArr := make([]int, len(val))
for index, v := range val {
if i, ok := v.(int); ok {
iArr[index] = i
if i, ok := v.(float64); ok {
iArr[index] = int(i)
}
}
return iArr, nil
Expand All @@ -164,8 +164,8 @@ func (j *Jsonic) GetInt64Array(path string) ([]int64, error) {
}
iArr := make([]int64, len(val))
for index, v := range val {
if i, ok := v.(int64); ok {
iArr[index] = i
if i, ok := v.(float64); ok {
iArr[index] = int64(i)
}
}
return iArr, nil
Expand All @@ -179,8 +179,8 @@ func (j *Jsonic) GetFloatArray(path string) ([]float32, error) {
}
fArr := make([]float32, len(val))
for index, v := range val {
if f, ok := v.(float32); ok {
fArr[index] = f
if f, ok := v.(float64); ok {
fArr[index] = float32(f)
}
}
return fArr, nil
Expand Down Expand Up @@ -251,8 +251,8 @@ func (j *Jsonic) GetIntMap(path string) (map[string]int, error) {
}
iMap := make(map[string]int)
for k, v := range val {
if i, ok := v.(int); ok {
iMap[k] = i
if i, ok := v.(float64); ok {
iMap[k] = int(i)
}
}
return iMap, nil
Expand All @@ -266,8 +266,8 @@ func (j *Jsonic) GetInt64Map(path string) (map[string]int64, error) {
}
iMap := make(map[string]int64)
for k, v := range val {
if i, ok := v.(int64); ok {
iMap[k] = i
if i, ok := v.(float64); ok {
iMap[k] = int64(i)
}
}
return iMap, nil
Expand All @@ -281,8 +281,8 @@ func (j *Jsonic) GetFloatMap(path string) (map[string]float32, error) {
}
fMap := make(map[string]float32)
for k, v := range val {
if f, ok := v.(float32); ok {
fMap[k] = f
if f, ok := v.(float64); ok {
fMap[k] = float32(f)
}
}
return fMap, nil
Expand Down Expand Up @@ -363,14 +363,15 @@ func (j *Jsonic) childFromArray(array []interface{}, path []string) (*Jsonic, er
}

func (j *Jsonic) childFromObject(object map[string]interface{}, path []string) (*Jsonic, error) {
current := path[0]
current := ""
// this loop is to handle the following scenario
// say the path elements are as follows a, b and c
// it might so happen that each of a, a.b and a.b.c are
// present in the json data as keys, so we should give
// each of them a fair chance. the only thing is we
// are giving preference in the following order a > a.b > a.b.c
for i, p := range path {
current += p
if cached := j.checkInCache(current); cached != nil {
result, err := cached.child(path[i+1:])
if err == nil {
Expand All @@ -387,7 +388,7 @@ func (j *Jsonic) childFromObject(object map[string]interface{}, path []string) (
}
}
// nothing here, check further
current = joinPath(current, p)
current += dot
}
return nil, ErrNoDataFound
}
Expand All @@ -408,7 +409,6 @@ func (j *Jsonic) child(path []string) (*Jsonic, error) {
return j.childFromObject(object, path)
}
return nil, ErrUnexpectedJSONData

}

func getIndex(element string) (int, error) {
Expand All @@ -427,7 +427,3 @@ func (j *Jsonic) saveInCache(path string, child *Jsonic) {
defer j.mu.Unlock()
j.cache[path] = child
}

func joinPath(first, second string) string {
return first + dot + second
}
96 changes: 86 additions & 10 deletions jsonic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,115 @@ import (

func readFromFile(path string, t *testing.T) []byte {
data, err := ioutil.ReadFile(path)
assert.Nil(t, err)
assert.NoError(t, err)
return data
}

func TestNewSuccess(t *testing.T) {
j, err := jsonic.New(readFromFile("test_data/test1.json", t))
assert.NotNil(t, j)
assert.Nil(t, err)
assert.NoError(t, err)
}

func TestNewError(t *testing.T) {
j, err := jsonic.New(nil)
assert.Nil(t, j)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "unexpected end of JSON input")
assert.Error(t, err)
assert.Equal(t, "unexpected end of JSON input", err.Error())
}

func TestChild(t *testing.T) {
j, err := jsonic.New(readFromFile("test_data/test1.json", t))
assert.NotNil(t, j)
assert.Nil(t, err)
assert.NoError(t, err)

// simple
c1, err := j.Child("c")
assert.Nil(t, err)
assert.NoError(t, err)
assert.NotNil(t, c1)
s, err := c1.GetString(".")
assert.Nil(t, err)
assert.Equal(t, s, "d")
assert.NoError(t, err)
assert.Equal(t, "d", s)

// same
c2, err := j.Child(".")
assert.Nil(t, err)
assert.NoError(t, err)
assert.NotNil(t, c2)
assert.Equal(t, c2, j)
assert.Equal(t, j, c2)

// not found
c3, err := j.Child("a.m")
assert.Error(t, err)
assert.Nil(t, c3)
assert.Equal(t, jsonic.ErrNoDataFound, err)

// array
c4, err := j.Child("a.arr")
assert.NoError(t, err)
assert.NotNil(t, c4)
m, err := c4.GetMap("[0]")
assert.NoError(t, err)
assert.NotNil(t, m)
assert.Equal(t, "b", m["a"])

// array nested query
c5, err := j.Child("a.arr.[0].c.d")
assert.NoError(t, err)
assert.NotNil(t, c5)
s, err = c5.GetString("e")
assert.NoError(t, err)
assert.Equal(t, "f", s)

// array index not found
c6, err := j.Child("a.arr.xyz")
assert.Error(t, err)
assert.Nil(t, c6)
assert.Equal(t, jsonic.ErrNoDataFound, err)

// array index out of bound
c7, err := j.Child("a.arr.[1]")
assert.Error(t, err)
assert.Nil(t, c7)
assert.Equal(t, jsonic.ErrNoDataFound, err)
}

func TestGet(t *testing.T) {
j, err := jsonic.New(readFromFile("test_data/test1.json", t))
assert.NotNil(t, j)
assert.NoError(t, err)

// child exists
v, err := j.Get("a.x")
assert.NoError(t, err)
assert.NotNil(t, v)
assert.Equal(t, "p", v)

// child not exists
v, err = j.Get("a.y")
assert.Error(t, err)
assert.Nil(t, v)
}

func TestTyped(t *testing.T) {
j, err := jsonic.New(readFromFile("test_data/test2.json", t))
assert.NotNil(t, j)
assert.NoError(t, err)

i, err := j.GetInt("z")
assert.Equal(t, 0, i)
assert.Error(t, err)
assert.Equal(t, jsonic.ErrNoDataFound, err)

i, err = j.GetInt("a")
assert.Equal(t, 1, i)
assert.NoError(t, err)

i64, err := j.GetInt64("z")
assert.Equal(t, int64(0), i64)
assert.Error(t, err)
assert.Equal(t, jsonic.ErrNoDataFound, err)

i64, err = j.GetInt64("a")
assert.Equal(t, int64(1), i64)
assert.NoError(t, err)
}
34 changes: 34 additions & 0 deletions test_data/test2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"a": 1,
"b": 2.2,
"c": true,
"d": "naruto",
"e": [
1,
2
],
"f": [
1.1,
2.2
],
"g": [
true,
false
],
"h": [
"naruto",
"boruto"
],
"i": {
"naruto": 1
},
"j": {
"naruto": 1.1
},
"k": {
"naruto": true
},
"l": {
"naruto": "rocks"
}
}

0 comments on commit 7f0eed2

Please sign in to comment.