Skip to content

Commit

Permalink
[params] enumify supported OpenAPI types
Browse files Browse the repository at this point in the history
  • Loading branch information
lispyclouds committed Dec 15, 2024
1 parent 7919589 commit 02fe5ad
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,34 @@ import (
"gopkg.in/yaml.v3"
)

// Currently supported OpenAPI types
type OpenAPIType string

const (
String OpenAPIType = "string"
Number OpenAPIType = "number"
Integer OpenAPIType = "integer"
Boolean OpenAPIType = "boolean"
)

// Metadata for all parameters
type ParamMeta struct {
Name string
Type string // Same as the type name in OpenAPI
Type OpenAPIType
}

// Data passed into each handler
type HandlerData struct {
Method string // the HTTP method
Path string // the parameterised path
Path string // the parameterised path. currently non interpolated
PathParams []ParamMeta // List of path params
QueryParams []ParamMeta // List of query params
HeaderParams []ParamMeta // List of header params
CookieParams []ParamMeta // List of cookie params
RequestBodyParam ParamMeta // The request body
}

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

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

for _, param := range op.Parameters {
schema := param.Schema.Schema()
t := "string"
t := String
if schema != nil {
t = schema.Type[0]
t = OpenAPIType(schema.Type[0])
}

switch t {
case "string":
case String:
flags.String(param.Name, "", param.Description)
case "integer":
case Integer:
flags.Int(param.Name, 0, param.Description)
case "number":
case Number:
flags.Float64(param.Name, 0.0, param.Description)
case "boolean":
case Boolean:
flags.Bool(param.Name, false, param.Description)
default:
// TODO: array, object
Expand Down Expand Up @@ -132,7 +145,7 @@ func addRequestBody(cmd *cobra.Command, op *v3.Operation, handlerData *HandlerDa
// TODO: Handle all the different MIME types and schemas from body.Content
// maybe assert the shape if mime is json and schema is an object
// Treats all request body content as a string as of now
handlerData.RequestBodyParam = ParamMeta{Name: paramName, Type: "string"}
handlerData.RequestBodyParam = ParamMeta{Name: paramName, Type: String}
cmd.Flags().String(paramName, "", body.Description)

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

0 comments on commit 02fe5ad

Please sign in to comment.