forked from swaggo/swag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
property.go
88 lines (80 loc) · 3.04 KB
/
property.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package swag
import (
"fmt"
"go/ast"
"strings"
)
type propertyName struct {
SchemaType string
ArrayType string
}
func parseFieldSelectorExpr(astTypeSelectorExpr *ast.SelectorExpr) propertyName {
// TODO: In the future, add functions and make them solve for each user
// Support for time.Time as a structure field
if "Time" == astTypeSelectorExpr.Sel.Name {
return propertyName{SchemaType: "string", ArrayType: "string"}
}
// Support bson.ObjectId type
if "ObjectId" == astTypeSelectorExpr.Sel.Name {
return propertyName{SchemaType: "string", ArrayType: "string"}
}
// Supprt UUID
if "UUID" == strings.ToUpper(astTypeSelectorExpr.Sel.Name) {
return propertyName{SchemaType: "string", ArrayType: "string"}
}
// Supprt shopspring/decimal
if "Decimal" == astTypeSelectorExpr.Sel.Name {
return propertyName{SchemaType: "number", ArrayType: "string"}
}
fmt.Printf("%s is not supported. but it will be set with string temporary. Please report any problems.", astTypeSelectorExpr.Sel.Name)
return propertyName{SchemaType: "string", ArrayType: "string"}
}
// getPropertyName returns the string value for the given field if it exists, otherwise it panics.
// allowedValues: array, boolean, integer, null, number, object, string
func getPropertyName(field *ast.Field) propertyName {
if astTypeSelectorExpr, ok := field.Type.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeSelectorExpr)
}
if astTypeIdent, ok := field.Type.(*ast.Ident); ok {
name := astTypeIdent.Name
schemeType := TransToValidSchemeType(name)
return propertyName{SchemaType: schemeType, ArrayType: schemeType}
}
if ptr, ok := field.Type.(*ast.StarExpr); ok {
if astTypeSelectorExpr, ok := ptr.X.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeSelectorExpr)
}
if astTypeIdent, ok := ptr.X.(*ast.Ident); ok {
name := astTypeIdent.Name
schemeType := TransToValidSchemeType(name)
return propertyName{SchemaType: schemeType, ArrayType: schemeType}
}
if astTypeArray, ok := ptr.X.(*ast.ArrayType); ok { // if array
if astTypeArrayIdent := astTypeArray.Elt.(*ast.Ident); ok {
name := astTypeArrayIdent.Name
return propertyName{SchemaType: "array", ArrayType: name}
}
}
}
if astTypeArray, ok := field.Type.(*ast.ArrayType); ok { // if array
if astTypeArrayExpr, ok := astTypeArray.Elt.(*ast.StarExpr); ok {
if astTypeArrayIdent := astTypeArrayExpr.X.(*ast.Ident); ok {
name := astTypeArrayIdent.Name
return propertyName{SchemaType: "array", ArrayType: name}
}
}
str := fmt.Sprintf("%s", astTypeArray.Elt)
return propertyName{SchemaType: "array", ArrayType: str}
}
if _, ok := field.Type.(*ast.MapType); ok { // if map
//TODO: support map
return propertyName{SchemaType: "object", ArrayType: "object"}
}
if _, ok := field.Type.(*ast.StructType); ok { // if struct
return propertyName{SchemaType: "object", ArrayType: "object"}
}
if _, ok := field.Type.(*ast.InterfaceType); ok { // if interface{}
return propertyName{SchemaType: "object", ArrayType: "object"}
}
panic("not supported" + fmt.Sprint(field.Type))
}