Skip to content

Commit

Permalink
code cleanup on query node(s) and tests (#50)
Browse files Browse the repository at this point in the history
* code cleanup on query node(s) and tests

* don't access unexported fields

* removed redundant struct signature
  • Loading branch information
wildan2711 committed Jun 7, 2020
1 parent 8ee8cfc commit 7f55858
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 56 deletions.
28 changes: 14 additions & 14 deletions delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ func TestGenerateUidsJson(t *testing.T) {

func TestDeleteFilter(t *testing.T) {
users := []*User{
&User{
{
Name: "wildan",
Username: "wildan",
Email: "wildan2711@gmail.com",
},
&User{
{
Name: "wildan",
Username: "wildansyah",
Email: "wildansyah2711@gmail.com",
},
&User{
{
Name: "aha",
Username: "wildani",
Email: "wildani@gmail.com",
Expand Down Expand Up @@ -111,18 +111,18 @@ func TestDeleteQuery(t *testing.T) {
}

users := []*User{
&User{
{
Name: "wildan",
Username: "wildan",
Email: "wildan2711@gmail.com",
Schools: []School{school},
},
&User{
{
Name: "wildan",
Username: "wildansyah",
Email: "wildansyah2711@gmail.com",
},
&User{
{
Name: "aha",
Username: "wildani",
Email: "wildani@gmail.com",
Expand Down Expand Up @@ -169,22 +169,22 @@ func TestDeleteQueryNode(t *testing.T) {
defer dropAll(c)

users := []*User{
&User{
{
Name: "wildan",
Username: "wildan",
Email: "wildan2711@gmail.com",
Schools: []School{
School{
{
Name: "wildan's school",
},
},
},
&User{
{
Name: "wildan",
Username: "wildansyah",
Email: "wildansyah2711@gmail.com",
},
&User{
{
Name: "aha",
Username: "wildani",
Email: "wildani@gmail.com",
Expand Down Expand Up @@ -231,16 +231,16 @@ func TestDeleteEdge(t *testing.T) {
defer dropAll(c)

schools := []School{
School{
{
Name: "wildan's school",
},
School{
{
Name: "wildan's second school",
},
School{
{
Name: "wildan's third school",
},
School{
{
Name: "wildan's fourth school",
},
}
Expand Down
9 changes: 7 additions & 2 deletions mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ func newMutateType(model interface{}) (*mutateType, error) {
mType := &mutateType{}
mType.schema = make(map[int]*Schema)
mType.predIndex = make(map[string]int)
mType.nodeType = GetNodeType(model)

vType, err := reflectType(model)
if err != nil {
return nil, err
}

mType.nodeType = getNodeType(vType)
mType.vType = vType

mType.value, err = reflectValue(model)
Expand Down Expand Up @@ -461,9 +461,14 @@ func injectTypeInValue(refVal *reflect.Value) error {
field := refType.Field(i)
fieldVal := refVal.Field(i)

if !fieldVal.CanSet() {
// don't access unexported fields
continue
}

predicate := getPredicate(&field)
if predicate == dgraphTypePredicate {
nodeType := GetNodeType(refVal.Interface())
nodeType := getNodeType(refType)
switch field.Type.Kind() {
case reflect.String:
fieldVal.SetString(nodeType)
Expand Down
28 changes: 14 additions & 14 deletions mutate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ func Test_marshalAndInjectType(t *testing.T) {
Edge: TestDTypeSliceInner{Name: "wildanjing"},
PtrEdge: &TestDTypeSliceInner{Name: "wildanjing2"},
SliceEdge: []TestDTypeSliceInner{
TestDTypeSliceInner{Name: "wildanjing3"},
{Name: "wildanjing3"},
},
SlicePtrEdge: []*TestDTypeSliceInner{
&TestDTypeSliceInner{Name: "wildanjing4"},
{Name: "wildanjing4"},
},
}},
want: []byte(`{"name":"wildan","edge":{"name":"wildanjing","dgraph.type":["TestDTypeSliceInner"]},"ptr_edge":{"name":"wildanjing2","dgraph.type":["TestDTypeSliceInner"]},"slice_edge":[{"name":"wildanjing3","dgraph.type":["TestDTypeSliceInner"]}],"slice_ptr_edge":[{"name":"wildanjing4","dgraph.type":["TestDTypeSliceInner"]}],"dgraph.type":["TestDTypeSlice"]}`),
Expand All @@ -112,12 +112,12 @@ func Test_marshalAndInjectType(t *testing.T) {
{
name: "should inject slice of string in dgraph.type for slice",
args: args{&[]TestDTypeSlice{
TestDTypeSlice{
{
Name: "wildan",
Edge: TestDTypeSliceInner{Name: "wildanjing"},
PtrEdge: &TestDTypeSliceInner{Name: "wildanjing2"},
},
TestDTypeSlice{
{
Name: "wildan",
Edge: TestDTypeSliceInner{Name: "wildanjing"},
PtrEdge: &TestDTypeSliceInner{Name: "wildanjing2"},
Expand Down Expand Up @@ -235,19 +235,19 @@ func TestAddCustomNode(t *testing.T) {

func TestCreate(t *testing.T) {
testUnique := []TestUnique{
TestUnique{
{
Name: "H3h3",
Username: "wildan",
Email: "wildan2711@gmail.com",
No: 1,
},
TestUnique{
{
Name: "PooDiePie",
Username: "wildansyah",
Email: "wildansyah2711@gmail.com",
No: 2,
},
TestUnique{
{
Name: "Poopsie",
Username: "wildani",
Email: "wildani@gmail.com",
Expand Down Expand Up @@ -278,19 +278,19 @@ func TestCreate(t *testing.T) {
}

testDuplicate := []TestUnique{
TestUnique{
{
Name: "H3h3",
Username: "wildanjing",
Email: "wildan2711@gmail.com",
No: 4,
},
TestUnique{
{
Name: "PooDiePie",
Username: "wildansyah",
Email: "wildanodol2711@gmail.com",
No: 5,
},
TestUnique{
{
Name: "lalap",
Username: "lalap",
Email: "lalap@gmail.com",
Expand Down Expand Up @@ -369,13 +369,13 @@ func TestUpdate(t *testing.T) {
defer dropAll(c)

testUniques := []TestUnique{
TestUnique{
{
Name: "haha",
Username: "",
Email: "wildan2711@gmail.com",
No: 1,
},
TestUnique{
{
Name: "haha 2",
Username: "wildancok2711",
Email: "wildancok2711@gmail.com",
Expand Down Expand Up @@ -497,13 +497,13 @@ func TestCreateOrGet(t *testing.T) {
defer dropAll(c)

testUniques := []TestUnique{
TestUnique{
{
Name: "haha",
Username: "wilcok",
Email: "wildan2711@gmail.com",
No: 1,
},
TestUnique{
{
Name: "haha 2",
Username: "wildancok2711",
Email: "wildancok2711@gmail.com",
Expand Down
37 changes: 18 additions & 19 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Query struct {
uid string
filter string
query string
blocks []*Query
}

// Query defines the query portion other than the root function
Expand Down Expand Up @@ -280,39 +281,37 @@ func (q *Query) executeQuery() (result []byte, err error) {
// returns error if no nodes are found,
// query root must be data(func ...)
func Node(jsonData []byte, model interface{}) error {
dataLen := len(jsonData)
// JSON data must be in format {"data":[{ ... }]}
// get only inner object
dataPrefix := `{"data":[`
strippedPrefix := strings.TrimPrefix(string(jsonData), dataPrefix)

if len(strippedPrefix) == len(jsonData)-len(dataPrefix) {
dataBytes := []byte(strippedPrefix)
// remove the ending array closer ']'
dataBytes = dataBytes[:len(dataBytes)-2]
dataPrefixLen := len(`{"data":[`)
if dataLen < dataPrefixLen {
return fmt.Errorf("invalid json result for node: %s", jsonData)
}

if len(dataBytes) == 0 {
return ErrNodeNotFound
}
// remove prefix and the ending array closer ']'
dataBytes := jsonData[dataPrefixLen : dataLen-2]

return json.Unmarshal(dataBytes, model)
if len(dataBytes) == 0 {
return ErrNodeNotFound
}

return fmt.Errorf("invalid json result for node: %s", jsonData)
return json.Unmarshal(dataBytes, model)
}

// Nodes marshals multiple nodes to a slice of model,
// query root must be data(func ...)
func Nodes(jsonData []byte, model interface{}) error {
dataLen := len(jsonData)
// JSON data must start with {"data":
dataPrefix := `{"data":`
strippedPrefix := strings.TrimPrefix(string(jsonData), dataPrefix)

if len(strippedPrefix) == len(jsonData)-len(dataPrefix) {
dataBytes := []byte(strippedPrefix)
return json.Unmarshal(dataBytes[:len(dataBytes)-1], model)
dataPrefixLen := len(`{"data":`)
if dataLen < dataPrefixLen {
return fmt.Errorf("invalid json result for nodes: %s", jsonData)
}

return fmt.Errorf("invalid json result for nodes: %s", jsonData)
dataBytes := jsonData[dataPrefixLen : dataLen-1]

return json.Unmarshal(dataBytes, model)
}

func parseQueryWithParams(query string, params []interface{}) string {
Expand Down
6 changes: 3 additions & 3 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,17 @@ func TestGetByFilter(t *testing.T) {

func TestFind(t *testing.T) {
source := []TestModel{
TestModel{
{
Name: "wildan anjing",
Address: "Beverly Hills",
Age: 17,
},
TestModel{
{
Name: "moh wildan",
Address: "Beverly Hills",
Age: 17,
},
TestModel{
{
Name: "wildancok",
Address: "Beverly Hills",
Age: 17,
Expand Down
11 changes: 7 additions & 4 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,9 @@ func MutateSchema(c *dgo.Dgraph, models ...interface{}) (*TypeSchema, error) {
return typeSchema, nil
}

// GetNodeType gets node type from the struct name, or "dgraph" tag
// in the "dgraph.type" predicate/json tag
func GetNodeType(data interface{}) string {
func getNodeType(dataType reflect.Type) string {
// get node type from struct name
nodeType := ""
dataType := reflect.TypeOf(data)
for dataType.Kind() != reflect.Struct {
dataType = dataType.Elem()
}
Expand All @@ -465,3 +462,9 @@ func GetNodeType(data interface{}) string {
}
return nodeType
}

// GetNodeType gets node type from the struct name, or "dgraph" tag
// in the "dgraph.type" predicate/json tag
func GetNodeType(data interface{}) string {
return getNodeType(reflect.TypeOf(data))
}

0 comments on commit 7f55858

Please sign in to comment.