-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_mutation_gen.go
150 lines (130 loc) · 3.49 KB
/
api_mutation_gen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright 2025 Hypermode Inc.
* Licensed under the terms of the Apache License, Version 2.0
* See the LICENSE file that accompanied this code for further details.
*
* SPDX-FileCopyrightText: 2025 Hypermode Inc. <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
package modusdb
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/dgraph-io/dgo/v240/protos/api"
"github.com/dgraph-io/dgraph/v24/dql"
"github.com/dgraph-io/dgraph/v24/protos/pb"
"github.com/dgraph-io/dgraph/v24/schema"
"github.com/dgraph-io/dgraph/v24/x"
"github.com/hypermodeinc/modusdb/api/apiutils"
"github.com/hypermodeinc/modusdb/api/dgraphtypes"
"github.com/hypermodeinc/modusdb/api/mutations"
"github.com/hypermodeinc/modusdb/api/structreflect"
)
func generateSetDqlMutationsAndSchema[T any](ctx context.Context, n *Namespace, object T,
gid uint64, dms *[]*dql.Mutation, sch *schema.ParsedSchema) error {
t := reflect.TypeOf(object)
if t.Kind() != reflect.Struct {
return fmt.Errorf("expected struct, got %s", t.Kind())
}
tagMaps, err := structreflect.GetFieldTags(t)
if err != nil {
return err
}
jsonTagToValue := structreflect.GetJsonTagToValues(object, tagMaps.FieldToJson)
nquads := make([]*api.NQuad, 0)
uniqueConstraintFound := false
for jsonName, value := range jsonTagToValue {
reflectValueType := reflect.TypeOf(value)
var nquad *api.NQuad
if tagMaps.JsonToReverseEdge[jsonName] != "" {
reverseEdgeStr := tagMaps.JsonToReverseEdge[jsonName]
typeName := strings.Split(reverseEdgeStr, ".")[0]
currSchema, err := getSchema(ctx, n)
if err != nil {
return err
}
typeFound := false
predicateFound := false
for _, t := range currSchema.Types {
if t.Name == typeName {
typeFound = true
for _, f := range t.Fields {
if f.Name == reverseEdgeStr {
predicateFound = true
break
}
}
break
}
}
if !(typeFound && predicateFound) {
if err := mutations.HandleReverseEdge(jsonName, reflectValueType, n.ID(), sch,
reverseEdgeStr); err != nil {
return err
}
}
continue
}
if jsonName == "gid" {
uniqueConstraintFound = true
continue
}
value, err = processStructValue(ctx, value, n)
if err != nil {
return err
}
value, err = processPointerValue(ctx, value, n)
if err != nil {
return err
}
nquad, u, err := mutations.CreateNQuadAndSchema(value, gid, jsonName, t, n.ID())
if err != nil {
return err
}
uniqueConstraintFound, err = dgraphtypes.HandleConstraints(u, tagMaps.JsonToDb,
jsonName, u.ValueType, uniqueConstraintFound)
if err != nil {
return err
}
sch.Preds = append(sch.Preds, u)
nquads = append(nquads, nquad)
}
if !uniqueConstraintFound {
return fmt.Errorf(apiutils.NoUniqueConstr, t.Name())
}
sch.Types = append(sch.Types, &pb.TypeUpdate{
TypeName: apiutils.AddNamespace(n.ID(), t.Name()),
Fields: sch.Preds,
})
val, err := dgraphtypes.ValueToApiVal(t.Name())
if err != nil {
return err
}
typeNquad := &api.NQuad{
Namespace: n.ID(),
Subject: fmt.Sprint(gid),
Predicate: "dgraph.type",
ObjectValue: val,
}
nquads = append(nquads, typeNquad)
*dms = append(*dms, &dql.Mutation{
Set: nquads,
})
return nil
}
func generateDeleteDqlMutations(n *Namespace, gid uint64) []*dql.Mutation {
return []*dql.Mutation{{
Del: []*api.NQuad{
{
Namespace: n.ID(),
Subject: fmt.Sprint(gid),
Predicate: x.Star,
ObjectValue: &api.Value{
Val: &api.Value_DefaultVal{DefaultVal: x.Star},
},
},
},
}}
}