Skip to content

Commit

Permalink
move reflection logic to utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
jairad26 committed Jan 4, 2025
1 parent dd2ea80 commit 87ca74f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ lint:
- [email protected]
- [email protected]
- [email protected]
- renovate@39.88.0
- renovate@39.90.2
- [email protected]
- [email protected]

Expand Down
9 changes: 8 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,17 @@ func Upsert[T any](db *DB, object T, ns ...uint64) (uint64, T, bool, error) {
return 0, object, false, err
}

gid, cf, err := GetUniqueConstraint[T](object)
gid, cfKeyValue, err := utils.GetUniqueConstraint[T](object)
if err != nil {
return 0, object, false, err
}
var cf *ConstrainedField
if cfKeyValue != nil {
cf = &ConstrainedField{
Key: cfKeyValue.Key(),
Value: cfKeyValue.Value(),
}
}

dms := make([]*dql.Mutation, 0)
sch := &schema.ParsedSchema{}
Expand Down
34 changes: 34 additions & 0 deletions api/utils/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,37 @@ func ConvertDynamicToTyped[T any](obj any, t reflect.Type) (uint64, T, error) {
}
return 0, result, fmt.Errorf("failed to convert type %T to %T", finalObject, obj)
}

func GetUniqueConstraint[T any](object T) (uint64, *keyValue, error) {
t := reflect.TypeOf(object)
fieldToJsonTags, jsonToDbTags, _, err := GetFieldTags(t)
if err != nil {
return 0, nil, err
}
jsonTagToValue := GetJsonTagToValues(object, fieldToJsonTags)

for jsonName, value := range jsonTagToValue {
if jsonName == "gid" {
gid, ok := value.(uint64)
if !ok {
continue
}
if gid != 0 {
return gid, nil, nil
}
}
if jsonToDbTags[jsonName] != nil && IsValidUniqueIndex(jsonToDbTags[jsonName].Constraint) {
// check if value is zero or nil
if value == reflect.Zero(reflect.TypeOf(value)).Interface() || value == nil {
continue
}
return 0, &keyValue{key: jsonName, value: value}, nil
}
}

return 0, nil, fmt.Errorf(NoUniqueConstr, t.Name())
}

func IsValidUniqueIndex(name string) bool {
return name == "unique"
}
13 changes: 13 additions & 0 deletions api/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ var (
NoUniqueConstr = "unique constraint not defined for any field on type %s"
)

type keyValue struct {
key string
value any
}

func (kv *keyValue) Key() string {
return kv.key
}

func (kv *keyValue) Value() any {
return kv.value
}

func GetPredicateName(typeName, fieldName string) string {
return fmt.Sprint(typeName, ".", fieldName)
}
Expand Down
4 changes: 2 additions & 2 deletions api_mutation_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func generateSetDqlMutationsAndSchema[T any](ctx context.Context, n *Namespace,
var nquad *api.NQuad

if jsonToReverseEdgeTags[jsonName] != "" {
if err := mutations.HandleReverseEdge(jsonName, reflectValueType, n.id, sch, jsonToReverseEdgeTags); err != nil {
if err := mutations.HandleReverseEdge(jsonName, reflectValueType, n.ID(), sch, jsonToReverseEdgeTags); err != nil {
return err
}
continue
Expand Down Expand Up @@ -82,7 +82,7 @@ func generateSetDqlMutationsAndSchema[T any](ctx context.Context, n *Namespace,
}

sch.Types = append(sch.Types, &pb.TypeUpdate{
TypeName: utils.AddNamespace(n.id, t.Name()),
TypeName: utils.AddNamespace(n.ID(), t.Name()),
Fields: sch.Preds,
})

Expand Down
6 changes: 5 additions & 1 deletion api_mutation_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ func processPointerValue(ctx context.Context, value any, n *Namespace) (any, err
}

func getUidOrMutate[T any](ctx context.Context, db *DB, n *Namespace, object T) (uint64, error) {
gid, cf, err := GetUniqueConstraint[T](object)
gid, cfKeyValue, err := utils.GetUniqueConstraint[T](object)
if err != nil {
return 0, err
}
var cf *ConstrainedField
if cfKeyValue != nil {
cf = &ConstrainedField{Key: cfKeyValue.Key(), Value: cfKeyValue.Value()}
}

dms := make([]*dql.Mutation, 0)
sch := &schema.ParsedSchema{}
Expand Down
54 changes: 0 additions & 54 deletions api_reflect.go

This file was deleted.

0 comments on commit 87ca74f

Please sign in to comment.