Skip to content

Commit

Permalink
feat: Support custom model names for generics
Browse files Browse the repository at this point in the history
add prefix to generic model names, to prevent renaming, if name annotation exists
  • Loading branch information
FabianMartin committed Aug 1, 2022
1 parent fff4b9f commit bf89c11
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
5 changes: 3 additions & 2 deletions generics.go
Expand Up @@ -56,6 +56,7 @@ func (pkgDefs *PackagesDefinitions) parametrizeStruct(original *TypeSpecDef, ful
return spec
}

pkgName := strings.Split(fullGenericForm, ".")[0]
genericTypeName, genericParams := splitStructName(fullGenericForm)
if genericParams == nil {
return nil
Expand Down Expand Up @@ -112,7 +113,7 @@ func (pkgDefs *PackagesDefinitions) parametrizeStruct(original *TypeSpecDef, ful
genericTypeName = strings.Split(genericTypeName, ".")[1]
}

var typeName = []string{TypeDocName(genericTypeName, parametrizedTypeSpec.TypeSpec)}
var typeName = []string{TypeDocName(fullTypeName(pkgName, genericTypeName), parametrizedTypeSpec.TypeSpec)}

for _, def := range original.TypeSpec.TypeParams.List {
if specDef, ok := genericParamTypeDefs[def.Names[0].Name]; ok {
Expand All @@ -128,7 +129,7 @@ func (pkgDefs *PackagesDefinitions) parametrizeStruct(original *TypeSpecDef, ful
}

ident.Name = strings.Join(typeName, "-")
ident.Name = strings.Replace(ident.Name, ".", "_", -1)
ident.Name = string(IgnoreNameOverridePrefix) + strings.Replace(strings.Replace(ident.Name, ".", "_", -1), "_", ".", 1)

parametrizedTypeSpec.TypeSpec.Name = ident
origStructType := original.TypeSpec.Type.(*ast.StructType)
Expand Down
2 changes: 1 addition & 1 deletion parser.go
Expand Up @@ -1066,7 +1066,7 @@ func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef) (*Schema, error)
}

func fullTypeName(pkgName, typeName string) string {
if pkgName != "" {
if pkgName != "" && !ignoreNameOverride(typeName) {
return pkgName + "." + typeName
}

Expand Down
13 changes: 12 additions & 1 deletion schema.go
Expand Up @@ -34,6 +34,9 @@ const (
ANY = "any"
// NIL represent a empty value.
NIL = "nil"

// IgnoreNameOverridePrefix Prepend to model to avoid renaming based on comment.
IgnoreNameOverridePrefix = '$'
)

// CheckSchemaType checks if typeName is not a name of primitive type.
Expand Down Expand Up @@ -132,7 +135,7 @@ func TransToValidCollectionFormat(format string) string {

// TypeDocName get alias from comment '// @name ', otherwise the original type name to display in doc.
func TypeDocName(pkgName string, spec *ast.TypeSpec) string {
if spec != nil {
if spec != nil && !ignoreNameOverride(pkgName) {
if spec.Comment != nil {
for _, comment := range spec.Comment.List {
texts := strings.Split(strings.TrimSpace(strings.TrimLeft(comment.Text, "/")), " ")
Expand All @@ -147,9 +150,17 @@ func TypeDocName(pkgName string, spec *ast.TypeSpec) string {
}
}

if pkgName[0] == '$' {
return pkgName[1:]
}

return pkgName
}

func ignoreNameOverride(name string) bool {
return len(name) != 0 && name[0] == IgnoreNameOverridePrefix
}

// RefSchema build a reference schema.
func RefSchema(refType string) *spec.Schema {
return spec.RefSchema("#/definitions/" + refType)
Expand Down

0 comments on commit bf89c11

Please sign in to comment.