Skip to content

Commit

Permalink
feat: improve variable naming with customCamelCase
Browse files Browse the repository at this point in the history
  • Loading branch information
rriski committed Oct 16, 2024
1 parent ef6a6fc commit 7238588
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
17 changes: 14 additions & 3 deletions generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func exec() error {
if rsp.CamelName != schemaOut.CamelName {
// Takes original name and turns to camel.
// "CamelName" field might have been modified because of name collisions
outReturn.Dot(strcase.ToCamel(rsp.name))
outReturn.Dot(customCamelCase(rsp.name))

if forcePointer {
// return &out.Foo
Expand Down Expand Up @@ -488,7 +488,7 @@ func writeStruct(f *jen.File, s *Schema) error {
continue
}

constant := s.CamelName + strcase.ToCamel(literal)
constant := s.CamelName + customCamelCase(literal)

// KafkaMirror ReplicationPolicyClassType makes bad generated name
if strings.HasPrefix(literal, "org.apache.kafka.connect.mirror.") {
Expand Down Expand Up @@ -529,7 +529,7 @@ func fmtStruct(s *Schema) *jen.Statement {
fields := make([]jen.Code, 0, len(s.Properties))
for _, k := range s.propertyNames {
p := s.Properties[k]
field := jen.Id(strcase.ToCamel(k)).Add(getType(p))
field := jen.Id(customCamelCase(k)).Add(getType(p))
tag := k

if !p.required {
Expand Down Expand Up @@ -648,3 +648,14 @@ func fmtQueryParam(funcName, queryParamType string, p *Parameter) (*jen.Statemen
func fmtQueryParamType() *jen.Statement {
return jen.Index(jen.Lit(queryParamArraySize)).String()
}

func customCamelCase(s string) string {
// Split the string by ":"
parts := strings.Split(s, ":")
for i, part := range parts {
// Convert each part to camelCase using strcase.ToCamel
parts[i] = strcase.ToCamel(part)
}
// Join the parts back together
return strings.Join(parts, "")
}
10 changes: 5 additions & 5 deletions generator/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@ func (s *Schema) init(doc *Doc, scope map[string]*Schema, name string) {
}

s.name = name
s.CamelName = strcase.ToCamel(s.name)
s.CamelName = customCamelCase(s.name)

if s.isEnum() {
const enumTypeSuffix = "Type"

betterName := getEnumName(s)
if betterName != s.name {
s.CamelName = cleanEnumName.ReplaceAllString(strcase.ToCamel(betterName), "") + s.CamelName
s.CamelName = cleanEnumName.ReplaceAllString(customCamelCase(betterName), "") + s.CamelName
}

if !strings.Contains(s.CamelName, enumTypeSuffix) {
Expand Down Expand Up @@ -236,7 +236,7 @@ func (s *Schema) init(doc *Doc, scope map[string]*Schema, name string) {
}

if s.parent != nil && s.parent.isPrivate() {
s.CamelName = strcase.ToCamel(s.parent.CamelName)
s.CamelName = customCamelCase(s.parent.CamelName)
}

// Some cases just impossible to cover
Expand Down Expand Up @@ -305,9 +305,9 @@ func (s *Schema) init(doc *Doc, scope map[string]*Schema, name string) {
}

if parent.isPrivate() {
s.CamelName = strcase.ToCamel(parent.CamelName)
s.CamelName = customCamelCase(parent.CamelName)
} else {
s.CamelName = strings.TrimSuffix(strcase.ToCamel(parent.CamelName), suffix) + s.CamelName
s.CamelName = strings.TrimSuffix(customCamelCase(parent.CamelName), suffix) + s.CamelName
}

// Marks all have collision
Expand Down

0 comments on commit 7238588

Please sign in to comment.