@@ -22,10 +22,10 @@ package main
22
22
import (
23
23
"bytes"
24
24
"encoding/json"
25
- "errors"
26
25
"fmt"
27
26
"os"
28
27
"regexp"
28
+ "slices"
29
29
"sort"
30
30
"strings"
31
31
@@ -76,8 +76,19 @@ func main() {
76
76
os .Exit (1 )
77
77
}
78
78
79
- endpoint := spec .(map [string ]interface {})["paths" ].(map [string ]interface {})[endpointPath ].(map [string ]interface {})
80
- schema := endpoint ["put" ].(map [string ]interface {})["requestBody" ].(map [string ]interface {})["content" ].(map [string ]interface {})["application/json" ].(map [string ]interface {})
79
+ parts := strings .Split (endpointPath , "/" )
80
+ if len (parts ) > 0 {
81
+ parts = parts [:len (parts )- 1 ]
82
+ }
83
+ shortEndpointPath := strings .Join (parts , "/" )
84
+ var schema map [string ]interface {}
85
+ paths := spec .(map [string ]interface {})["paths" ].(map [string ]interface {})
86
+ // use POST schema if it exists, otherwise fall back to PUT schema
87
+ if endpoint , ok := paths [shortEndpointPath ].(map [string ]interface {})["post" ]; ok {
88
+ schema = endpoint .(map [string ]interface {})["requestBody" ].(map [string ]interface {})["content" ].(map [string ]interface {})["application/json" ].(map [string ]interface {})
89
+ } else {
90
+ schema = paths [endpointPath ].(map [string ]interface {})["put" ].(map [string ]interface {})["requestBody" ].(map [string ]interface {})["content" ].(map [string ]interface {})["application/json" ].(map [string ]interface {})
91
+ }
81
92
example := schema ["schema" ].(map [string ]interface {})["example" ].(map [string ]interface {})
82
93
exampleStr , err := json .Marshal (& example )
83
94
if err != nil {
@@ -95,14 +106,6 @@ func main() {
95
106
config .IdName = urlResult .idName [1 : len (urlResult .idName )- 1 ]
96
107
}
97
108
config .DocCategory = urlResult .category
98
- dataSourceNameQuery := false
99
- for _ , a := range config .Attributes {
100
- if a .ModelName == "name" {
101
- dataSourceNameQuery = true
102
- break
103
- }
104
- }
105
- config .DataSourceNameQuery = dataSourceNameQuery
106
109
config .Name = resourceName
107
110
if urlResult .oneToOne {
108
111
config .PutCreate = true
@@ -123,21 +126,52 @@ func main() {
123
126
attr .Example = "<<Example>>"
124
127
attributes = append (attributes , attr )
125
128
}
126
- attributes = append (attributes , traverseProperties (schema ["schema" ].(map [string ]interface {})["properties" ].(map [string ]interface {}), []string {}, "" , string (exampleStr ))... )
129
+ required := []string {}
130
+ if r , ok := schema ["schema" ].(map [string ]interface {})["required" ]; ok {
131
+ required = toStringSlice (r .([]interface {}))
132
+ }
133
+ attributes = append (attributes , traverseProperties (schema ["schema" ].(map [string ]interface {})["properties" ].(map [string ]interface {}), []string {}, "" , string (exampleStr ), required )... )
127
134
config .Attributes = attributes
128
135
136
+ dataSourceNameQuery := false
137
+ for _ , a := range config .Attributes {
138
+ if a .ModelName == "name" {
139
+ dataSourceNameQuery = true
140
+ break
141
+ }
142
+ }
143
+ config .DataSourceNameQuery = dataSourceNameQuery
144
+
145
+ outputPath := definitionsPath + yamlconfig .SnakeCase (resourceName ) + ".yaml"
146
+
147
+ existingConfig := yamlconfig.YamlConfig {}
148
+ if yamlFile , err := os .ReadFile (outputPath ); err == nil {
149
+ existingConfig = yamlconfig.YamlConfig {}
150
+ err = yaml .Unmarshal (yamlFile , & existingConfig )
151
+ if err != nil {
152
+ panic (err )
153
+ }
154
+ }
155
+
156
+ newConfig := yamlconfig .MergeYamlConfig (config , existingConfig )
157
+
129
158
var yamlBytes bytes.Buffer
130
159
yamlEncoder := yaml .NewEncoder (& yamlBytes )
131
160
yamlEncoder .SetIndent (2 )
132
- err = yamlEncoder .Encode (& config )
161
+ err = yamlEncoder .Encode (& newConfig )
133
162
if err != nil {
134
163
panic (err )
135
164
}
136
- outputPath := definitionsPath + yamlconfig .SnakeCase (resourceName ) + ".yaml"
137
- // Only write the file if it doesn't exist
138
- if _ , err := os .Stat (outputPath ); errors .Is (err , os .ErrNotExist ) {
139
- os .WriteFile (outputPath , []byte ("---\n " + string (yamlBytes .Bytes ())), 0644 )
165
+
166
+ os .WriteFile (outputPath , yamlBytes .Bytes (), 0644 )
167
+ }
168
+
169
+ func toStringSlice (i []interface {}) []string {
170
+ ret := []string {}
171
+ for _ , v := range i {
172
+ ret = append (ret , v .(string ))
140
173
}
174
+ return ret
141
175
}
142
176
143
177
var jsonTypes = map [string ]string {
@@ -176,7 +210,7 @@ func parseUrl(url string) parseUrlResult {
176
210
return ret
177
211
}
178
212
179
- func traverseProperties (m map [string ]interface {}, path []string , gjsonPath string , exampleStr string ) []yamlconfig.YamlConfigAttribute {
213
+ func traverseProperties (m map [string ]interface {}, path []string , gjsonPath string , exampleStr string , requiredProperties [] string ) []yamlconfig.YamlConfigAttribute {
180
214
ret := []yamlconfig.YamlConfigAttribute {}
181
215
182
216
keys := maps .Keys (m )
@@ -215,6 +249,9 @@ func traverseProperties(m map[string]interface{}, path []string, gjsonPath strin
215
249
attr .MaxFloat = max .(float64 )
216
250
}
217
251
}
252
+ if slices .Contains (requiredProperties , propName ) {
253
+ attr .Mandatory = true
254
+ }
218
255
ret = append (ret , attr )
219
256
}
220
257
}
@@ -223,7 +260,11 @@ func traverseProperties(m map[string]interface{}, path []string, gjsonPath strin
223
260
if propMap ["type" ] == "object" {
224
261
childPath := append (path , propName )
225
262
childGjsonPath := gjsonPath + "." + propName
226
- children := traverseProperties (propMap ["properties" ].(map [string ]interface {}), childPath , childGjsonPath , exampleStr )
263
+ childRequired := []string {}
264
+ if rp , ok := propMap ["required" ]; ok {
265
+ childRequired = toStringSlice (rp .([]interface {}))
266
+ }
267
+ children := traverseProperties (propMap ["properties" ].(map [string ]interface {}), childPath , childGjsonPath , exampleStr , childRequired )
227
268
ret = append (ret , children ... )
228
269
}
229
270
}
@@ -245,7 +286,11 @@ func traverseProperties(m map[string]interface{}, path []string, gjsonPath strin
245
286
attr .Example = res .String ()
246
287
} else if items ["type" ].(string ) == "object" {
247
288
childGjsonPath := gjsonPath + "." + propName + ".0"
248
- children := traverseProperties (items ["properties" ].(map [string ]interface {}), []string {}, childGjsonPath , exampleStr )
289
+ childRequired := []string {}
290
+ if rp , ok := items ["required" ]; ok {
291
+ childRequired = toStringSlice (rp .([]interface {}))
292
+ }
293
+ children := traverseProperties (items ["properties" ].(map [string ]interface {}), []string {}, childGjsonPath , exampleStr , childRequired )
249
294
attr .Attributes = children
250
295
}
251
296
ret = append (ret , attr )
0 commit comments