-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
219 lines (171 loc) · 5.21 KB
/
config.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package proto2gql
import (
"regexp"
"time"
"github.com/pkg/errors"
"github.com/EGT-Ukraine/go2gql/generator/plugins/dataloader"
)
const (
RequestTypeQuery = "QUERY"
RequestTypeMutation = "MUTATION"
)
const (
DataLoaderType1ToN = "1-N"
DataLoaderType1To1 = "1-1"
)
type FieldsConfig struct {
ContextKey string `mapstructure:"context_key"`
}
type MessageConfig struct {
ErrorField string `mapstructure:"error_field"`
Fields map[string]FieldsConfig `mapstructure:"fields"`
DataLoaders []dataloader.FieldConfig `mapstructure:"data_loaders"`
UnwrapField bool `mapstructure:"unwrap_field"`
}
type MethodConfig struct {
Alias string `mapstructure:"alias"`
RequestType string `mapstructure:"request_type"` // QUERY | MUTATION
DataLoaderProvider map[string]DataLoaderConfig `mapstructure:"data_loaders"`
}
type DataLoaderConfig struct {
RequestField string `mapstructure:"request_field"`
ResultField string `mapstructure:"result_field"`
MatchField string `mapstructure:"match_field"`
Type string `mapstructure:"type"`
WaitDuration time.Duration `mapstructure:"wait_duration"`
}
type ServiceConfig struct {
ServiceName string `mapstructure:"service_name"`
Methods map[string]MethodConfig `mapstructure:"methods"`
}
type Config struct {
Files []*ProtoFileConfig `mapstructure:"files"`
// Global configs for proto files
Paths []string `mapstructure:"paths"`
OutputPath string `mapstructure:"output_path"`
ImportsAliases []map[string]string `mapstructure:"imports_aliases"`
Messages []map[string]MessageConfig `mapstructure:"messages"`
}
func (c *Config) GetOutputPath() string {
if c == nil {
return ""
}
return c.OutputPath
}
type ProtoFileConfig struct {
Name string `mapstructure:"name"`
Paths []string `mapstructure:"paths"`
ImportsAliases []map[string]string `mapstructure:"imports_aliases"`
ProtoPath string `mapstructure:"proto_path"`
OutputPkg string `mapstructure:"output_package"`
OutputPath string `mapstructure:"output_path"`
ProtoGoPackage string `mapstructure:"proto_go_package"` // go package of protoc generated code
GQLEnumsPrefix string `mapstructure:"gql_enums_prefix"`
GQLMessagePrefix string `mapstructure:"gql_messages_prefix"`
Services map[string]ServiceConfig `mapstructure:"services"`
Messages []map[string]MessageConfig `mapstructure:"messages"`
}
func (pc *ProtoFileConfig) MessageConfig(msgName string) (MessageConfig, error) {
if pc == nil {
return MessageConfig{}, nil
}
for _, cfgs := range pc.Messages {
for msgNameRegex, cfg := range cfgs {
r, err := regexp.Compile(msgNameRegex)
if err != nil {
return MessageConfig{}, errors.Wrapf(err, "failed to compile message name regex '%s'", msgNameRegex)
}
if r.MatchString(msgName) {
return cfg, nil
}
}
}
return MessageConfig{}, nil
}
func (pc *ProtoFileConfig) GetName() string {
if pc == nil {
return ""
}
return pc.Name
}
func (pc *ProtoFileConfig) GetPaths() []string {
if pc == nil {
return []string{}
}
return pc.Paths
}
func (pc *ProtoFileConfig) GetProtoPath() string {
if pc == nil {
return ""
}
return pc.ProtoPath
}
func (pc *ProtoFileConfig) GetOutputPkg() string {
if pc == nil {
return ""
}
return pc.OutputPkg
}
func (pc *ProtoFileConfig) GetGoPackage() string {
if pc == nil {
return ""
}
return pc.ProtoGoPackage
}
func (pc *ProtoFileConfig) GetOutputPath() string {
if pc == nil {
return ""
}
return pc.OutputPath
}
func (pc *ProtoFileConfig) GetGQLEnumsPrefix() string {
if pc == nil {
return ""
}
return pc.GQLEnumsPrefix
}
func (pc *ProtoFileConfig) GetGQLMessagePrefix() string {
if pc == nil {
return ""
}
return pc.GQLMessagePrefix
}
func (pc *ProtoFileConfig) GetImportsAliases() []map[string]string {
if pc == nil {
return []map[string]string{}
}
return pc.ImportsAliases
}
func (pc *ProtoFileConfig) GetServices() map[string]ServiceConfig {
if pc == nil {
return map[string]ServiceConfig{}
}
return pc.Services
}
func (pc *ProtoFileConfig) GetMessages() []map[string]MessageConfig {
if pc == nil {
return []map[string]MessageConfig{}
}
return pc.Messages
}
type SchemaNodeConfig struct {
Type string `mapstructure:"type"` // "OBJECT|SERVICE"
Proto string `mapstructure:"proto"`
Service string `mapstructure:"service"`
ObjectName string `mapstructure:"object_name"`
Field string `mapstructure:"field"`
Fields []SchemaNodeConfig `mapstructure:"fields"`
ExcludeMethods []string `mapstructure:"exclude_methods"`
FilterMethods []string `mapstructure:"filter_methods"`
}
type SchemaConfig struct {
Name string `mapstructure:"name"`
OutputPath string `mapstructure:"output_path"`
OutputPackage string `mapstructure:"output_package"`
Queries *SchemaNodeConfig `mapstructure:"queries"`
Mutations *SchemaNodeConfig `mapstructure:"mutations"`
}
type GenerateConfig struct {
Tracer bool
VendorPath string
}