/
templates_resolvers.go
133 lines (124 loc) · 4 KB
/
templates_resolvers.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
package swagger2gql
import (
"bytes"
"text/template"
"github.com/pkg/errors"
"github.com/EGT-Ukraine/go2gql/generator/plugins/graphql"
)
func (p *Plugin) renderArrayValueResolver(
arg string,
resultGoTyp graphql.GoType,
ctx graphql.BodyContext,
elemResolver graphql.ValueResolver,
elemResolverWithErr bool) (string, error) {
tplBody, err := templatesValue_resolver_arrayGohtmlBytes()
if err != nil {
panic(errors.Wrap(err, "failed to get array value resolver template").Error())
}
tpl, err := template.New("array_value_resolver").Funcs(template.FuncMap{
"errorsPkg": func() string {
return ctx.Importer.New(graphql.ErrorsPkgPath)
},
}).Parse(string(tplBody))
if err != nil {
panic(errors.Wrap(err, "failed to parse array value resolver template"))
}
res := new(bytes.Buffer)
err = tpl.Execute(res, map[string]interface{}{
"resultType": func() string {
return resultGoTyp.String(ctx.Importer)
},
"rootCtx": ctx,
"elemResolver": elemResolver,
"elemResolverWithErr": elemResolverWithErr,
"arg": arg,
})
return res.String(), err
}
func (p *Plugin) renderPtrDatetimeResolver(arg string, ctx graphql.BodyContext) (string, error) {
tplBody, err := templatesValue_resolver_ptr_datetimeGohtmlBytes()
if err != nil {
panic(errors.Wrap(err, "failed to get array value resolver template").Error())
}
tpl, err := template.New("array_value_resolver").Funcs(template.FuncMap{
"strfmtPkg": func() string {
return ctx.Importer.New(strFmtPkg)
},
"errorsPkg": func() string {
return ctx.Importer.New(graphql.ErrorsPkgPath)
},
"timePkg": func() string {
return ctx.Importer.New(timePkg)
},
}).Parse(string(tplBody))
if err != nil {
panic(errors.Wrap(err, "failed to parse array value resolver template"))
}
res := new(bytes.Buffer)
err = tpl.Execute(res, map[string]interface{}{
"arg": arg,
})
return res.String(), err
}
func (p *Plugin) renderDatetimeValueResolverTemplate(arg string, ctx graphql.BodyContext) (string, error) {
tplBody, err := templatesValue_resolver_datetimeGohtmlBytes()
if err != nil {
panic(errors.Wrap(err, "failed to get array value resolver template").Error())
}
tpl, err := template.New("array_value_resolver").Funcs(template.FuncMap{
"strfmtPkg": func() string {
return ctx.Importer.New(strFmtPkg)
},
"errorsPkg": func() string {
return ctx.Importer.New(graphql.ErrorsPkgPath)
},
"timePkg": func() string {
return ctx.Importer.New(timePkg)
},
}).Parse(string(tplBody))
if err != nil {
panic(errors.Wrap(err, "failed to parse array value resolver template"))
}
res := new(bytes.Buffer)
err = tpl.Execute(res, map[string]interface{}{
"arg": arg,
})
return res.String(), err
}
func (p *Plugin) renderMethodCaller(responseType, requestType, requestVar, clientVar, methodName string) (string, error) {
tplBody, err := templatesMethod_callerGohtmlBytes()
if err != nil {
panic(errors.Wrap(err, "failed to get array value resolver template").Error())
}
tpl, err := template.New("array_value_resolver").Parse(string(tplBody))
if err != nil {
panic(errors.Wrap(err, "failed to parse array value resolver template"))
}
res := new(bytes.Buffer)
err = tpl.Execute(res, map[string]interface{}{
"clientVar": clientVar,
"methodName": methodName,
"reqType": requestType,
"reqVar": requestVar,
"respType": responseType,
})
return res.String(), err
}
func (p *Plugin) renderNullMethodCaller(requestType, requestVar, clientVar, methodName string) (string, error) {
tplBody, err := templatesMethod_caller_nullGohtmlBytes()
if err != nil {
panic(errors.Wrap(err, "failed to get array value resolver template").Error())
}
tpl, err := template.New("array_value_resolver").Parse(string(tplBody))
if err != nil {
panic(errors.Wrap(err, "failed to parse array value resolver template"))
}
res := new(bytes.Buffer)
err = tpl.Execute(res, map[string]interface{}{
"clientVar": clientVar,
"methodName": methodName,
"reqType": requestType,
"reqVar": requestVar,
})
return res.String(), err
}