-
Notifications
You must be signed in to change notification settings - Fork 3
/
sprig.go
158 lines (137 loc) · 3.67 KB
/
sprig.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
151
152
153
154
155
156
157
158
package protoTemplates
import (
"github.com/Masterminds/sprig"
"github.com/eclipse/eclipsefuro/furo/pkg/orderedmap"
"github.com/eclipse/eclipsefuro/furo/pkg/specSpec"
"github.com/spf13/viper"
"html/template"
"regexp"
"strings"
)
type FieldMap struct {
Fieldname string
Field *specSpec.Field
}
type EnumMap struct {
Optionname string
Value *int32
}
func fieldpairs(orderedMap orderedmap.OrderedMap) (f []FieldMap) {
f = []FieldMap{}
for p := orderedMap.Oldest(); p != nil; p = p.Next() {
m := FieldMap{
Fieldname: p.Key.(string),
Field: p.Value.(*specSpec.Field),
}
f = append(f, m)
}
return f
}
func enumpairs(orderedMap orderedmap.OrderedMap) (f []EnumMap) {
f = []EnumMap{}
for p := orderedMap.Oldest(); p != nil; p = p.Next() {
m := EnumMap{
Optionname: p.Key.(string),
Value: p.Value.(*int32),
}
f = append(f, m)
}
return f
}
func rpcmap(orderedMap *orderedmap.OrderedMap) (f map[string]*specSpec.Rpc) {
f = map[string]*specSpec.Rpc{}
for p := orderedMap.Oldest(); p != nil; p = p.Next() {
f[p.Key.(string)] = p.Value.(*specSpec.Rpc)
}
return f
}
func noescape(str string) template.HTML {
return template.HTML(str)
}
func collectoneof(fields []FieldMap) (oneoflist map[string]map[string]*specSpec.Field) {
oneoflist = map[string]map[string]*specSpec.Field{}
for _, f := range fields {
field := f.Field
if field.XProto.Oneof != "" {
if oneoflist[field.XProto.Oneof] == nil {
oneoflist[field.XProto.Oneof] = map[string]*specSpec.Field{}
}
oneoflist[field.XProto.Oneof][f.Fieldname] = field
}
}
return oneoflist
}
type Qparams struct {
Description string
Type string
Name string
FieldId int
}
// extract request params from service
func requestParamsFromRpc(rpc *specSpec.Rpc) []Qparams {
// path params regexFindAll "{[a-zA-Z]*}" $method.deeplink.href 10
regex := regexp.MustCompile(`{([^{]+)}`)
matches := regex.FindAllStringSubmatch(rpc.Deeplink.Href, 50)
params := []Qparams{}
i := 1
for _, match := range matches {
params = append(params, Qparams{
Description: "deeplink params for path segment {" + match[1] + "} of " + rpc.Deeplink.Href,
Type: "string",
Name: match[1],
FieldId: i,
})
i++
}
// request type
if rpc.Data.Request != "" {
params = append(params, Qparams{
Description: "request type " + rpc.Data.Request,
Type: rpc.Data.Request,
Name: "data",
FieldId: i,
})
i++
}
// query
if rpc.Query != nil {
rpc.Query.Map(func(iKey interface{}, iValue interface{}) {
p := iValue.(*specSpec.Queryparam)
params = append(params, Qparams{
Description: p.Description,
Type: p.Type,
Name: iKey.(string),
FieldId: i,
})
i++
})
}
return params
}
// builds the request for the rpc
// rpc {{$method.RpcName}} ({{$method | rpcRequest}}) returns ({{$method.Data.Response}}){
// return specified methods for pure grpc or stream, otherwise RpcName + Suffix
func rpcRequest(method *specSpec.Rpc) string {
if strings.HasPrefix(method.Data.Request, "stream ") || method.Deeplink.Href == "" {
return method.Data.Request
}
return method.RpcName + viper.GetString("muSpec.requestTypeSuffix")
}
func isNotStream(method *specSpec.Rpc) bool {
if strings.HasPrefix(method.Data.Request, "stream ") || method.Deeplink.Href == "" {
return false
}
return true
}
func GetSprigFuncs() template.FuncMap {
fn := sprig.FuncMap()
fn["noescape"] = noescape
fn["collectoneof"] = collectoneof
fn["fieldpairs"] = fieldpairs
fn["enumpairs"] = enumpairs
fn["rpcmap"] = rpcmap
fn["requestParamsFromRpc"] = requestParamsFromRpc
fn["rpcRequest"] = rpcRequest
fn["isNotStream"] = isNotStream
return fn
}