Skip to content

Commit

Permalink
use is nullable from override mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
ZelvaMan committed Apr 24, 2024
1 parent 2f2d55d commit c5378a7
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 15 deletions.
8 changes: 6 additions & 2 deletions dbGen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dbGen

import (
"fmt"
"github.com/guregu/null/v5"
"github.com/keenmate/db-gen/common"
"github.com/spf13/viper"
"os"
Expand Down Expand Up @@ -56,13 +57,16 @@ type ColumnMapping struct {
MappedName string `mapstructure:"MappedName"`
MappedType string `mapstructure:"MappedType"`
MappingFunction string `mapstructure:"MappingFunction"`
IsNullable bool `mapstructure:"IsNullable"`

IsNullable null.Bool `mapstructure:"IsNullable"`
}

type ParamMapping struct {
IsNullable bool `mapstructure:"IsNullable"`
MappedName string `mapstructure:"MappedName"`
MappedType string `mapstructure:"MappedType"`

// this is string because it can be empty
IsNullable null.Bool `mapstructure:"IsNullable"`
}

type Mapping struct {
Expand Down
11 changes: 11 additions & 0 deletions dbGen/configMapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dbGen

import (
"fmt"
"github.com/guregu/null/v5"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"reflect"
Expand Down Expand Up @@ -46,6 +47,9 @@ func mapCustomTypes() mapstructure.DecodeHookFunc {
if to == reflect.TypeOf(map[string]ColumnMapping{}) {
return decodeColumnMapping(data)
}
if to == reflect.TypeOf(null.Bool{}) {
return decodeNullBool(data)
}

return data, nil
}
Expand Down Expand Up @@ -109,3 +113,10 @@ func decodeColumnMapping(data interface{}) (interface{}, error) {

return columns, nil
}

func decodeNullBool(data interface{}) (interface{}, error) {
if reflect.TypeOf(data).Kind() == reflect.Bool {
return null.BoolFrom(data.(bool)), nil
}
return null.NewBool(false, false), nil
}
30 changes: 20 additions & 10 deletions dbGen/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dbGen

import (
"fmt"
"github.com/guregu/null/v5"
"github.com/keenmate/db-gen/common"
"github.com/stoewer/go-strcase"
"slices"
Expand Down Expand Up @@ -118,7 +119,7 @@ func mapReturnColumns(routine DbRoutine, typeMappings *map[string]mapping, routi
PropertyType: typeMapping.mappedType,
Position: column.OrdinalPosition - positionOffset,
MapperFunction: typeMapping.mappedFunction,
Nullable: column.IsNullable,
Nullable: getIsNullable(columnMapping.IsNullable, column.IsNullable),
}

properties = append(properties, *property)
Expand All @@ -145,8 +146,8 @@ func mapParameters(attributes []DbParameter, typeMappings *map[string]mapping, r
//common.LogDebug("Possition offset is %d", positionOffset)

for i, parameter := range attributes {
paramMapping := getParamMapping(parameter.Name, routineMapping)

paramMapping := getParamMapping(parameter, routineMapping)
common.LogDebug("parameter %s custom mapping: %+v", parameter.Name, paramMapping)
propertyName := getPropertyName(parameter.Name, paramMapping.MappedName)

typeMapping, err := getTypeMapping(parameter.UDTName, paramMapping.MappedType, "", typeMappings, config)
Expand All @@ -160,8 +161,8 @@ func mapParameters(attributes []DbParameter, typeMappings *map[string]mapping, r
PropertyName: propertyName,
PropertyType: typeMapping.mappedType,
Position: parameter.OrdinalPosition - positionOffset,
MapperFunction: typeMapping.mappedFunction,
Nullable: parameter.IsNullable,
MapperFunction: "",
Nullable: getIsNullable(paramMapping.IsNullable, parameter.IsNullable),
}

properties[i] = *property
Expand Down Expand Up @@ -244,6 +245,7 @@ func handleMappingOverride(typeOverride string, mappingFunctionOverride string,
}, nil
}
}

// no mapping function is set and no mapping exist for type given
return nil, fmt.Errorf("mapped type overriden to %s, but no mapping functions specified and mapping function for override type doenst exist in mappings", typeOverride)
}
Expand All @@ -263,7 +265,6 @@ func getRoutineMapping(routine DbRoutine, schemaConfigs map[string]SchemaConfig)
// this should never happen
panic("trying ty get function mapping for function in schema that is not defined. This should never happen, because function should have been fitered out")
}

routineMapping, found := schemaConfig.Functions[routine.RoutineNameWithParams]
if found {
return routineMapping
Expand All @@ -282,7 +283,7 @@ var emptyColumnMapping = ColumnMapping{
MappedName: "",
MappedType: "",
MappingFunction: "",
IsNullable: false,
IsNullable: null.NewBool(false, false),
}

func getColumnMapping(columnName string, routineMapping *RoutineMapping) (bool, ColumnMapping) {
Expand All @@ -303,16 +304,25 @@ func getColumnMapping(columnName string, routineMapping *RoutineMapping) (bool,
var emptyParamMapping = ParamMapping{
MappedName: "",
MappedType: "",
IsNullable: false,
IsNullable: null.NewBool(false, false),
}

func getParamMapping(paramName string, routineMapping *RoutineMapping) ParamMapping {
func getParamMapping(param DbParameter, routineMapping *RoutineMapping) ParamMapping {

columnMapping, hasExplicitParamMapping := routineMapping.Parameters[paramName]
columnMapping, hasExplicitParamMapping := routineMapping.Parameters[param.Name]
if hasExplicitParamMapping {
return columnMapping
}

return emptyParamMapping

}

func getIsNullable(typeMappingOverride null.Bool, implicitValue bool) bool {
if typeMappingOverride.Valid {
common.LogDebug("using explicit value of nullable %t", typeMappingOverride.Bool)
return typeMappingOverride.Bool
}

return implicitValue
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/keenmate/db-gen

go 1.21
go 1.21.4

toolchain go1.21.9

require (
github.com/jackc/pgx/v5 v5.5.1
Expand All @@ -12,6 +14,7 @@ require (

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/guregu/null/v5 v5.0.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/guregu/null/v5 v5.0.0 h1:PRxjqyOekS11W+w/7Vfz6jgJE/BCwELWtgvOJzddimw=
github.com/guregu/null/v5 v5.0.0/go.mod h1:SjupzNy+sCPtwQTKWhUCqjhVCO69hpsl2QsZrWHjlwU=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down
5 changes: 3 additions & 2 deletions testing/db-gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
"SelectOnlySpecified": true,
"Model": {
"__number": {
"MappedType": "int"
"MappedType": "int",
"IsNullable": true
},
"__json": {
"MappedName": "jason",
"MappedType": "string",
"MappingFunction": "mapJsonAsString",
"IsNullable": false
"IsNullable": null
}
}
},
Expand Down

0 comments on commit c5378a7

Please sign in to comment.