Skip to content

Commit

Permalink
Issue ostafen#51:
Browse files Browse the repository at this point in the history
- adding `books.json` to help on the slice testing;
- adding `lookupSliceField` that recursively dive inside a slice;
- adding test cases for the indexing structure.
  • Loading branch information
DaniloMarques1 committed Jun 5, 2022
1 parent f9c3713 commit 819c0c8
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
24 changes: 24 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
airlinesPath = "test/data/airlines.json"
todosPath = "test/data/todos.json"
earthquakes = "test/data/earthquakes.json"
booksPath = "test/data/books.json"
)

type TodoModel struct {
Expand Down Expand Up @@ -1365,3 +1366,26 @@ func TestCompareDocumentFields(t *testing.T) {
}
})
}

func TestSliceIndexing(t *testing.T) {
runCloverTest(t, booksPath, nil, func(t *testing.T, db *c.DB) {
docs, err := db.Query("books").Where(c.Field("title").Eq("A Study in Scarlet")).FindAll()
require.NoError(t, err)
d0 := docs[0]

docs, err = db.Query("books").Where(c.Field("title").Eq("The Hound of The Baskervilles")).FindAll()
require.NoError(t, err)
d1 := docs[0]

require.Equal(t, "Scotland", d0.Get("authors.0.bio.born"))
require.Equal(t, float64(1859), d1.Get("authors.0.bio.year"))
require.Equal(t, "Sir Arthur Conan Doyle", d1.Get("authors.0.name"))
require.Equal(t, "Crime", d1.Get("genre.1"))
require.Equal(t, "Detective Fiction", d0.Get("genre.0"))
require.Nil(t, d1.Get("genre.10"))
require.Nil(t, d1.Get("genre.first"))
require.Nil(t, d1.Get("authors.name"))
require.Equal(t, "The final problem", d0.Get("authors.0.bio.best_sellers.0.2"))
require.Nil(t, d1.Get("authors.0.bio.best_sellers.0"))
})
}
44 changes: 42 additions & 2 deletions document.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package clover

import (
"strconv"
"strings"

"github.com/ostafen/clover/encoding"
Expand Down Expand Up @@ -54,7 +55,10 @@ func lookupField(name string, fieldMap map[string]interface{}, force bool) (map[
var exists bool
var f interface{}
currMap := fieldMap
for i, field := range fields {
end := len(fields)
i := 0
for i < end {
field := fields[i]
f, exists = currMap[field]

m, isMap := f.(map[string]interface{})
Expand All @@ -69,13 +73,49 @@ func lookupField(name string, fieldMap map[string]interface{}, force bool) (map[
return nil, nil, ""
}

if i < len(fields)-1 {
s, isSlice := f.([]interface{})
if isSlice {
if i < end-1 {
v, increment := lookupSliceField(fields[i+1:], 2, s)
if m, isMap := v.(map[string]interface{}); isMap {
currMap = m
} else if v == nil {
return nil, nil, ""
} else {
f = v
}

i += increment
continue
}
} else if i < end-1 {
currMap = m
}

i += 1
}
return currMap, f, fields[len(fields)-1]
}

func lookupSliceField(fields []string, increment int, s []interface{}) (interface{}, int) {
fidx := fields[0]
idx, err := strconv.Atoi(fidx)
if err != nil {
return nil, 1
}
if idx > len(s)-1 {
return nil, 1
}

value, isSlice := s[idx].([]interface{})
if isSlice {
return lookupSliceField(fields[1:], increment+1, value)
}

return s[idx], increment

}

// Has tells returns true if the document contains a field with the supplied name.
func (doc *Document) Has(name string) bool {
fieldMap, _, _ := lookupField(name, doc.fields, false)
Expand Down
38 changes: 38 additions & 0 deletions test/data/books.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"title": "A Study in Scarlet",
"publisher": "Ward Lock & Co",
"genre": ["Detective Fiction", "Crime"],
"authors": [
{
"name": "Sir Arthur Conan Doyle",
"bio": {
"born": "Scotland",
"year": 1859,
"best_sellers": [
[
"The Hound of the Baskerville",
"The Adventure of the Speckled Band",
"The final problem"
]
]
}
}
]
},
{
"title": "The Hound of The Baskervilles",
"publisher": "George Newnes Ltd",
"genre": ["Detective Fiction", "Crime"],
"authors": [
{
"name": "Sir Arthur Conan Doyle",
"bio": {
"born": "Scotland",
"year": 1859,
"best_sellers": []
}
}
]
}
]

0 comments on commit 819c0c8

Please sign in to comment.