Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Huge refactor so that tables can be generic #302

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading