代码拉取完成,页面将自动刷新
package swag
import (
"fmt"
"github.com/go-openapi/spec"
"go/ast"
"strings"
)
const (
//ARRAY array
ARRAY = "array"
//OBJECT object
OBJECT = "object"
//PRIMITIVE primitive
PRIMITIVE = "primitive"
//BOOLEAN boolean
BOOLEAN = "boolean"
//INTEGER integer
INTEGER = "integer"
//NUMBER number
NUMBER = "number"
//STRING string
STRING = "string"
//FUNC func
FUNC = "func"
)
// CheckSchemaType checks if typeName is not a name of primitive type
func CheckSchemaType(typeName string) error {
if !IsPrimitiveType(typeName) {
return fmt.Errorf("%s is not basic types", typeName)
}
return nil
}
// IsSimplePrimitiveType determine whether the type name is a simple primitive type
func IsSimplePrimitiveType(typeName string) bool {
switch typeName {
case STRING, NUMBER, INTEGER, BOOLEAN:
return true
default:
return false
}
}
// IsPrimitiveType determine whether the type name is a primitive type
func IsPrimitiveType(typeName string) bool {
switch typeName {
case STRING, NUMBER, INTEGER, BOOLEAN, ARRAY, OBJECT, FUNC:
return true
default:
return false
}
}
// IsNumericType determines whether the swagger type name is a numeric type
func IsNumericType(typeName string) bool {
return typeName == INTEGER || typeName == NUMBER
}
// TransToValidSchemeType indicates type will transfer golang basic type to swagger supported type.
func TransToValidSchemeType(typeName string) string {
switch typeName {
case "uint", "int", "uint8", "int8", "uint16", "int16", "byte":
return INTEGER
case "uint32", "int32", "rune":
return INTEGER
case "uint64", "int64":
return INTEGER
case "float32", "float64":
return NUMBER
case "bool":
return BOOLEAN
case "string":
return STRING
default:
return typeName // to support user defined types
}
}
// IsGolangPrimitiveType determine whether the type name is a golang primitive type
func IsGolangPrimitiveType(typeName string) bool {
switch typeName {
case "uint",
"int",
"uint8",
"int8",
"uint16",
"int16",
"byte",
"uint32",
"int32",
"rune",
"uint64",
"int64",
"float32",
"float64",
"bool",
"string":
return true
default:
return false
}
}
// TransToValidCollectionFormat determine valid collection format
func TransToValidCollectionFormat(format string) string {
switch format {
case "csv", "multi", "pipes", "tsv", "ssv":
return format
default:
return ""
}
}
// TypeDocName get alias from comment '// @name ', otherwise the original type name to display in doc
func TypeDocName(pkgName string, spec *ast.TypeSpec) string {
if spec != nil {
if spec.Comment != nil {
for _, comment := range spec.Comment.List {
text := strings.TrimSpace(comment.Text)
text = strings.TrimLeft(text, "//")
text = strings.TrimSpace(text)
texts := strings.Split(text, " ")
if len(texts) > 1 && strings.ToLower(texts[0]) == "@name" {
return texts[1]
}
}
}
if spec.Name != nil {
return fullTypeName(strings.Split(pkgName, ".")[0], spec.Name.Name)
}
}
return pkgName
}
//RefSchema build a reference schema
func RefSchema(refType string) *spec.Schema {
return spec.RefSchema("#/definitions/" + refType)
}
//PrimitiveSchema build a primitive schema
func PrimitiveSchema(refType string) *spec.Schema {
return &spec.Schema{SchemaProps: spec.SchemaProps{Type: []string{refType}}}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。