Skip to content

Commit 02fe5ad

Browse files
committed
[params] enumify supported OpenAPI types
1 parent 7919589 commit 02fe5ad

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

lib.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,34 @@ import (
1212
"gopkg.in/yaml.v3"
1313
)
1414

15+
// Currently supported OpenAPI types
16+
type OpenAPIType string
17+
18+
const (
19+
String OpenAPIType = "string"
20+
Number OpenAPIType = "number"
21+
Integer OpenAPIType = "integer"
22+
Boolean OpenAPIType = "boolean"
23+
)
24+
25+
// Metadata for all parameters
1526
type ParamMeta struct {
1627
Name string
17-
Type string // Same as the type name in OpenAPI
28+
Type OpenAPIType
1829
}
1930

31+
// Data passed into each handler
2032
type HandlerData struct {
2133
Method string // the HTTP method
22-
Path string // the parameterised path
34+
Path string // the parameterised path. currently non interpolated
2335
PathParams []ParamMeta // List of path params
2436
QueryParams []ParamMeta // List of query params
2537
HeaderParams []ParamMeta // List of header params
2638
CookieParams []ParamMeta // List of cookie params
2739
RequestBodyParam ParamMeta // The request body
2840
}
2941

42+
// The handler signature
3043
type Handler func(opts *cobra.Command, args []string, data HandlerData)
3144

3245
type extensions struct {
@@ -73,19 +86,19 @@ func addParams(cmd *cobra.Command, op *v3.Operation, handlerData *HandlerData) {
7386

7487
for _, param := range op.Parameters {
7588
schema := param.Schema.Schema()
76-
t := "string"
89+
t := String
7790
if schema != nil {
78-
t = schema.Type[0]
91+
t = OpenAPIType(schema.Type[0])
7992
}
8093

8194
switch t {
82-
case "string":
95+
case String:
8396
flags.String(param.Name, "", param.Description)
84-
case "integer":
97+
case Integer:
8598
flags.Int(param.Name, 0, param.Description)
86-
case "number":
99+
case Number:
87100
flags.Float64(param.Name, 0.0, param.Description)
88-
case "boolean":
101+
case Boolean:
89102
flags.Bool(param.Name, false, param.Description)
90103
default:
91104
// TODO: array, object
@@ -132,7 +145,7 @@ func addRequestBody(cmd *cobra.Command, op *v3.Operation, handlerData *HandlerDa
132145
// TODO: Handle all the different MIME types and schemas from body.Content
133146
// maybe assert the shape if mime is json and schema is an object
134147
// Treats all request body content as a string as of now
135-
handlerData.RequestBodyParam = ParamMeta{Name: paramName, Type: "string"}
148+
handlerData.RequestBodyParam = ParamMeta{Name: paramName, Type: String}
136149
cmd.Flags().String(paramName, "", body.Description)
137150

138151
if req := body.Required; req != nil && *req {

0 commit comments

Comments
 (0)