From 0979dd22407b7ea95caea8e53f8fdcbc027b8f09 Mon Sep 17 00:00:00 2001 From: Jan Rada <31016933+ZelvaMan@users.noreply.github.com> Date: Sun, 22 Oct 2023 11:45:33 +0200 Subject: [PATCH] Add template functions to change case and: - Rename Function to routine --- readme.md | 11 +++++++++++ src/generator-functions.go | 20 ++++++++++++++++++++ src/generator.go | 25 +++++++++++++++++-------- src/preprocessor.go | 13 +++++++------ src/types.go | 4 ++-- 5 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 src/generator-functions.go diff --git a/readme.md b/readme.md index 10d236d..3adbf2d 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/src/generator-functions.go b/src/generator-functions.go new file mode 100644 index 0000000..d4a9a1f --- /dev/null +++ b/src/generator-functions.go @@ -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) + }, + } +} diff --git a/src/generator.go b/src/generator.go index acb1dff..7e4ca49 100644 --- a/src/generator.go +++ b/src/generator.go @@ -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) @@ -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) @@ -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 { @@ -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) @@ -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) { diff --git a/src/preprocessor.go b/src/preprocessor.go index 03aabe4..430b21f 100644 --- a/src/preprocessor.go +++ b/src/preprocessor.go @@ -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) @@ -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) } @@ -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) @@ -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, @@ -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) } diff --git a/src/types.go b/src/types.go index bb74338..ac1ed76 100644 --- a/src/types.go +++ b/src/types.go @@ -10,7 +10,7 @@ type Property struct { MapperFunction string } -type Function struct { +type Routine struct { FunctionName string DbFullFunctionName string ModelName string @@ -24,7 +24,7 @@ type Function struct { } type DbContextData struct { - Functions []Function + Functions []Routine } type Command string