Skip to content

Commit

Permalink
Add template functions to change case and:
Browse files Browse the repository at this point in the history
- Rename Function to routine
  • Loading branch information
ZelvaMan committed Oct 22, 2023
1 parent 7de03aa commit 0979dd2
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 16 deletions.
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ Enable verbose logging with `--verbose` flag (can)
processor and model templates have `Function` struct availible as `.` argument
dbcontext has `DbContextData` struct as `.` argument

### Case

By default, all field use camel case. You should use `snake`/`lCamel`/`uCamel` to change case.
for example:

```gotemplate
{{snake $func.FunctionName}}
```

Data available in template

```go
package main

Expand Down
20 changes: 20 additions & 0 deletions src/generator-functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dbGen

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

func getTemplateFunctions() template.FuncMap {
return template.FuncMap{
"uCamel": func(s string) string {
return strcase.UpperCamelCase(s)
},
"lCamel": func(s string) string {
return strcase.LowerCamelCase(s)
},
"snake": func(s string) string {
return strcase.SnakeCase(s)
},
}
}
25 changes: 17 additions & 8 deletions src/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
const processorsFolder = "processors"
const modelsFolder = "models"

func Generate(routines []Function, config *Config) error {
func Generate(routines []Routine, config *Config) error {
fileHashes, err := generateFileHashes(config.OutputFolder)
if err != nil {
return fmt.Errorf("generating file hashes: %s", err)
Expand Down Expand Up @@ -59,7 +59,7 @@ func Generate(routines []Function, config *Config) error {
return nil
}

func generateDbContext(routines []Function, hashMap *map[string]string, config *Config) error {
func generateDbContext(routines []Routine, hashMap *map[string]string, config *Config) error {
dbcontextTemplate, err := parseTemplates(config.DbContextTemplate)
if err != nil {
return fmt.Errorf("loading dbContext template: %s", err)
Expand All @@ -86,7 +86,7 @@ func generateDbContext(routines []Function, hashMap *map[string]string, config *

}

func generateModels(routines []Function, hashMap *map[string]string, config *Config) error {
func generateModels(routines []Routine, hashMap *map[string]string, config *Config) error {

moduleTemplate, err := parseTemplates(config.ModelTemplate)
if err != nil {
Expand Down Expand Up @@ -118,7 +118,7 @@ func generateModels(routines []Function, hashMap *map[string]string, config *Con
return nil
}

func generateProcessors(routines []Function, hashMap *map[string]string, config *Config) error {
func generateProcessors(routines []Routine, hashMap *map[string]string, config *Config) error {
processorTemplate, err := parseTemplates(config.ProcessorTemplate)
if err != nil {
return fmt.Errorf("loading processor template: %s", err)
Expand Down Expand Up @@ -151,13 +151,22 @@ func generateProcessors(routines []Function, hashMap *map[string]string, config
return nil
}

func parseTemplates(filepath string) (*template.Template, error) {
if !fileExists(filepath) {
return nil, fmt.Errorf("config file %s does not exist", filepath)
func parseTemplates(templatePath string) (*template.Template, error) {
if !fileExists(templatePath) {
return nil, fmt.Errorf("template file %s does not exist", templatePath)

}

return template.ParseFiles(filepath)
name := filepath.Base(templatePath)

tmpl, err := template.New(name).
Funcs(getTemplateFunctions()).
ParseFiles(templatePath)

if err != nil {
return nil, err
}
return tmpl, nil
}

func generateFile(data interface{}, template *template.Template, fp string, hashMap *map[string]string) (bool, error) {
Expand Down
13 changes: 7 additions & 6 deletions src/preprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// Transforms data returned by database to structures that are used in generator

func Preprocess(routines []DbRoutine, config *Config) ([]Function, error) {
func Preprocess(routines []DbRoutine, config *Config) ([]Routine, error) {
// In future this should be more modular

filteredFunctions := filterFunctions(&routines, config)
Expand Down Expand Up @@ -48,13 +48,13 @@ func filterFunctions(functions *[]DbRoutine, config *Config) []DbRoutine {
if schemaConfig.AllFunctions || slices.Contains(schemaConfig.Functions, function.RoutineName) {
// Case sensitive
if slices.Contains(schemaConfig.IgnoredFunctions, function.RoutineName) {
VerboseLog("Function '%s.%s' in ignored functions", function.RoutineSchema, function.RoutineName)
VerboseLog("Routine '%s.%s' in ignored functions", function.RoutineSchema, function.RoutineName)
continue
}

filteredFunctions = append(filteredFunctions, function)
} else {
VerboseLog("Function '%s.%s' not generated because all function is false or isnt included in functions",
VerboseLog("Routine '%s.%s' not generated because all function is false or isnt included in functions",
function.RoutineSchema,
function.RoutineName)
}
Expand All @@ -74,8 +74,8 @@ func getSchemaConfigMap(config *Config) map[string]SchemaConfig {
return schemaMap
}

func mapFunctions(routines *[]DbRoutine, typeMappings *map[string]mapping, config *Config) ([]Function, error) {
mappedFunctions := make([]Function, len(*routines))
func mapFunctions(routines *[]DbRoutine, typeMappings *map[string]mapping, config *Config) ([]Routine, error) {
mappedFunctions := make([]Routine, len(*routines))

for i, routine := range *routines {
VerboseLog("Mapping %s", routine.RoutineName)
Expand All @@ -95,7 +95,7 @@ func mapFunctions(routines *[]DbRoutine, typeMappings *map[string]mapping, confi
modelName := getModelName(routine.RoutineName)
processorName := getProcessorName(routine.RoutineName)

function := &Function{
function := &Routine{
FunctionName: functionName,
DbFullFunctionName: dbFullFunctionName,
ModelName: modelName,
Expand Down Expand Up @@ -184,6 +184,7 @@ func getParameters(attributes []DbParameter, typeMappings *map[string]mapping) (
return properties, nil
}

// If you want to use different case, use template function in templates
func getFunctionName(dbFunctionName string) string {
return strcase.UpperCamelCase(dbFunctionName)
}
Expand Down
4 changes: 2 additions & 2 deletions src/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Property struct {
MapperFunction string
}

type Function struct {
type Routine struct {
FunctionName string
DbFullFunctionName string
ModelName string
Expand All @@ -24,7 +24,7 @@ type Function struct {
}

type DbContextData struct {
Functions []Function
Functions []Routine
}

type Command string
Expand Down

0 comments on commit 0979dd2

Please sign in to comment.