[Vendor] update go-swagger v0.21.0 -> v0.25.0 (#12670)

* Update go-swagger

* vendor
This commit is contained in:
6543 2020-09-01 16:01:23 +02:00 committed by GitHub
parent 66843f2237
commit 3270e7a443
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
350 changed files with 26353 additions and 5552 deletions

83
vendor/github.com/go-swagger/go-swagger/scan/enum.go generated vendored Normal file
View file

@ -0,0 +1,83 @@
// +build !go1.11
package scan
import (
"go/ast"
"strconv"
"strings"
"unicode"
)
func upperSnakeCase(s string) string {
in := []rune(s)
isLower := func(idx int) bool {
return idx >= 0 && idx < len(in) && unicode.IsLower(in[idx])
}
out := make([]rune, 0, len(in)+len(in)/2)
for i, r := range in {
if unicode.IsUpper(r) {
r = unicode.ToLower(r)
if i > 0 && in[i-1] != '_' && (isLower(i-1) || isLower(i+1)) {
out = append(out, '_')
}
}
out = append(out, r)
}
return strings.ToUpper(string(out))
}
func getEnumBasicLitValue(basicLit *ast.BasicLit) interface{} {
switch basicLit.Kind.String() {
case "INT":
if result, err := strconv.ParseInt(basicLit.Value, 10, 64); err == nil {
return result
}
case "FLOAT":
if result, err := strconv.ParseFloat(basicLit.Value, 64); err == nil {
return result
}
default:
return strings.Trim(basicLit.Value, "\"")
}
return nil
}
func getEnumValues(file *ast.File, typeName string) (list []interface{}) {
for _, decl := range file.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
if genDecl.Tok.String() == "const" {
for _, spec := range genDecl.Specs {
if valueSpec, ok := spec.(*ast.ValueSpec); ok {
switch valueSpec.Type.(type) {
case *ast.Ident:
if valueSpec.Type.(*ast.Ident).Name == typeName {
if basicLit, ok := valueSpec.Values[0].(*ast.BasicLit); ok {
list = append(list, getEnumBasicLitValue(basicLit))
}
}
default:
var name = valueSpec.Names[0].Name
if strings.HasPrefix(name, upperSnakeCase(typeName)) {
var values = strings.SplitN(name, "__", 2)
if len(values) == 2 {
list = append(list, values[1])
}
}
}
}
}
}
}
return
}

View file

@ -40,6 +40,10 @@ func (pt paramTypable) Typed(tpe, format string) {
pt.param.Typed(tpe, format)
}
func (pt paramTypable) WithEnum(values ...interface{}) {
pt.param.WithEnum(values...)
}
func (pt paramTypable) SetRef(ref spec.Ref) {
pt.param.Ref = ref
}
@ -83,6 +87,10 @@ func (pt itemsTypable) SetRef(ref spec.Ref) {
pt.items.Ref = ref
}
func (pt itemsTypable) WithEnum(values ...interface{}) {
pt.items.WithEnum(values...)
}
func (pt itemsTypable) Schema() *spec.Schema {
return nil
}

View file

@ -38,6 +38,10 @@ func (ht responseTypable) Typed(tpe, format string) {
ht.header.Typed(tpe, format)
}
func (ht responseTypable) WithEnum(values ...interface{}) {
ht.header.WithEnum(values)
}
func bodyTypable(in string, schema *spec.Schema) (swaggerTypable, *spec.Schema) {
if in == "body" {
// get the schema for items on the schema property
@ -85,6 +89,7 @@ func (ht responseTypable) Schema() *spec.Schema {
func (ht responseTypable) SetSchema(schema *spec.Schema) {
ht.response.Schema = schema
}
func (ht responseTypable) CollectionOf(items *spec.Items, format string) {
ht.header.CollectionOf(items, format)
}

View file

@ -471,6 +471,7 @@ type swaggerTypable interface {
Typed(string, string)
SetRef(spec.Ref)
Items() swaggerTypable
WithEnum(...interface{})
Schema() *spec.Schema
Level() int
}

View file

@ -81,8 +81,13 @@ func (st schemaTypable) AdditionalProperties() swaggerTypable {
st.schema.Typed("object", "")
return schemaTypable{st.schema.AdditionalProperties.Schema, st.level + 1}
}
func (st schemaTypable) Level() int { return st.level }
func (st schemaTypable) WithEnum(values ...interface{}) {
st.schema.WithEnum(values...)
}
type schemaValidations struct {
current *spec.Schema
}
@ -248,6 +253,18 @@ func (scp *schemaParser) parseDecl(definitions map[string]spec.Schema, decl *sch
return err
}
}
if enumName, ok := enumName(decl.Decl.Doc); ok {
var enumValues = getEnumValues(decl.File, enumName)
if len(enumValues) > 0 {
var typeName = reflect.TypeOf(enumValues[0]).String()
prop.WithEnum(enumValues...)
err := swaggerSchemaForType(typeName, prop)
if err != nil {
return fmt.Errorf("file %s, error is: %v", decl.File.Name, err)
}
}
}
case *ast.SelectorExpr:
prop := &schemaTypable{schPtr, 0}
if strfmtName, ok := strfmtName(decl.Decl.Doc); ok {
@ -1025,8 +1042,15 @@ func (scp *schemaParser) parseIdentProperty(pkg *loader.PackageInfo, expr *ast.I
}
if enumName, ok := enumName(gd.Doc); ok {
log.Println(enumName)
return nil
var enumValues = getEnumValues(file, enumName)
if len(enumValues) > 0 {
prop.WithEnum(enumValues...)
var typeName = reflect.TypeOf(enumValues[0]).String()
err := swaggerSchemaForType(typeName, prop)
if err != nil {
return fmt.Errorf("file %s, error is: %v", file.Name, err)
}
}
}
if defaultName, ok := defaultName(gd.Doc); ok {