From e24fe507a0ca2c852a3c6ac0f63d532cc20fbf19 Mon Sep 17 00:00:00 2001 From: Jai Radhakrishnan <55522316+jairad26@users.noreply.github.com> Date: Fri, 20 Dec 2024 00:04:25 -0800 Subject: [PATCH] fix query after delete --- api_helper.go | 9 +++++++-- api_reflect.go | 14 ++++++++++++-- api_test.go | 9 ++++++--- api_types.go | 6 ++++++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/api_helper.go b/api_helper.go index bcd4928..0cda75b 100644 --- a/api_helper.go +++ b/api_helper.go @@ -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) @@ -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) @@ -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 } diff --git a/api_reflect.go b/api_reflect.go index 817736e..be32e69 100644 --- a/api_reflect.go +++ b/api_reflect.go @@ -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() @@ -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 + return 0, ErrNoObjFound + } } else { finalField = vFinal.FieldByName(field.Name) } @@ -111,5 +121,5 @@ func mapDynamicToFinal(dynamic any, final any) uint64 { } } } - return gid + return gid, nil } diff --git a/api_test.go b/api_test.go index 4d2c7aa..5b1c6bc 100644 --- a/api_test.go +++ b/api_test.go @@ -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()) diff --git a/api_types.go b/api_types.go index 5d2f90d..9cea571 100644 --- a/api_types.go +++ b/api_types.go @@ -1,5 +1,11 @@ package modusdb +import "fmt" + +var ( + ErrNoObjFound = fmt.Errorf("no object found") +) + type UniqueField interface { uint64 | ConstrainedField }