Skip to content

Commit

Permalink
Start showing parameter defaults and examples in operation usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz committed Aug 11, 2023
1 parent 657000b commit fcdccb0
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
17 changes: 16 additions & 1 deletion cmd/broom/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,24 @@ func operationUsage(op broom.Operation, profile string) {

// prepareParameterDescription prepares a parameter description for display.
//
// Adds default and example values.
// If a description has multiple lines, all lines are indented to match the first line's width.
func prepareParameterDescription(p broom.Parameter) string {
values := make([]string, 0, 2)
if p.Default != nil {
values = append(values, fmt.Sprintf("%v %v", color.YellowString("Default:"), p.Default))
}
if p.Example != nil {
values = append(values, fmt.Sprintf("%v %v", color.YellowString("Example:"), p.Example))
}

description := p.Description
if len(values) > 0 {
description = fmt.Sprintf("%s\n%s", description, strings.Join(values, " "))
}
// Since colors are used for the name column, tabwriter requires color codes to
// be present even when that column is empty, for the tab width to be right.
return strings.ReplaceAll(p.Description, "\n", "\n\t"+color.GreenString("")+"\t")
description = strings.ReplaceAll(description, "\n", "\n\t"+color.GreenString("")+"\t")

return description
}
1 change: 1 addition & 0 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ type Parameter struct {
Style string
Type string
Enum []string
Example any
Default any
Deprecated bool
Required bool
Expand Down
2 changes: 2 additions & 0 deletions spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func newOperationFromSpec(method string, path string, params openapi3.Parameters
Description: Sanitize(schema.Value.Description),
Type: getSchemaType(*schema.Value),
Enum: castEnum(schema.Value.Enum),
Example: schema.Value.Example,
Default: schema.Value.Default,
Deprecated: schema.Value.Deprecated,
Required: required,
Expand All @@ -171,6 +172,7 @@ func newParameterFromSpec(specParam openapi3.Parameter) Parameter {
Style: specParam.Style,
Type: getSchemaType(*specParam.Schema.Value),
Enum: castEnum(specParam.Schema.Value.Enum),
Example: specParam.Schema.Value.Example,
Default: specParam.Schema.Value.Default,
Deprecated: specParam.Deprecated,
Required: specParam.Required,
Expand Down
2 changes: 1 addition & 1 deletion spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestLoadOperations(t *testing.T) {
Name: "price",
Description: "The product price, in cents.",
Type: "integer",
Default: any(float64(1099)),
Example: any(float64(1099)),
Required: true,
},
broom.Parameter{
Expand Down
2 changes: 1 addition & 1 deletion testdata/openapi3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ paths:
price:
type: integer
description: The product price, in cents.
default: 1099
example: 1099
currency_code:
type: string
description: The currency code.
Expand Down
2 changes: 1 addition & 1 deletion testdata/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ paths:
price:
type: integer
description: The product price, in cents.
default: 1099
example: 1099
currency_code:
type: string
description: The currency code.
Expand Down

0 comments on commit fcdccb0

Please sign in to comment.