From 2f2d55df33cc02187a8a32f61a0d1ec3df9c68e0 Mon Sep 17 00:00:00 2001 From: Jan Rada <31016933+ZelvaMan@users.noreply.github.com> Date: Tue, 23 Apr 2024 17:18:24 +0200 Subject: [PATCH] move overload method detection after filtering --- dbGen/filter.go | 24 ----------------------- dbGen/preprocess.go | 48 +++++++++++++++++++++++++++++++++++++++++---- dbGen/processing.go | 7 ++++--- 3 files changed, 48 insertions(+), 31 deletions(-) diff --git a/dbGen/filter.go b/dbGen/filter.go index 626420e..00b0d3f 100755 --- a/dbGen/filter.go +++ b/dbGen/filter.go @@ -1,7 +1,6 @@ package dbGen import ( - "fmt" "github.com/keenmate/db-gen/common" ) @@ -23,13 +22,6 @@ func FilterFunctions(routines *[]DbRoutine, config *Config) ([]DbRoutine, error) continue } - // enforce that overloaded routine has to have mapping - if routine.HasOverload && !hasCustomMappedName(&schemaConfig, &routine) { - // todo return error - common.Log("Overloaded function %s doesnt have mapping", routine.RoutineNameWithParams) - return nil, fmt.Errorf("overloaded function %s.%s doesn't have mapping defined", routine.RoutineSchema, routine.RoutineNameWithParams) - } - filteredFunctions = append(filteredFunctions, routine) } @@ -57,19 +49,3 @@ func getSchemaConfigMap(config *Config) map[string]SchemaConfig { return schemaMap } - -func hasCustomMappedName(schemaConfig *SchemaConfig, routine *DbRoutine) bool { - mappingInfo, exists := schemaConfig.Functions[routine.RoutineNameWithParams] - if !exists { - common.LogDebug("mapping for function %s doesnt exist", routine.RoutineNameWithParams) - return false - } - - if mappingInfo.MappedName == "" { - common.LogDebug("mapping for function %s exists, but mapped name is not set", routine.RoutineNameWithParams) - - return false - } - - return true -} diff --git a/dbGen/preprocess.go b/dbGen/preprocess.go index bbd7488..2455a62 100755 --- a/dbGen/preprocess.go +++ b/dbGen/preprocess.go @@ -1,12 +1,22 @@ package dbGen -import "github.com/keenmate/db-gen/common" +import ( + "fmt" + "github.com/keenmate/db-gen/common" +) -func PreprocessRoutines(routines *[]DbRoutine) { - markFunctionAsOverloaded(routines) +func PreprocessRoutines(routines *[]DbRoutine, config *Config) error { + err := markOverloadedRoutines(routines, config) + if err != nil { + return err + } + + return nil } -func markFunctionAsOverloaded(routines *[]DbRoutine) { +func markOverloadedRoutines(routines *[]DbRoutine, config *Config) error { + schemaMap := getSchemaConfigMap(config) + // first, we find all the overloaded functions namesCounter := make(map[string]int) for _, routine := range *routines { @@ -31,6 +41,19 @@ func markFunctionAsOverloaded(routines *[]DbRoutine) { continue } + schemaConfig, exists := schemaMap[routine.RoutineSchema] + + if !exists { + panic(fmt.Sprintf("schema config for schema %s missing")) + } + + // enforce that overloaded routine has to have mapping + if routine.HasOverload && !hasCustomMappedName(&schemaConfig, &routine) { + // todo return error + common.Log("Overloaded function %s doesnt have mapping", routine.RoutineNameWithParams) + return fmt.Errorf("overloaded function %s.%s doesn't have mapping defined", routine.RoutineSchema, routine.RoutineNameWithParams) + } + overloadedFunctionCount++ (*routines)[i].HasOverload = true @@ -38,4 +61,21 @@ func markFunctionAsOverloaded(routines *[]DbRoutine) { common.Log("Marked %d functions as overload", overloadedFunctionCount) + return nil +} + +func hasCustomMappedName(schemaConfig *SchemaConfig, routine *DbRoutine) bool { + mappingInfo, exists := schemaConfig.Functions[routine.RoutineNameWithParams] + if !exists { + common.LogDebug("mapping for function %s doesnt exist", routine.RoutineNameWithParams) + return false + } + + if mappingInfo.MappedName == "" { + common.LogDebug("mapping for function %s exists, but mapped name is not set", routine.RoutineNameWithParams) + + return false + } + + return true } diff --git a/dbGen/processing.go b/dbGen/processing.go index 4898aba..eb9a57a 100755 --- a/dbGen/processing.go +++ b/dbGen/processing.go @@ -9,19 +9,20 @@ import ( // Transforms data returned by database to structures that are used in generator func Process(routines []DbRoutine, config *Config) ([]Routine, error) { - PreprocessRoutines(&routines) - filteredFunctions, err := FilterFunctions(&routines, config) + filteredRoutines, err := FilterFunctions(&routines, config) if err != nil { return nil, fmt.Errorf("filtering routines: %s", err) } + err = PreprocessRoutines(&routines, config) + // don't need to compute for every property typeMappings := getTypeMappings(config) common.LogDebug("Got %d type mappings", len(typeMappings)) // Map routines - functions, err := mapFunctions(&filteredFunctions, &typeMappings, config) + functions, err := mapFunctions(&filteredRoutines, &typeMappings, config) if err != nil { log.Println("Error while processing functions")