Skip to content

Commit

Permalink
move overload method detection after filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
ZelvaMan committed Apr 23, 2024
1 parent 794a95e commit 2f2d55d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
24 changes: 0 additions & 24 deletions dbGen/filter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dbGen

import (
"fmt"
"github.com/keenmate/db-gen/common"
)

Expand All @@ -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)

}
Expand Down Expand Up @@ -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
}
48 changes: 44 additions & 4 deletions dbGen/preprocess.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -31,11 +41,41 @@ func markFunctionAsOverloaded(routines *[]DbRoutine) {
continue
}

schemaConfig, exists := schemaMap[routine.RoutineSchema]

if !exists {
panic(fmt.Sprintf("schema config for schema %s missing"))

Check failure on line 47 in dbGen/preprocess.go

View workflow job for this annotation

GitHub Actions / build

fmt.Sprintf format %s reads arg #1, but call has 0 args

Check failure on line 47 in dbGen/preprocess.go

View workflow job for this annotation

GitHub Actions / build

fmt.Sprintf format %s reads arg #1, but call has 0 args

Check failure on line 47 in dbGen/preprocess.go

View workflow job for this annotation

GitHub Actions / build

fmt.Sprintf format %s reads arg #1, but call has 0 args
}

// 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

}

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
}
7 changes: 4 additions & 3 deletions dbGen/processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 2f2d55d

Please sign in to comment.