-
Notifications
You must be signed in to change notification settings - Fork 4
/
grpc.go
115 lines (104 loc) · 3.32 KB
/
grpc.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 (
"cmp"
"fmt"
"slices"
"github.com/pkg/errors"
"projectforge.dev/projectforge/app/file"
"projectforge.dev/projectforge/app/project/export/files/helper"
"projectforge.dev/projectforge/app/project/export/golang"
"projectforge.dev/projectforge/app/project/export/model"
"projectforge.dev/projectforge/app/util"
)
const updateKey = "Update"
func GRPC(m *model.Model, args *model.Args, addHeader bool, linebreak string) (file.Files, error) {
fileArgs, err := GetGRPCFileArgs(m, args)
if err != nil {
return nil, errors.Wrap(err, "invalid arguments")
}
var ret file.Files
for _, fa := range fileArgs {
g, err := grpcFile(m, args, fa, addHeader, linebreak)
if err != nil {
return nil, errors.Wrap(err, "")
}
ret = append(ret, g)
}
return ret, nil
}
func GetGRPCFileArgs(m *model.Model, args *model.Args) ([]*FileArgs, error) {
grpcPackage := args.Config.GetStringOpt("grpcPackage")
if grpcPackage == "" {
return nil, errors.New("must provide [grpcPackage] in the export config")
}
grpcClass := args.Config.GetStringOpt("grpcClass")
if grpcClass == "" {
return nil, errors.New("must provide [grpcClass] in the export config")
}
cPkg, _ := util.StringSplitLast(grpcClass, '.', true)
grpcGroups, _ := args.Config.ParseMap("grpcGroups", true, true)
if len(grpcGroups) == 0 {
grpcGroups = util.ValueMap{"*": "*"}
}
var ret []*FileArgs
for k, grpIface := range grpcGroups {
grp, ok := grpIface.(string)
if !ok {
return nil, errors.New("grpcGroups values must be strings")
}
if grp == "*" {
ret = append(ret, &FileArgs{Class: grpcClass, Pkg: grpcPackage, CPkg: cPkg, API: k, Grp: nil})
} else {
g := m.Columns.Get(grp)
if g == nil {
continue
// return nil, errors.Errorf("grpcGroups references missing column [%s]", grp)
}
if !g.HasTag("grouped") {
return nil, errors.Errorf("grpcGroups references non-grouped column [%s]", grp)
}
ret = append(ret, &FileArgs{Class: grpcClass, Pkg: grpcPackage, CPkg: cPkg, API: k, Grp: g})
}
}
slices.SortFunc(ret, func(l *FileArgs, r *FileArgs) int {
return cmp.Compare(l.API, r.API)
})
return ret, nil
}
func grpcFile(m *model.Model, args *model.Args, ga *FileArgs, addHeader bool, linebreak string) (*file.File, error) {
fn := m.Package
if ga.API != "*" {
fn += "by" + ga.API
}
g := golang.NewFile(ga.Pkg, []string{"app", ga.Pkg}, fn)
g.AddImport(helper.ImpErrors, helper.ImpFilter, helper.ImpAppUtil)
g.AddImport(helper.AppImport("app/lib/" + ga.CPkg))
if ga.Grp == nil {
g.AddImport(helper.AppImport("app/" + m.PackageWithGroup("")))
}
grpcArgs := fmt.Sprintf("p *%s.Params", ga.CPkg)
grpcRet := fmt.Sprintf("(*%sTransaction, error)", ga.Class)
g.AddBlocks(grpcList(m, grpcArgs, grpcRet, ga))
if m.HasSearches() {
g.AddBlocks(grpcSearch(m, grpcArgs, grpcRet, ga))
}
if detail, err := grpcDetail(g, m, grpcArgs, grpcRet, ga); err == nil {
g.AddBlocks(detail)
} else {
return nil, err
}
g.AddBlocks(
grpcCall("Create", m, false, grpcArgs, grpcRet, ga),
grpcCall(updateKey, m, true, grpcArgs, grpcRet, ga),
grpcCall("Save", m, true, grpcArgs, grpcRet, ga),
grpcDelete(m, grpcArgs, grpcRet, ga),
)
if ga.Grp == nil {
b, err := grpcParamsFromRequest(g, m, grpcArgs, args.Enums)
if err != nil {
return nil, err
}
g.AddBlocks(grpcFromRequest(m, grpcArgs), b)
}
return g.Render(addHeader, linebreak)
}