-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathparse.go
254 lines (204 loc) · 7.13 KB
/
parse.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package config
import (
"errors"
"fmt"
"regexp"
"gopkg.in/yaml.v3"
)
// This regex matches attribute locations, dot-separated, as represented as {attribute_name}.{nested_attribute_name}
// - category = MATCH
// - category.id = MATCH
// - category.tags.name = MATCH
// - category. = NO MATCH
// - .category = NO MATCH
var attributeLocationRegex = regexp.MustCompile(`^[\w]+(?:\.[\w]+)*$`)
// Config represents a YAML generator config.
type Config struct {
Provider Provider `yaml:"provider"`
Resources map[string]Resource `yaml:"resources"`
DataSources map[string]DataSource `yaml:"data_sources"`
}
// Provider generator config section.
type Provider struct {
Name string `yaml:"name"`
SchemaRef string `yaml:"schema_ref"`
// TODO: At some point, this should probably be refactored to work with the SchemaOptions struct
// Ignores are a slice of strings, representing an attribute location to ignore during mapping (dot-separated for nested attributes).
Ignores []string `yaml:"ignores"`
}
// Resource generator config section.
type Resource struct {
Create *OpenApiSpecLocation `yaml:"create"`
Read *OpenApiSpecLocation `yaml:"read"`
Update *OpenApiSpecLocation `yaml:"update"`
Delete *OpenApiSpecLocation `yaml:"delete"`
SchemaOptions SchemaOptions `yaml:"schema"`
}
// DataSource generator config section.
type DataSource struct {
Read *OpenApiSpecLocation `yaml:"read"`
SchemaOptions SchemaOptions `yaml:"schema"`
}
// OpenApiSpecLocation defines a location in an OpenAPI spec for an API operation.
type OpenApiSpecLocation struct {
// Matches the path key for a path item (refer to [OAS Paths Object]).
//
// [OAS Paths Object]: https://spec.openapis.org/oas/v3.1.0#paths-object
Path string `yaml:"path"`
// Matches the operation method in a path item: GET, POST, etc (refer to [OAS Path Item Object]).
//
// [OAS Path Item Object]: https://spec.openapis.org/oas/v3.1.0#pathItemObject
Method string `yaml:"method"`
}
// SchemaOptions generator config section. This section contains options for modifying the output of the generator.
type SchemaOptions struct {
// Ignores are a slice of strings, representing an attribute location to ignore during mapping (dot-separated for nested attributes).
Ignores []string `yaml:"ignores"`
AttributeOptions AttributeOptions `yaml:"attributes"`
}
// AttributeOptions generator config section. This section is used to modify the output of specific attributes.
type AttributeOptions struct {
// Aliases are a map, with the key being a parameter name in an OpenAPI operation and the value being the new name (alias).
Aliases map[string]string `yaml:"aliases"`
// Overrides are a map, with the key being an attribute location (dot-separated for nested attributes) and the value being overrides to apply to the attribute.
Overrides map[string]Override `yaml:"overrides"`
}
// Override generator config section.
type Override struct {
// Description overrides the description that was mapped/merged from the OpenAPI specification.
Description string `yaml:"description"`
// ComputedOptionalRequired overrides the inferred value from the OpenAPI specification.
ComputedOptionalRequired string `yaml:"computed_optional_required"`
}
// ParseConfig takes in a byte array (of YAML), unmarshals into a Config struct, and validates the result
func ParseConfig(bytes []byte) (*Config, error) {
var result Config
err := yaml.Unmarshal(bytes, &result)
if err != nil {
return nil, fmt.Errorf("error unmarshaling config: %w", err)
}
if err = result.Validate(); err != nil {
return nil, fmt.Errorf("config validation error(s):\n%w", err)
}
return &result, nil
}
func (c Config) Validate() error {
var result error
if len(c.DataSources) == 0 && len(c.Resources) == 0 {
result = errors.Join(result, errors.New("\tat least one object is required in either 'resources' or 'data_sources'"))
}
// Validate Provider
err := c.Provider.Validate()
if err != nil {
result = errors.Join(result, fmt.Errorf("\tprovider %w", err))
}
// Validate all Resources
for name, resource := range c.Resources {
err := resource.Validate()
if err != nil {
result = errors.Join(result, fmt.Errorf("\tresource '%s' %w", name, err))
}
}
// Validate all Data Sources
for name, dataSource := range c.DataSources {
err := dataSource.Validate()
if err != nil {
result = errors.Join(result, fmt.Errorf("\tdata_source '%s' %w", name, err))
}
}
return result
}
func (p Provider) Validate() error {
var result error
if p.Name == "" {
result = errors.Join(result, errors.New("must have a 'name' property"))
}
for _, ignore := range p.Ignores {
if !attributeLocationRegex.MatchString(ignore) {
result = errors.Join(result, fmt.Errorf("invalid item for ignores: %q - must be dot-separated string", ignore))
}
}
return result
}
func (r Resource) Validate() error {
var result error
if r.Create == nil {
result = errors.Join(result, errors.New("must have a create object"))
}
if r.Read == nil {
result = errors.Join(result, errors.New("must have a read object"))
}
err := r.Create.Validate()
if err != nil {
result = errors.Join(result, fmt.Errorf("invalid create: %w", err))
}
err = r.Read.Validate()
if err != nil {
result = errors.Join(result, fmt.Errorf("invalid read: %w", err))
}
err = r.Update.Validate()
if err != nil {
result = errors.Join(result, fmt.Errorf("invalid update: %w", err))
}
err = r.Delete.Validate()
if err != nil {
result = errors.Join(result, fmt.Errorf("invalid delete: %w", err))
}
err = r.SchemaOptions.Validate()
if err != nil {
result = errors.Join(result, fmt.Errorf("invalid schema: %w", err))
}
return result
}
func (d DataSource) Validate() error {
var result error
if d.Read == nil {
result = errors.Join(result, errors.New("must have a read object"))
}
err := d.Read.Validate()
if err != nil {
result = errors.Join(result, fmt.Errorf("invalid read: %w", err))
}
err = d.SchemaOptions.Validate()
if err != nil {
result = errors.Join(result, fmt.Errorf("invalid schema: %w", err))
}
return result
}
func (o *OpenApiSpecLocation) Validate() error {
var result error
if o == nil {
return nil
}
if o.Path == "" {
result = errors.Join(result, errors.New("'path' property is required"))
}
if o.Method == "" {
result = errors.Join(result, errors.New("'method' property is required"))
}
return result
}
func (s *SchemaOptions) Validate() error {
var result error
err := s.AttributeOptions.Validate()
if err != nil {
result = errors.Join(result, fmt.Errorf("invalid attributes: %w", err))
}
for _, ignore := range s.Ignores {
if !attributeLocationRegex.MatchString(ignore) {
result = errors.Join(result, fmt.Errorf("invalid item for ignores: %q - must be dot-separated string", ignore))
}
}
return result
}
func (s *AttributeOptions) Validate() error {
var result error
for path := range s.Overrides {
if !attributeLocationRegex.MatchString(path) {
result = errors.Join(result, fmt.Errorf("invalid key for override: %q - must be dot-separated string", path))
}
}
return result
}