Skip to content

Commit

Permalink
fix case conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ZelvaMan committed Apr 26, 2024
1 parent 83d1526 commit c4c6a87
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
23 changes: 23 additions & 0 deletions common/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package common

import (
"github.com/stoewer/go-strcase"
"strings"
)

func NormalizeStr(s string) string {
return strings.TrimLeft(s, "_")
}

func ToPascalCase(s string) string {
return strcase.UpperCamelCase(NormalizeStr(s))
}

func ToCamelCase(s string) string {
return strcase.LowerCamelCase(NormalizeStr(s))
}

func ToSnakeCase(s string) string {
return strcase.SnakeCase(NormalizeStr(s))

}
8 changes: 4 additions & 4 deletions dbGen/generator-functions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dbGen

import (
"github.com/stoewer/go-strcase"
"github.com/keenmate/db-gen/common"
"text/template"
)

Expand All @@ -11,13 +11,13 @@ func getTemplateFunctions() template.FuncMap {
return n + 1
},
"pascalCased": func(s string) string {
return strcase.UpperCamelCase(s)
return common.ToPascalCase(s)
},
"camelCased": func(s string) string {
return strcase.LowerCamelCase(s)
return common.ToCamelCase(s)
},
"snakeCased": func(s string) string {
return strcase.SnakeCase(s)
return common.ToSnakeCase(s)
},
}
}
7 changes: 3 additions & 4 deletions dbGen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"fmt"
"github.com/keenmate/db-gen/common"
"github.com/stoewer/go-strcase"
"io"
"log"
"os"
Expand Down Expand Up @@ -296,11 +295,11 @@ func generateFileHashes(outputFolder string) (*map[string]string, error) {
func changeCase(str string, desiredCase string) string {
switch desiredCase {
case "pascalcase":
return strcase.UpperCamelCase(str)
return common.ToPascalCase(str)
case "camelcase":
return strcase.LowerCamelCase(str)
return common.ToCamelCase(str)
case "snakecase":
return strcase.SnakeCase(str)
return common.ToSnakeCase(str)
default:
common.LogWarn("unknown case, this should never happen")
return str
Expand Down
11 changes: 5 additions & 6 deletions dbGen/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dbGen
import (
"fmt"
"github.com/keenmate/db-gen/common"
"github.com/stoewer/go-strcase"
"slices"
"sort"
)
Expand Down Expand Up @@ -211,7 +210,7 @@ func getColumnMapping(param DbParameter, routineMapping *RoutineMapping, globalM
return false, nil, nil
}

name := strcase.UpperCamelCase(param.Name)
name := common.ToPascalCase(param.Name)
isNullable := param.IsNullable
var typeMapping *mapping = nil
var err error = nil
Expand Down Expand Up @@ -308,16 +307,16 @@ func getFunctionName(dbFunctionName string, schema string, mappedName string) st
schemaPrefix := ""
// don't add public_ to function names
if schema != hiddenSchema {
schemaPrefix = strcase.UpperCamelCase(schema)
schemaPrefix = common.ToPascalCase(common.NormalizeStr(schema))
}
return schemaPrefix + strcase.UpperCamelCase(dbFunctionName)
return schemaPrefix + common.ToPascalCase(dbFunctionName)
}

func getModelName(functionName string) string {
return strcase.UpperCamelCase(functionName) + "Model"
return common.ToPascalCase(functionName) + "Model"
}
func getProcessorName(functionName string) string {
return strcase.UpperCamelCase(functionName) + "Processor"
return common.ToPascalCase(functionName) + "Processor"
}

// getTypeMapping if explicit mapping doesnt exist, try fallback
Expand Down

0 comments on commit c4c6a87

Please sign in to comment.