Skip to content

Commit

Permalink
chore: fix naming
Browse files Browse the repository at this point in the history
  • Loading branch information
ubogdan committed Jun 14, 2022
1 parent 6f9d2ec commit adf24b0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
7 changes: 5 additions & 2 deletions generics.go
Expand Up @@ -15,7 +15,7 @@ func typeSpecFullName(typeSpecDef *TypeSpecDef) string {
fullName = fullName + "["
for i, typeParam := range typeSpecDef.TypeSpec.TypeParams.List {
if i > 0 {
fullName = fullName + ","
fullName = fullName + "-"
}

fullName = fullName + typeParam.Names[0].Name
Expand Down Expand Up @@ -47,6 +47,7 @@ func (pkgDefs *PackagesDefinitions) parametrizeStruct(original *TypeSpecDef, ful
if !ok {
return nil
}

genericParamTypeDefs[original.TypeSpec.TypeParams.List[i].Names[0].Name] = tdef
}

Expand All @@ -64,11 +65,13 @@ func (pkgDefs *PackagesDefinitions) parametrizeStruct(original *TypeSpecDef, ful
NamePos: original.TypeSpec.Name.NamePos,
Obj: original.TypeSpec.Name.Obj,
}

genNameParts := strings.Split(fullGenericForm, "[")
if strings.Contains(genNameParts[0], ".") {
genNameParts[0] = strings.Split(genNameParts[0], ".")[1]
}
ident.Name = genNameParts[0] + "[" + strings.Replace(strings.Join(genNameParts[1:], ""), ".", "_", -1)

ident.Name = genNameParts[0] + "-" + strings.Replace(strings.Join(genericParams, "-"), ".", "_", -1)
ident.Name = strings.Replace(strings.Replace(ident.Name, "\t", "", -1), " ", "", -1)

parametrizedTypeSpec.TypeSpec.Name = ident
Expand Down
28 changes: 15 additions & 13 deletions generics_test.go
Expand Up @@ -5,6 +5,7 @@ package swag

import (
"encoding/json"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -48,13 +49,13 @@ func TestParseGenericsBasic(t *testing.T) {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/web.GenericResponse[web_Post]"
"$ref": "#/definitions/web.GenericResponse-web_Post"
}
},
"222": {
"description": "",
"schema": {
"$ref": "#/definitions/web.GenericResponseMulti[web_Post,web_Post]"
"$ref": "#/definitions/web.GenericResponseMulti-web_Post-web_Post"
}
},
"400": {
Expand Down Expand Up @@ -96,7 +97,7 @@ func TestParseGenericsBasic(t *testing.T) {
}
}
},
"web.GenericResponseMulti[web_Post,web_Post]": {
"web.GenericResponse-web_Post": {
"type": "object",
"properties": {
"data": {
Expand Down Expand Up @@ -127,7 +128,15 @@ func TestParseGenericsBasic(t *testing.T) {
}
}
},
"meta": {
"status": {
"type": "string"
}
}
},
"web.GenericResponseMulti-web_Post-web_Post": {
"type": "object",
"properties": {
"data": {
"type": "object",
"properties": {
"data": {
Expand Down Expand Up @@ -155,15 +164,7 @@ func TestParseGenericsBasic(t *testing.T) {
}
}
},
"status": {
"type": "string"
}
}
},
"web.GenericResponse[web_Post]": {
"type": "object",
"properties": {
"data": {
"meta": {
"type": "object",
"properties": {
"data": {
Expand Down Expand Up @@ -204,5 +205,6 @@ func TestParseGenericsBasic(t *testing.T) {
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)
b, _ := json.MarshalIndent(p.swagger, "", " ")
os.WriteFile("testdata/generics_basic/swagger.json", b, 0644)
assert.Equal(t, expected, string(b))
}
3 changes: 3 additions & 0 deletions testdata/generics_basic/api/api.go
Expand Up @@ -2,6 +2,8 @@ package api

import (
"net/http"

"github.com/swaggo/swag/testdata/generics_basic/web"
)

// @Summary Add a new pet to the store
Expand All @@ -16,4 +18,5 @@ import (
// @Router /posts/{post_id} [get]
func GetPost(w http.ResponseWriter, r *http.Request) {
//write your code
_ = web.GenericResponse[web.Post]{}
}

0 comments on commit adf24b0

Please sign in to comment.