forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/swaggo/swag/schema.go

516 lines
6.6 KiB
Go
Raw Normal View History

2024-05-14 13:07:09 +00:00
package swag
import (
"errors"
"fmt"
2024-06-14 08:41:36 +00:00
2024-05-14 13:07:09 +00:00
"github.com/go-openapi/spec"
)
const (
2024-05-14 13:07:09 +00:00
// ARRAY represent a array value.
2024-05-14 13:07:09 +00:00
ARRAY = "array"
2024-05-14 13:07:09 +00:00
// OBJECT represent a object value.
2024-05-14 13:07:09 +00:00
OBJECT = "object"
2024-05-14 13:07:09 +00:00
// PRIMITIVE represent a primitive value.
2024-05-14 13:07:09 +00:00
PRIMITIVE = "primitive"
2024-05-14 13:07:09 +00:00
// BOOLEAN represent a boolean value.
2024-05-14 13:07:09 +00:00
BOOLEAN = "boolean"
2024-05-14 13:07:09 +00:00
// INTEGER represent a integer value.
2024-05-14 13:07:09 +00:00
INTEGER = "integer"
2024-05-14 13:07:09 +00:00
// NUMBER represent a number value.
2024-05-14 13:07:09 +00:00
NUMBER = "number"
2024-05-14 13:07:09 +00:00
// STRING represent a string value.
2024-05-14 13:07:09 +00:00
STRING = "string"
2024-05-14 13:07:09 +00:00
// FUNC represent a function value.
2024-05-14 13:07:09 +00:00
FUNC = "func"
2024-05-14 13:07:09 +00:00
// ERROR represent a error value.
2024-05-14 13:07:09 +00:00
ERROR = "error"
2024-05-14 13:07:09 +00:00
// INTERFACE represent a interface value.
2024-05-14 13:07:09 +00:00
INTERFACE = "interface{}"
2024-05-14 13:07:09 +00:00
// ANY represent a any value.
2024-05-14 13:07:09 +00:00
ANY = "any"
2024-05-14 13:07:09 +00:00
// NIL represent a empty value.
2024-05-14 13:07:09 +00:00
NIL = "nil"
// IgnoreNameOverridePrefix Prepend to model to avoid renaming based on comment.
2024-05-14 13:07:09 +00:00
IgnoreNameOverridePrefix = '$'
)
// CheckSchemaType checks if typeName is not a name of primitive type.
2024-05-14 13:07:09 +00:00
func CheckSchemaType(typeName string) error {
2024-05-14 13:07:09 +00:00
if !IsPrimitiveType(typeName) {
2024-05-14 13:07:09 +00:00
return fmt.Errorf("%s is not basic types", typeName)
2024-05-14 13:07:09 +00:00
}
return nil
2024-05-14 13:07:09 +00:00
}
// IsSimplePrimitiveType determine whether the type name is a simple primitive type.
2024-05-14 13:07:09 +00:00
func IsSimplePrimitiveType(typeName string) bool {
2024-05-14 13:07:09 +00:00
switch typeName {
2024-05-14 13:07:09 +00:00
case STRING, NUMBER, INTEGER, BOOLEAN:
2024-05-14 13:07:09 +00:00
return true
2024-05-14 13:07:09 +00:00
}
return false
2024-05-14 13:07:09 +00:00
}
// IsPrimitiveType determine whether the type name is a primitive type.
2024-05-14 13:07:09 +00:00
func IsPrimitiveType(typeName string) bool {
2024-05-14 13:07:09 +00:00
switch typeName {
2024-05-14 13:07:09 +00:00
case STRING, NUMBER, INTEGER, BOOLEAN, ARRAY, OBJECT, FUNC:
2024-05-14 13:07:09 +00:00
return true
2024-05-14 13:07:09 +00:00
}
return false
2024-05-14 13:07:09 +00:00
}
// IsInterfaceLike determines whether the swagger type name is an go named interface type like error type.
2024-05-14 13:07:09 +00:00
func IsInterfaceLike(typeName string) bool {
2024-05-14 13:07:09 +00:00
return typeName == ERROR || typeName == ANY
2024-05-14 13:07:09 +00:00
}
// IsNumericType determines whether the swagger type name is a numeric type.
2024-05-14 13:07:09 +00:00
func IsNumericType(typeName string) bool {
2024-05-14 13:07:09 +00:00
return typeName == INTEGER || typeName == NUMBER
2024-05-14 13:07:09 +00:00
}
// TransToValidSchemeType indicates type will transfer golang basic type to swagger supported type.
2024-05-14 13:07:09 +00:00
func TransToValidSchemeType(typeName string) string {
2024-05-14 13:07:09 +00:00
switch typeName {
2024-05-14 13:07:09 +00:00
case "uint", "int", "uint8", "int8", "uint16", "int16", "byte":
2024-05-14 13:07:09 +00:00
return INTEGER
2024-05-14 13:07:09 +00:00
case "uint32", "int32", "rune":
2024-05-14 13:07:09 +00:00
return INTEGER
2024-05-14 13:07:09 +00:00
case "uint64", "int64":
2024-05-14 13:07:09 +00:00
return INTEGER
2024-05-14 13:07:09 +00:00
case "float32", "float64":
2024-05-14 13:07:09 +00:00
return NUMBER
2024-05-14 13:07:09 +00:00
case "bool":
2024-05-14 13:07:09 +00:00
return BOOLEAN
2024-05-14 13:07:09 +00:00
case "string":
2024-05-14 13:07:09 +00:00
return STRING
2024-05-14 13:07:09 +00:00
}
return typeName
2024-05-14 13:07:09 +00:00
}
// IsGolangPrimitiveType determine whether the type name is a golang primitive type.
2024-05-14 13:07:09 +00:00
func IsGolangPrimitiveType(typeName string) bool {
2024-05-14 13:07:09 +00:00
switch typeName {
2024-05-14 13:07:09 +00:00
case "uint",
2024-05-14 13:07:09 +00:00
"int",
2024-05-14 13:07:09 +00:00
"uint8",
2024-05-14 13:07:09 +00:00
"int8",
2024-05-14 13:07:09 +00:00
"uint16",
2024-05-14 13:07:09 +00:00
"int16",
2024-05-14 13:07:09 +00:00
"byte",
2024-05-14 13:07:09 +00:00
"uint32",
2024-05-14 13:07:09 +00:00
"int32",
2024-05-14 13:07:09 +00:00
"rune",
2024-05-14 13:07:09 +00:00
"uint64",
2024-05-14 13:07:09 +00:00
"int64",
2024-05-14 13:07:09 +00:00
"float32",
2024-05-14 13:07:09 +00:00
"float64",
2024-05-14 13:07:09 +00:00
"bool",
2024-05-14 13:07:09 +00:00
"string":
2024-05-14 13:07:09 +00:00
return true
2024-05-14 13:07:09 +00:00
}
return false
2024-05-14 13:07:09 +00:00
}
// TransToValidCollectionFormat determine valid collection format.
2024-05-14 13:07:09 +00:00
func TransToValidCollectionFormat(format string) string {
2024-05-14 13:07:09 +00:00
switch format {
2024-05-14 13:07:09 +00:00
case "csv", "multi", "pipes", "tsv", "ssv":
2024-05-14 13:07:09 +00:00
return format
2024-05-14 13:07:09 +00:00
}
return ""
2024-05-14 13:07:09 +00:00
}
func ignoreNameOverride(name string) bool {
2024-05-14 13:07:09 +00:00
return len(name) != 0 && name[0] == IgnoreNameOverridePrefix
2024-05-14 13:07:09 +00:00
}
// IsComplexSchema whether a schema is complex and should be a ref schema
2024-05-14 13:07:09 +00:00
func IsComplexSchema(schema *spec.Schema) bool {
2024-05-14 13:07:09 +00:00
// a enum type should be complex
2024-05-14 13:07:09 +00:00
if len(schema.Enum) > 0 {
2024-05-14 13:07:09 +00:00
return true
2024-05-14 13:07:09 +00:00
}
// a deep array type is complex, how to determine deep? here more than 2 ,for example: [][]object,[][][]int
2024-05-14 13:07:09 +00:00
if len(schema.Type) > 2 {
2024-05-14 13:07:09 +00:00
return true
2024-05-14 13:07:09 +00:00
}
//Object included, such as Object or []Object
2024-05-14 13:07:09 +00:00
for _, st := range schema.Type {
2024-05-14 13:07:09 +00:00
if st == OBJECT {
2024-05-14 13:07:09 +00:00
return true
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return false
2024-05-14 13:07:09 +00:00
}
// IsRefSchema whether a schema is a reference schema.
2024-05-14 13:07:09 +00:00
func IsRefSchema(schema *spec.Schema) bool {
2024-05-14 13:07:09 +00:00
return schema.Ref.Ref.GetURL() != nil
2024-05-14 13:07:09 +00:00
}
// RefSchema build a reference schema.
2024-05-14 13:07:09 +00:00
func RefSchema(refType string) *spec.Schema {
2024-05-14 13:07:09 +00:00
return spec.RefSchema("#/definitions/" + refType)
2024-05-14 13:07:09 +00:00
}
// PrimitiveSchema build a primitive schema.
2024-05-14 13:07:09 +00:00
func PrimitiveSchema(refType string) *spec.Schema {
2024-05-14 13:07:09 +00:00
return &spec.Schema{SchemaProps: spec.SchemaProps{Type: []string{refType}}}
2024-05-14 13:07:09 +00:00
}
// BuildCustomSchema build custom schema specified by tag swaggertype.
2024-05-14 13:07:09 +00:00
func BuildCustomSchema(types []string) (*spec.Schema, error) {
2024-05-14 13:07:09 +00:00
if len(types) == 0 {
2024-05-14 13:07:09 +00:00
return nil, nil
2024-05-14 13:07:09 +00:00
}
switch types[0] {
2024-05-14 13:07:09 +00:00
case PRIMITIVE:
2024-05-14 13:07:09 +00:00
if len(types) == 1 {
2024-05-14 13:07:09 +00:00
return nil, errors.New("need primitive type after primitive")
2024-05-14 13:07:09 +00:00
}
return BuildCustomSchema(types[1:])
2024-05-14 13:07:09 +00:00
case ARRAY:
2024-05-14 13:07:09 +00:00
if len(types) == 1 {
2024-05-14 13:07:09 +00:00
return nil, errors.New("need array item type after array")
2024-05-14 13:07:09 +00:00
}
schema, err := BuildCustomSchema(types[1:])
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
return spec.ArrayProperty(schema), nil
2024-05-14 13:07:09 +00:00
case OBJECT:
2024-05-14 13:07:09 +00:00
if len(types) == 1 {
2024-05-14 13:07:09 +00:00
return PrimitiveSchema(types[0]), nil
2024-05-14 13:07:09 +00:00
}
schema, err := BuildCustomSchema(types[1:])
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
return spec.MapProperty(schema), nil
2024-05-14 13:07:09 +00:00
default:
2024-05-14 13:07:09 +00:00
err := CheckSchemaType(types[0])
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
return PrimitiveSchema(types[0]), nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
// MergeSchema merge schemas
2024-05-14 13:07:09 +00:00
func MergeSchema(dst *spec.Schema, src *spec.Schema) *spec.Schema {
2024-05-14 13:07:09 +00:00
if len(src.Type) > 0 {
2024-05-14 13:07:09 +00:00
dst.Type = src.Type
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if len(src.Properties) > 0 {
2024-05-14 13:07:09 +00:00
dst.Properties = src.Properties
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if src.Items != nil {
2024-05-14 13:07:09 +00:00
dst.Items = src.Items
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if src.AdditionalProperties != nil {
2024-05-14 13:07:09 +00:00
dst.AdditionalProperties = src.AdditionalProperties
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if len(src.Description) > 0 {
2024-05-14 13:07:09 +00:00
dst.Description = src.Description
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if src.Nullable {
2024-05-14 13:07:09 +00:00
dst.Nullable = src.Nullable
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if len(src.Format) > 0 {
2024-05-14 13:07:09 +00:00
dst.Format = src.Format
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if src.Default != nil {
2024-05-14 13:07:09 +00:00
dst.Default = src.Default
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if src.Example != nil {
2024-05-14 13:07:09 +00:00
dst.Example = src.Example
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if len(src.Extensions) > 0 {
2024-05-14 13:07:09 +00:00
dst.Extensions = src.Extensions
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if src.Maximum != nil {
2024-05-14 13:07:09 +00:00
dst.Maximum = src.Maximum
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if src.Minimum != nil {
2024-05-14 13:07:09 +00:00
dst.Minimum = src.Minimum
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if src.ExclusiveMaximum {
2024-05-14 13:07:09 +00:00
dst.ExclusiveMaximum = src.ExclusiveMaximum
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if src.ExclusiveMinimum {
2024-05-14 13:07:09 +00:00
dst.ExclusiveMinimum = src.ExclusiveMinimum
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if src.MaxLength != nil {
2024-05-14 13:07:09 +00:00
dst.MaxLength = src.MaxLength
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if src.MinLength != nil {
2024-05-14 13:07:09 +00:00
dst.MinLength = src.MinLength
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if len(src.Pattern) > 0 {
2024-05-14 13:07:09 +00:00
dst.Pattern = src.Pattern
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if src.MaxItems != nil {
2024-05-14 13:07:09 +00:00
dst.MaxItems = src.MaxItems
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if src.MinItems != nil {
2024-05-14 13:07:09 +00:00
dst.MinItems = src.MinItems
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if src.UniqueItems {
2024-05-14 13:07:09 +00:00
dst.UniqueItems = src.UniqueItems
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if src.MultipleOf != nil {
2024-05-14 13:07:09 +00:00
dst.MultipleOf = src.MultipleOf
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if len(src.Enum) > 0 {
2024-05-14 13:07:09 +00:00
dst.Enum = src.Enum
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if len(src.Extensions) > 0 {
2024-05-14 13:07:09 +00:00
dst.Extensions = src.Extensions
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if len(src.ExtraProps) > 0 {
2024-05-14 13:07:09 +00:00
dst.ExtraProps = src.ExtraProps
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return dst
2024-05-14 13:07:09 +00:00
}