From b56b3794e481bf9be4d88afa2a614fb04bacd312 Mon Sep 17 00:00:00 2001 From: wucheng Date: Wed, 12 Aug 2020 10:19:09 +0800 Subject: [PATCH 1/7] fixbug: ignore tag did not works; set property type to the second tag value only if it is not omitempty and isBasicType --- generate/swaggergen/g_docs.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/generate/swaggergen/g_docs.go b/generate/swaggergen/g_docs.go index 48b54ad..b84b12d 100644 --- a/generate/swaggergen/g_docs.go +++ b/generate/swaggergen/g_docs.go @@ -1143,6 +1143,10 @@ func parseStruct(st *ast.StructType, k string, m *swagger.Schema, realTypes *[]s } } + if ignore := stag.Get("ignore"); ignore != "" { + continue + } + tag := stag.Get("json") if tag != "" { tagValues = strings.Split(tag, ",") @@ -1156,6 +1160,14 @@ func parseStruct(st *ast.StructType, k string, m *swagger.Schema, realTypes *[]s 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 != "" { ts := strings.Split(thrifttag, ",") if ts[0] != "" { @@ -1175,15 +1187,15 @@ func parseStruct(st *ast.StructType, k string, m *swagger.Schema, realTypes *[]s m.Properties[name] = mp } - if ignore := stag.Get("ignore"); ignore != "" { - continue - } } else { // only parse case of when embedded field is TypeName // cases of *TypeName and Interface are not handled, maybe useless for swagger spec tag := "" if field.Tag != nil { stag := reflect.StructTag(strings.Trim(field.Tag.Value, "`")) + if ignore := stag.Get("ignore"); ignore != "" { + continue + } tag = stag.Get("json") } From 2862ff48db237f8e7c987c4fca90b3ed8529b653 Mon Sep 17 00:00:00 2001 From: wucheng Date: Wed, 12 Aug 2020 16:14:23 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E5=AF=B9@Description=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=A4=9A=E8=A1=8C=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generate/swaggergen/g_docs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generate/swaggergen/g_docs.go b/generate/swaggergen/g_docs.go index b84b12d..a3adc8d 100644 --- a/generate/swaggergen/g_docs.go +++ b/generate/swaggergen/g_docs.go @@ -182,7 +182,7 @@ func GenerateDocs(curpath string) { } else if strings.HasPrefix(s, "@Title") { rootapi.Infos.Title = strings.TrimSpace(s[len("@Title"):]) } else if strings.HasPrefix(s, "@Description") { - rootapi.Infos.Description = strings.TrimSpace(s[len("@Description"):]) + rootapi.Infos.Description += fmt.Sprintf("\n%s", strings.TrimSpace(s[len("@Description"):])) } else if strings.HasPrefix(s, "@TermsOfServiceUrl") { rootapi.Infos.TermsOfService = strings.TrimSpace(s[len("@TermsOfServiceUrl"):]) } else if strings.HasPrefix(s, "@Contact") { From 53b0b68d00dcf88a27e0ec0faa946f08afc9bd0b Mon Sep 17 00:00:00 2001 From: wucheng Date: Mon, 24 Aug 2020 10:48:59 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BD=BF=E5=BE=97function=E7=9A=84Descript?= =?UTF-8?q?ion=E5=A4=B4=E4=B9=9F=E5=8F=AF=E4=BB=A5=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E5=A4=9A=E8=A1=8C=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generate/swaggergen/g_docs.go | 4 ++-- go.sum | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/generate/swaggergen/g_docs.go b/generate/swaggergen/g_docs.go index a3adc8d..1c368ba 100644 --- a/generate/swaggergen/g_docs.go +++ b/generate/swaggergen/g_docs.go @@ -182,7 +182,7 @@ func GenerateDocs(curpath string) { } else if strings.HasPrefix(s, "@Title") { rootapi.Infos.Title = strings.TrimSpace(s[len("@Title"):]) } else if strings.HasPrefix(s, "@Description") { - rootapi.Infos.Description += fmt.Sprintf("\n%s", strings.TrimSpace(s[len("@Description"):])) + rootapi.Infos.Description += fmt.Sprintf("%s\n", strings.TrimSpace(s[len("@Description"):])) } else if strings.HasPrefix(s, "@TermsOfServiceUrl") { rootapi.Infos.TermsOfService = strings.TrimSpace(s[len("@TermsOfServiceUrl"):]) } else if strings.HasPrefix(s, "@Contact") { @@ -552,7 +552,7 @@ func parserComments(f *ast.FuncDecl, controllerName, pkgpath string) error { } else if strings.HasPrefix(t, "@Title") { opts.OperationID = controllerName + "." + strings.TrimSpace(t[len("@Title"):]) } else if strings.HasPrefix(t, "@Description") { - opts.Description = strings.TrimSpace(t[len("@Description"):]) + opts.Description += fmt.Sprintf("%s\n
", strings.TrimSpace(t[len("@Description"):])) } else if strings.HasPrefix(t, "@Summary") { opts.Summary = strings.TrimSpace(t[len("@Summary"):]) } else if strings.HasPrefix(t, "@Success") { diff --git a/go.sum b/go.sum index 35bcead..32d530c 100644 --- a/go.sum +++ b/go.sum @@ -156,6 +156,7 @@ github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzR github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mattn/go-colorable v0.0.0-20170327083344-ded68f7a9561 h1:isR/L+BIZ+rqODWYR/f526ygrBMGKZYFhaaFRDGvuZ8= github.com/mattn/go-colorable v0.0.0-20170327083344-ded68f7a9561/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= From 2c673b7d1e8bf0ee643559b6f9f02e806f576cad Mon Sep 17 00:00:00 2001 From: wucheng Date: Wed, 26 Aug 2020 11:17:09 +0800 Subject: [PATCH 4/7] support interface type --- generate/swaggergen/g_docs.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/generate/swaggergen/g_docs.go b/generate/swaggergen/g_docs.go index 1c368ba..b1baa30 100644 --- a/generate/swaggergen/g_docs.go +++ b/generate/swaggergen/g_docs.go @@ -1260,6 +1260,8 @@ func typeAnalyser(f *ast.Field) (isSlice bool, realType, swaggerType string) { return false, astTypeMap, basicTypes[val] } return false, val, astTypeObject + case *ast.InterfaceType: + return false, "interface", astTypeObject } basicType := fmt.Sprint(f.Type) if object, isStdLibObject := stdlibObject[basicType]; isStdLibObject { From b45ded3355bc6c27d18d3d11d88f21834ab8db63 Mon Sep 17 00:00:00 2001 From: wucheng Date: Wed, 26 Aug 2020 17:22:57 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=9A=84=E5=AF=B9=E8=B1=A1=E6=98=BE=E7=A4=BA=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generate/swaggergen/g_docs.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/generate/swaggergen/g_docs.go b/generate/swaggergen/g_docs.go index b1baa30..5e81a1a 100644 --- a/generate/swaggergen/g_docs.go +++ b/generate/swaggergen/g_docs.go @@ -93,6 +93,10 @@ var stdlibObject = map[string]string{ "&{json RawMessage}": "json.RawMessage", } +var customObject = map[string]string{ + "&{base ObjectID}": "string", +} + func init() { pkgCache = make(map[string]struct{}) controllerComments = make(map[string]string) @@ -1236,6 +1240,9 @@ func typeAnalyser(f *ast.Field) (isSlice bool, realType, swaggerType string) { if isBasicType(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 { return false, fmt.Sprintf("map[%v][%v]", mp.Key, mp.Value), astTypeObject } From c0d29b0d2a0d4455044794996b789a9d9f208d4a Mon Sep 17 00:00:00 2001 From: wucheng Date: Thu, 27 Aug 2020 14:23:02 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89map=E7=BB=93=E6=9E=84=E7=9A=84=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generate/swaggergen/g_docs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generate/swaggergen/g_docs.go b/generate/swaggergen/g_docs.go index 5e81a1a..fc3752d 100644 --- a/generate/swaggergen/g_docs.go +++ b/generate/swaggergen/g_docs.go @@ -1266,7 +1266,7 @@ func typeAnalyser(f *ast.Field) (isSlice bool, realType, swaggerType string) { if isBasicType(val) { return false, astTypeMap, basicTypes[val] } - return false, val, astTypeObject + return false, fmt.Sprintf("map[%v][%v]", t.Key, t.Value), astTypeObject case *ast.InterfaceType: return false, "interface", astTypeObject } From d496d26a1c11cfa0cbf1d683f07683f436329a36 Mon Sep 17 00:00:00 2001 From: wucheng Date: Thu, 27 Aug 2020 14:53:32 +0800 Subject: [PATCH 7/7] =?UTF-8?q?revert:=E4=BF=AE=E5=A4=8D=E4=BA=86=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89map=E7=BB=93=E6=9E=84=E7=9A=84=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generate/swaggergen/g_docs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generate/swaggergen/g_docs.go b/generate/swaggergen/g_docs.go index fc3752d..5e81a1a 100644 --- a/generate/swaggergen/g_docs.go +++ b/generate/swaggergen/g_docs.go @@ -1266,7 +1266,7 @@ func typeAnalyser(f *ast.Field) (isSlice bool, realType, swaggerType string) { if isBasicType(val) { return false, astTypeMap, basicTypes[val] } - return false, fmt.Sprintf("map[%v][%v]", t.Key, t.Value), astTypeObject + return false, val, astTypeObject case *ast.InterfaceType: return false, "interface", astTypeObject }