-
Notifications
You must be signed in to change notification settings - Fork 45
/
entity_tupledata.go
129 lines (116 loc) · 3.12 KB
/
entity_tupledata.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
package relgom
import (
. "github.com/anz-bank/sysl/language/go/pkg/ast" //nolint:golint,stylecheck
"github.com/anz-bank/sysl/pkg/syslutil"
)
const tupleDataRecv = "d"
func (g *entityGenerator) goAppendTupleDataDecls(decls []Decl) []Decl {
return append(decls,
g.goTupleDataTypeDecl(),
g.marshalTupleDataJSONFunc(),
g.unmarshalTupleDataJSONFunc(),
)
}
func (g *entityGenerator) goTupleDataTypeDecl() Decl {
fields := []Field{}
if g.haveKeys {
fields = append(fields, Field{Type: I(g.pkName)})
}
fields = append(fields,
g.goFieldsForSyslAttrDefs(syslutil.NamedTypeNot(g.isPkeyAttr), false, false, nil)...,
)
return Types(TypeSpec{
Name: *I(g.dataName),
Type: Struct(fields...),
}).WithDoc(Commentf("// %s is the internal representation of a tuple in the model.", g.dataName))
}
func (g *entityGenerator) tupleDataMethod(f FuncDecl) *FuncDecl {
f.Recv = Fields(Field{
Names: Idents(tupleDataRecv),
Type: Star(I(g.dataName)),
}).Parens()
return &f
}
func (g *entityGenerator) marshalTupleDataJSONFunc() Decl {
kvs := []Expr{}
for _, nt := range g.namedAttrs {
ti := g.typeInfoForSyslType(nt.Type)
kvs = append(kvs, &KeyValueExpr{
Key: ExportedID(nt.Name),
Value: ti.stage(Dot(I(tupleDataRecv), NonExportedName(nt.Name))),
})
}
return g.tupleDataMethod(*marshalJSONMethodDecl(
Return(Call(g.json("Marshal"), Composite(g.goExportedStruct(true), kvs...))),
))
}
func (g *entityGenerator) unmarshalTupleDataJSONFunc() Decl {
tmp := "u"
pkKVs := []Expr{}
kvs := []Expr{}
stagings := []Stmt{}
if g.haveKeys {
kvs = append(kvs, nil)
}
for _, nt := range g.namedAttrs {
ti := g.typeInfoForSyslType(nt.Type)
ename := ExportedName(nt.Name)
var value Expr = Dot(I(tmp), ename)
if ti.unstage != nil {
u := "unstage" + ename
stagings = append(stagings,
Init(u, "err")(ti.unstage(value)),
If(nil, Binary(I("err"), "!=", I("nil")),
Return(Call(g.imported("fmt")("Errorf"),
String("error unstaging %s.%s: %v"), String(g.tname), String(nt.Name), I("err"),
)),
),
)
value = I(u)
}
if g.isPkeyAttr(nt) {
pkKVs = append(pkKVs, &KeyValueExpr{
Key: NonExportedID(nt.Name),
Value: value,
})
} else {
kvs = append(kvs, &KeyValueExpr{
Key: NonExportedID(nt.Name),
Value: value,
})
}
}
if g.haveKeys {
kvs[0] = &KeyValueExpr{
Key: I(g.pkName),
Value: Composite(I(g.pkName), pkKVs...),
}
}
stmts := []Stmt{
&DeclStmt{Decl: Var(ValueSpec{
Names: Idents(tmp),
Type: g.goExportedStruct(true),
})},
If(
Init("err")(Call(g.json("Unmarshal"), I("data"), AddrOf(I(tmp)))),
Binary(I("err"), "!=", Nil()),
Return(I("err")),
),
}
stmts = append(stmts, stagings...)
stmts = append(stmts,
Assign(Star(I(tupleDataRecv)))("=")(Composite(I(g.dataName), kvs...)),
Return(Nil()),
)
return g.tupleDataMethod(*unmarshalJSONMethodDecl(stmts...))
}
func (g *entityGenerator) goExportedStruct(staging bool) *StructType {
return Struct(g.goFieldsForSyslAttrDefs(
syslutil.NamedTypeAll,
true,
staging,
func(nt syslutil.NamedType) map[string]string {
return map[string]string{"json": nt.Name + ",omitempty"}
},
)...)
}