refactor with improved developer ux in mind
- Most functions now live directly in the dbx package - dbxp and dbxm are now ONLY the few functions that cannot be shared
This commit is contained in:
36
internal/dbxshared/dbxshared.go
Normal file
36
internal/dbxshared/dbxshared.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package dbxshared
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DestName returns the name of the type passed as `destTypeStruct` as a string,
|
||||
// normalized for compatibility with the Jet QRM.
|
||||
func DestName(destTypeStruct any, path ...string) string {
|
||||
v := reflect.ValueOf(destTypeStruct)
|
||||
for v.Kind() == reflect.Ptr {
|
||||
v = v.Elem()
|
||||
}
|
||||
|
||||
destIdent := v.Type().String()
|
||||
destIdent = destIdent[strings.LastIndex(destIdent, ".")+1:]
|
||||
|
||||
for i, p := range path {
|
||||
if v.Kind() != reflect.Struct {
|
||||
DBModule.Logger().Error("DestName: path parent is not a struct", "path", destIdent+"."+strings.Join(path[:i+1], "."))
|
||||
return ""
|
||||
}
|
||||
|
||||
v = v.FieldByName(p)
|
||||
|
||||
if !v.IsValid() {
|
||||
DBModule.Logger().Error("DestName: field does not exist", "path", destIdent+"."+strings.Join(path[:i+1], "."))
|
||||
return ""
|
||||
}
|
||||
|
||||
destIdent += "." + p
|
||||
}
|
||||
|
||||
return destIdent
|
||||
}
|
||||
30
internal/dbxshared/logger.go
Normal file
30
internal/dbxshared/logger.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package dbxshared
|
||||
|
||||
type Logger interface {
|
||||
InitLogger()
|
||||
}
|
||||
|
||||
type dialectString interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
var loggerRegistry = make(map[string]Logger)
|
||||
|
||||
// RegisterLogger allows dialect-specific loggers to be registered.
|
||||
func RegisterLogger(dialect dialectString, logger Logger) {
|
||||
dialectStr := dialect.String()
|
||||
if _, exists := loggerRegistry[dialectStr]; exists {
|
||||
panic("Logger for dialect already registered: " + dialectStr)
|
||||
}
|
||||
loggerRegistry[dialectStr] = logger
|
||||
}
|
||||
|
||||
// InitLogger initializes the logger for a specific dialect.
|
||||
func InitLogger(dialect dialectString) {
|
||||
dialectStr := dialect.String()
|
||||
logger, exists := loggerRegistry[dialectStr]
|
||||
if !exists {
|
||||
panic("No logger registered for dialect: " + dialectStr)
|
||||
}
|
||||
logger.InitLogger()
|
||||
}
|
||||
5
internal/dbxshared/module.go
Normal file
5
internal/dbxshared/module.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package dbxshared
|
||||
|
||||
import "gitea.auvem.com/go-toolkit/app"
|
||||
|
||||
var DBModule *app.Module
|
||||
Reference in New Issue
Block a user