Skip to content

Commit

Permalink
Huge refactor so that tables can be generic
Browse files Browse the repository at this point in the history
The allows drivers to add extra information to constraints and indexes
  • Loading branch information
stephenafamo committed Nov 16, 2024
1 parent 793e307 commit a8ee78c
Show file tree
Hide file tree
Showing 64 changed files with 1,640 additions and 1,251 deletions.
47 changes: 1 addition & 46 deletions gen/aliases.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gen

import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
Expand All @@ -10,26 +9,12 @@ import (
"github.com/volatiletech/strmangle"
)

// Aliases defines aliases for the generation run
type Aliases map[string]TableAlias

// TableAlias defines the spellings for a table name in Go
type TableAlias struct {
UpPlural string `yaml:"up_plural,omitempty" toml:"up_plural,omitempty" json:"up_plural,omitempty"`
UpSingular string `yaml:"up_singular,omitempty" toml:"up_singular,omitempty" json:"up_singular,omitempty"`
DownPlural string `yaml:"down_plural,omitempty" toml:"down_plural,omitempty" json:"down_plural,omitempty"`
DownSingular string `yaml:"down_singular,omitempty" toml:"down_singular,omitempty" json:"down_singular,omitempty"`

Columns map[string]string `yaml:"columns,omitempty" toml:"columns,omitempty" json:"columns,omitempty"`
Relationships map[string]string `yaml:"relationships,omitempty" toml:"relationships,omitempty" json:"relationships,omitempty"`
}

// initAliases takes the table information from the driver
// and fills in aliases where the user has provided none.
//
// This leaves us with a complete list of Go names for all tables,
// columns, and relationships.
func initAliases(a Aliases, tables []drivers.Table, relMap Relationships) {
func initAliases[C, I any](a drivers.Aliases, tables drivers.Tables[C, I], relMap Relationships) {
for _, t := range tables {
tableAlias := a[t.Key]
cleanKey := strings.ReplaceAll(t.Key, ".", "_")
Expand Down Expand Up @@ -76,33 +61,3 @@ func initAliases(a Aliases, tables []drivers.Table, relMap Relationships) {
a[t.Key] = tableAlias
}
}

// Table gets a table alias, panics if not found.
func (a Aliases) Table(table string) TableAlias {
t, ok := a[table]
if !ok {
panic("could not find table aliases for: " + table)
}

return t
}

// Column get's a column's aliased name, panics if not found.
func (t TableAlias) Column(column string) string {
c, ok := t.Columns[column]
if !ok {
panic(fmt.Sprintf("could not find column alias for: %s.%s", t.UpSingular, column))
}

return c
}

// Relationship looks up a relationship, panics if not found.
func (t TableAlias) Relationship(fkey string) string {
r, ok := t.Relationships[fkey]
if !ok {
panic(fmt.Sprintf("could not find relationship alias for: %s.%s", t.UpSingular, fkey))
}

return r
}
10 changes: 5 additions & 5 deletions gen/bobgen-helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func DefaultOutputs(destination, pkgname string, noFactory bool, templates *Temp
return outputs
}

func GetConfigFromFile[DriverConfig any](configPath, driverConfigKey string) (gen.Config, DriverConfig, error) {
func GetConfigFromFile[ConstraintExtra, DriverConfig any](configPath, driverConfigKey string) (gen.Config[ConstraintExtra], DriverConfig, error) {
var provider koanf.Provider
var config gen.Config
var config gen.Config[ConstraintExtra]
var driverConfig DriverConfig

_, err := os.Stat(configPath)
Expand All @@ -85,11 +85,11 @@ func GetConfigFromFile[DriverConfig any](configPath, driverConfigKey string) (ge
return config, driverConfig, err
}

return GetConfigFromProvider[DriverConfig](provider, driverConfigKey)
return GetConfigFromProvider[ConstraintExtra, DriverConfig](provider, driverConfigKey)
}

func GetConfigFromProvider[DriverConfig any](provider koanf.Provider, driverConfigKey string) (gen.Config, DriverConfig, error) {
var config gen.Config
func GetConfigFromProvider[ConstraintExtra, DriverConfig any](provider koanf.Provider, driverConfigKey string) (gen.Config[ConstraintExtra], DriverConfig, error) {
var config gen.Config[ConstraintExtra]
var driverConfig DriverConfig

k := koanf.New(".")
Expand Down
Loading

0 comments on commit a8ee78c

Please sign in to comment.