This commit is contained in:
jacky 2020-12-04 23:49:07 -08:00 committed by GitHub
commit a99f24dcbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 5 deletions

View File

@ -93,6 +93,10 @@ var stdlibObject = map[string]string{
"&{json RawMessage}": "json.RawMessage", "&{json RawMessage}": "json.RawMessage",
} }
var customObject = map[string]string{
"&{base ObjectID}": "string",
}
func init() { func init() {
pkgCache = make(map[string]struct{}) pkgCache = make(map[string]struct{})
controllerComments = make(map[string]string) controllerComments = make(map[string]string)
@ -182,7 +186,7 @@ func GenerateDocs(curpath string) {
} else if strings.HasPrefix(s, "@Title") { } else if strings.HasPrefix(s, "@Title") {
rootapi.Infos.Title = strings.TrimSpace(s[len("@Title"):]) rootapi.Infos.Title = strings.TrimSpace(s[len("@Title"):])
} else if strings.HasPrefix(s, "@Description") { } else if strings.HasPrefix(s, "@Description") {
rootapi.Infos.Description = strings.TrimSpace(s[len("@Description"):]) rootapi.Infos.Description += fmt.Sprintf("%s\n", strings.TrimSpace(s[len("@Description"):]))
} else if strings.HasPrefix(s, "@TermsOfServiceUrl") { } else if strings.HasPrefix(s, "@TermsOfServiceUrl") {
rootapi.Infos.TermsOfService = strings.TrimSpace(s[len("@TermsOfServiceUrl"):]) rootapi.Infos.TermsOfService = strings.TrimSpace(s[len("@TermsOfServiceUrl"):])
} else if strings.HasPrefix(s, "@Contact") { } else if strings.HasPrefix(s, "@Contact") {
@ -552,7 +556,7 @@ func parserComments(f *ast.FuncDecl, controllerName, pkgpath string) error {
} else if strings.HasPrefix(t, "@Title") { } else if strings.HasPrefix(t, "@Title") {
opts.OperationID = controllerName + "." + strings.TrimSpace(t[len("@Title"):]) opts.OperationID = controllerName + "." + strings.TrimSpace(t[len("@Title"):])
} else if strings.HasPrefix(t, "@Description") { } else if strings.HasPrefix(t, "@Description") {
opts.Description = strings.TrimSpace(t[len("@Description"):]) opts.Description += fmt.Sprintf("%s\n<br>", strings.TrimSpace(t[len("@Description"):]))
} else if strings.HasPrefix(t, "@Summary") { } else if strings.HasPrefix(t, "@Summary") {
opts.Summary = strings.TrimSpace(t[len("@Summary"):]) opts.Summary = strings.TrimSpace(t[len("@Summary"):])
} else if strings.HasPrefix(t, "@Success") { } else if strings.HasPrefix(t, "@Success") {
@ -1143,6 +1147,10 @@ func parseStruct(st *ast.StructType, k string, m *swagger.Schema, realTypes *[]s
} }
} }
if ignore := stag.Get("ignore"); ignore != "" {
continue
}
tag := stag.Get("json") tag := stag.Get("json")
if tag != "" { if tag != "" {
tagValues = strings.Split(tag, ",") tagValues = strings.Split(tag, ",")
@ -1156,6 +1164,14 @@ func parseStruct(st *ast.StructType, k string, m *swagger.Schema, realTypes *[]s
name = tagValues[0] name = tagValues[0]
} }
// set property type to the second tag value only if it is not omitempty and isBasicType
if len(tagValues) > 1 && tagValues[1] != "omitempty" && isBasicType(tagValues[1]) {
typeFormat := strings.Split(basicTypes[tagValues[1]], ":")
mp.Type = typeFormat[0]
mp.Format = typeFormat[1]
mp.Ref = ""
}
if thrifttag := stag.Get("thrift"); thrifttag != "" { if thrifttag := stag.Get("thrift"); thrifttag != "" {
ts := strings.Split(thrifttag, ",") ts := strings.Split(thrifttag, ",")
if ts[0] != "" { if ts[0] != "" {
@ -1175,15 +1191,15 @@ func parseStruct(st *ast.StructType, k string, m *swagger.Schema, realTypes *[]s
m.Properties[name] = mp m.Properties[name] = mp
} }
if ignore := stag.Get("ignore"); ignore != "" {
continue
}
} else { } else {
// only parse case of when embedded field is TypeName // only parse case of when embedded field is TypeName
// cases of *TypeName and Interface are not handled, maybe useless for swagger spec // cases of *TypeName and Interface are not handled, maybe useless for swagger spec
tag := "" tag := ""
if field.Tag != nil { if field.Tag != nil {
stag := reflect.StructTag(strings.Trim(field.Tag.Value, "`")) stag := reflect.StructTag(strings.Trim(field.Tag.Value, "`"))
if ignore := stag.Get("ignore"); ignore != "" {
continue
}
tag = stag.Get("json") tag = stag.Get("json")
} }
@ -1224,6 +1240,9 @@ func typeAnalyser(f *ast.Field) (isSlice bool, realType, swaggerType string) {
if isBasicType(fmt.Sprint(arr.Elt)) { if isBasicType(fmt.Sprint(arr.Elt)) {
return true, fmt.Sprintf("[]%v", arr.Elt), basicTypes[fmt.Sprint(arr.Elt)] return true, fmt.Sprintf("[]%v", arr.Elt), basicTypes[fmt.Sprint(arr.Elt)]
} }
if object, isCustomObject := customObject[fmt.Sprint(arr.Elt)]; isCustomObject {
return true, fmt.Sprintf("[]%v", object), basicTypes[object]
}
if mp, ok := arr.Elt.(*ast.MapType); ok { if mp, ok := arr.Elt.(*ast.MapType); ok {
return false, fmt.Sprintf("map[%v][%v]", mp.Key, mp.Value), astTypeObject return false, fmt.Sprintf("map[%v][%v]", mp.Key, mp.Value), astTypeObject
} }
@ -1248,6 +1267,8 @@ func typeAnalyser(f *ast.Field) (isSlice bool, realType, swaggerType string) {
return false, astTypeMap, basicTypes[val] return false, astTypeMap, basicTypes[val]
} }
return false, val, astTypeObject return false, val, astTypeObject
case *ast.InterfaceType:
return false, "interface", astTypeObject
} }
basicType := fmt.Sprint(f.Type) basicType := fmt.Sprint(f.Type)
if object, isStdLibObject := stdlibObject[basicType]; isStdLibObject { if object, isStdLibObject := stdlibObject[basicType]; isStdLibObject {