Skip to content

Commit

Permalink
fix query after delete
Browse files Browse the repository at this point in the history
  • Loading branch information
jairad26 committed Dec 20, 2024
1 parent 688d2ff commit e24fe50
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
9 changes: 7 additions & 2 deletions api_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func getByGid[T any](ctx context.Context, n *Namespace, gid uint64) (uint64, *T,
obj(func: uid(%d)) {
uid
expand(_all_)
dgraph.type
}
}
`, gid)
Expand All @@ -203,6 +204,7 @@ func getByConstrainedField[T any](ctx context.Context, n *Namespace, cf Constrai
obj(func: eq(%s, %s)) {
uid
expand(_all_)
dgraph.type
}
}
`, getPredicateName(t.Name(), cf.Key), cf.Value)
Expand Down Expand Up @@ -246,12 +248,15 @@ func executeGet[T any](ctx context.Context, n *Namespace, query string, cf *Cons

// Check if we have at least one object in the response
if len(result.Obj) == 0 {
return 0, nil, fmt.Errorf("no object found")
return 0, nil, ErrNoObjFound
}

// Map the dynamic struct to the final type T
finalObject := reflect.New(t).Interface()
gid := mapDynamicToFinal(result.Obj[0], finalObject)
gid, err := mapDynamicToFinal(result.Obj[0], finalObject)
if err != nil {
return 0, nil, err
}

return gid, finalObject.(*T), nil
}
Expand Down
14 changes: 12 additions & 2 deletions api_reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,15 @@ func createDynamicStruct(t reflect.Type, jsonFields map[string]string) reflect.T
Name: "Uid",
Type: reflect.TypeOf(""),
Tag: reflect.StructTag(`json:"uid"`),
}, reflect.StructField{
Name: "DgraphType",
Type: reflect.TypeOf([]string{}),
Tag: reflect.StructTag(`json:"dgraph.type"`),
})
return reflect.StructOf(fields)
}

func mapDynamicToFinal(dynamic any, final any) uint64 {
func mapDynamicToFinal(dynamic any, final any) (uint64, error) {
vFinal := reflect.ValueOf(final).Elem()
vDynamic := reflect.ValueOf(dynamic).Elem()

Expand All @@ -99,6 +103,12 @@ func mapDynamicToFinal(dynamic any, final any) uint64 {
finalField = vFinal.FieldByName("Gid")
gidStr := value.String()
gid, _ = strconv.ParseUint(gidStr, 0, 64)
} else if field.Name == "DgraphType" {
fieldArr := value.Interface().([]string)
if len(fieldArr) == 0 {
final = nil

Check failure on line 109 in api_reflect.go

View workflow job for this annotation

GitHub Actions / ci-go-lint

ineffectual assignment to final (ineffassign)
return 0, ErrNoObjFound
}
} else {
finalField = vFinal.FieldByName(field.Name)
}
Expand All @@ -111,5 +121,5 @@ func mapDynamicToFinal(dynamic any, final any) uint64 {
}
}
}
return gid
return gid, nil
}
9 changes: 6 additions & 3 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,12 @@ func TestDeleteApi(t *testing.T) {
_, _, err = modusdb.Delete[User](db, gid, db1.ID())
require.NoError(t, err)

// TODO: when getting with uid, it returns an object with values set to 0 instead of an error
// _, queriedUser, err := modusdb.Get[User](db, gid, db1.ID())
_, queriedUser, err := modusdb.Get[User](db, modusdb.ConstrainedField{
_, queriedUser, err := modusdb.Get[User](db, gid, db1.ID())
require.Error(t, err)
require.Equal(t, "no object found", err.Error())
require.Nil(t, queriedUser)

_, queriedUser, err = modusdb.Get[User](db, modusdb.ConstrainedField{
Key: "clerk_id",
Value: "123",
}, db1.ID())
Expand Down
6 changes: 6 additions & 0 deletions api_types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package modusdb

import "fmt"

var (
ErrNoObjFound = fmt.Errorf("no object found")
)

type UniqueField interface {
uint64 | ConstrainedField
}
Expand Down

0 comments on commit e24fe50

Please sign in to comment.