-
Notifications
You must be signed in to change notification settings - Fork 4
/
args.go
115 lines (103 loc) · 3.28 KB
/
args.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
package grpc
import (
"strings"
"github.com/pkg/errors"
"projectforge.dev/projectforge/app/export/files/helper"
"projectforge.dev/projectforge/app/export/golang"
"projectforge.dev/projectforge/app/export/model"
"projectforge.dev/projectforge/app/lib/types"
"projectforge.dev/projectforge/app/util"
)
type FileArgs struct {
Class string
Pkg string
CPkg string
API string
Grp *model.Column
}
func (a FileArgs) APISuffix() string {
if a.API == "*" {
return ""
}
return "By" + util.StringToTitle(a.API)
}
func (a FileArgs) KeySuffix() string {
if a.API == "*" {
return ""
}
return "." + a.API
}
func (a FileArgs) GrpSuffix() string {
if a.Grp == nil {
return ""
}
return "By" + a.Grp.Proper()
}
func (a FileArgs) AddStaticCheck(ref string, ret *golang.Block, m *model.Model, grp *model.Column, act string) {
if grp == nil {
return
}
ret.W("\tif %s.%s != %s {", ref, grp.Proper(), grp.Camel())
const msg = "\t\treturn nil, errors.Errorf(\"unauthorized - user acting on behalf of [%%%%s] cannot %s a %s for [%%%%s]\", %s, %s.%s)"
ret.W(msg, act, m.Camel(), grp.Camel(), ref, grp.Proper())
ret.W("\t}")
}
func grpcAddSection(b *golang.Block, key string, indent int) {
ind := util.StringRepeat("\t", indent)
b.W(ind+"// $PF_SECTION_START(%s)$", key)
b.W(ind+"// $PF_SECTION_END(%s)$", key)
}
func idClauseFor(m *model.Model) (string, string) {
if m.IsSoftDelete() {
return "\tincludeDeleted, _ := provider.GetBool(p.R, p.TX, \"includeDeleted\")", ", includeDeleted"
}
return "", ""
}
func grpcParamsFromRequest(m *model.Model, args string, g *golang.File) (*golang.Block, error) {
ret := golang.NewBlock("grpcParamsFromRequest", "func")
pks := m.PKs()
ret.W("func %sParamsFromRequest(%s) (%s, error) {", m.Camel(), args, strings.Join(pks.GoTypeKeys(m.Package), ", "))
zeroVals := strings.Join(pks.ZeroVals(), ", ")
for _, col := range pks {
err := grpcArgFor(col, ret, zeroVals, g)
if err != nil {
return nil, errors.Wrap(err, "")
}
}
ret.W("\treturn %s, nil", strings.Join(pks.CamelNames(), ", "))
ret.W("}")
return ret, nil
}
func grpcArgFor(col *model.Column, b *golang.Block, zeroVals string, g *golang.File) error {
switch col.Type.Key() {
case types.KeyBool:
b.W("\t%s, err := provider.GetRequestBool(p.R, %q)", col.Camel(), col.Camel())
b.W("\tif err != nil {")
b.W("\t\treturn %s, err", zeroVals)
b.W("\t}")
case types.KeyInt:
b.W("\t%s, err := provider.GetRequestInt(p.R, %q)", col.Camel(), col.Camel())
b.W("\tif err != nil {")
b.W("\t\treturn %s, err", zeroVals)
b.W("\t}")
case types.KeyString:
b.W("\t%s, err := provider.GetRequestString(p.R, %q)", col.Camel(), col.Camel())
b.W("\tif err != nil {")
b.W("\t\treturn %s, err", zeroVals)
b.W("\t}")
case types.KeyUUID:
g.AddImport(helper.ImpUUID)
b.W("\t%sString, err := provider.GetRequestString(p.R, %q)", col.Camel(), col.Camel())
b.W("\tif err != nil {")
b.W("\t\treturn %s, err", zeroVals)
b.W("\t}")
b.W("\t%sParsed := util.UUIDFromString(%sString)", col.Camel(), col.Camel())
b.W("\tif %sParsed == nil {", col.Camel())
b.W("\t\treturn %s, errors.New(\"field [%s] must be a uuid\")", zeroVals, col.Camel())
b.W("\t}")
b.W("\t%s := *%sParsed", col.Camel(), col.Camel())
default:
return errors.Errorf("unhandled gRPC arg type [%s]", col.Type.String())
}
return nil
}