Skip to content

Commit

Permalink
Merge pull request zeromicro#50 from colakuma/main
Browse files Browse the repository at this point in the history
add exampleOption support
  • Loading branch information
MaxToby authored Sep 22, 2022
2 parents fd61f75 + 5f66286 commit 7df4fb1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
9 changes: 5 additions & 4 deletions generate/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ type swaggerParameterObject struct {
CollectionFormat string `json:"collectionFormat,omitempty"`
Default string `json:"default,omitempty"`
MinItems *int `json:"minItems,omitempty"`
Example string `json:"example,omitempty"`

// Or you can explicitly refer to another type. If this is defined all
// other fields should be empty
Expand All @@ -171,10 +172,10 @@ type swaggerParameterObject struct {
// core part of schema, which is common to itemsObject and schemaObject.
// http://swagger.io/specification/#itemsObject
type schemaCore struct {
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Ref string `json:"$ref,omitempty"`
Example json.RawMessage `json:"example,omitempty"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Ref string `json:"$ref,omitempty"`
Example string `json:"example,omitempty"`

Items *swaggerItemsObject `json:"items,omitempty"`
// If the item is an enumeration include a list of all the *NAMES* of the
Expand Down
20 changes: 20 additions & 0 deletions generate/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
omitemptyOption = "omitempty"
optionsOption = "options"
rangeOption = "range"
exampleOption = "example"
optionSeparator = "|"
equalToken = "="
)
Expand Down Expand Up @@ -189,6 +190,13 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge
} else if strings.HasPrefix(option, optionalOption) || strings.HasPrefix(option, omitemptyOption) {
required = false
}

if strings.HasPrefix(option, exampleOption) {
segs := strings.Split(option, equalToken)
if len(segs) == 2 {
sp.Example = segs[1]
}
}
}
sp.Required = required
}
Expand Down Expand Up @@ -358,6 +366,13 @@ func renderStruct(member spec.Member) swaggerParameterObject {
} else if strings.HasPrefix(option, optionalOption) || strings.HasPrefix(option, omitemptyOption) {
required = false
}

if strings.HasPrefix(option, exampleOption) {
segs := strings.Split(option, equalToken)
if len(segs) == 2 {
sp.Example = segs[1]
}
}
}
sp.Required = required
}
Expand Down Expand Up @@ -572,6 +587,11 @@ func schemaOfField(member spec.Member) swaggerSchemaObject {
ret.Maximum = max
}
}
case strings.HasPrefix(option, exampleOption):
segs := strings.Split(option, equalToken)
if len(segs) == 2 {
ret.Example = segs[1]
}
}
}
}
Expand Down

0 comments on commit 7df4fb1

Please sign in to comment.