Documentation
¶
Index ¶
- func ConvertToNumericType(value string, kind reflect.Kind) (interface{}, error)
- func ExtractColumnFromBunTag(tag string) string
- func ExtractColumnFromGormTag(tag string) string
- func ExtractSourceColumn(colName string) string
- func ExtractTableNameOnly(fullName string) string
- func GetColumnTypeFromModel(model interface{}, colName string) reflect.Kind
- func GetModelColumns(model any) []string
- func GetPrimaryKeyName(model any) string
- func GetPrimaryKeyValue(model any) any
- func GetRelationModel(model interface{}, fieldName string) interface{}
- func GetSQLModelColumns(model any) []string
- func IsColumnWritable(model any, columnName string) bool
- func IsNumericType(kind reflect.Kind) bool
- func IsNumericValue(value string) bool
- func IsStringType(kind reflect.Kind) bool
- func Len(v any) int
- func MapToStruct(dataMap map[string]interface{}, target interface{}) error
- func ToSnakeCase(s string) string
- type ModelFieldDetail
- type PrimaryKeyNameProvider
- type RelationType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertToNumericType ¶ added in v0.0.55
ConvertToNumericType converts a string value to the appropriate numeric type
func ExtractColumnFromBunTag ¶
ExtractColumnFromBunTag extracts the column name from a bun tag Example: "id,pk" -> "id" Example: ",pk" -> "" (will fall back to json tag)
func ExtractColumnFromGormTag ¶
ExtractColumnFromGormTag extracts the column name from a gorm tag Example: "column:id;primaryKey" -> "id"
func ExtractSourceColumn ¶ added in v0.0.55
ExtractSourceColumn extracts the base column name from PostgreSQL JSON operators Examples:
- "columna->>'val'" returns "columna"
- "columna->'key'" returns "columna"
- "columna" returns "columna"
- "table.columna->>'val'" returns "table.columna"
func ExtractTableNameOnly ¶ added in v0.0.57
ExtractTableNameOnly extracts the table name from a fully qualified table reference. It removes any schema prefix (e.g., "schema.table" -> "table") and truncates at the first delimiter (comma, space, tab, or newline). If the input contains multiple dots, it returns everything after the last dot up to the first delimiter.
func GetColumnTypeFromModel ¶ added in v0.0.55
GetColumnTypeFromModel uses reflection to determine the Go type of a column in a model
func GetModelColumns ¶
GetModelColumns extracts all column names from a model using reflection It checks bun tags first, then gorm tags, then json tags, and finally falls back to lowercase field names This function recursively processes embedded structs to include their fields
func GetPrimaryKeyName ¶
GetPrimaryKeyName extracts the primary key column name from a model It first checks if the model implements PrimaryKeyNameProvider (GetIDName method) Falls back to reflection to find bun:",pk" tag, then gorm:"primaryKey" tag
func GetPrimaryKeyValue ¶ added in v0.0.31
GetPrimaryKeyValue extracts the primary key value from a model instance Returns the value of the primary key field
func GetRelationModel ¶ added in v0.0.55
func GetRelationModel(model interface{}, fieldName string) interface{}
GetRelationModel gets the model type for a relation field It searches for the field by name in the following order (case-insensitive): 1. Actual field name 2. Bun tag name (if exists) 3. Gorm tag name (if exists) 4. JSON tag name (if exists)
Supports recursive field paths using dot notation (e.g., "MAL.MAL.DEF") For nested fields, it traverses through each level of the struct hierarchy
func GetSQLModelColumns ¶ added in v0.0.52
GetSQLModelColumns extracts column names that have valid SQL field mappings This function only returns columns that: 1. Have bun or gorm tags (not just json tags) 2. Are not relations (no rel:, join:, foreignKey, references, many2many tags) 3. Are not scan-only embedded fields
func IsColumnWritable ¶ added in v0.0.32
IsColumnWritable checks if a column can be written to in the database For bun: returns false if the field has "scanonly" tag For gorm: returns false if the field has "<-:false" or "->" (read-only) tag This function recursively searches embedded structs
func IsNumericType ¶ added in v0.0.55
IsNumericType checks if a reflect.Kind is a numeric type
func IsNumericValue ¶ added in v0.0.55
IsNumericValue checks if a string value can be parsed as a number
func IsStringType ¶ added in v0.0.55
IsStringType checks if a reflect.Kind is a string type
func MapToStruct ¶ added in v0.0.109
MapToStruct populates a struct from a map while preserving custom types It uses reflection to set struct fields based on map keys, matching by: 1. Bun tag column name 2. Gorm tag column name 3. JSON tag name 4. Field name (case-insensitive) This preserves custom types that implement driver.Valuer like SqlJSONB
func ToSnakeCase ¶ added in v0.0.55
ToSnakeCase converts a string from CamelCase to snake_case
Types ¶
type ModelFieldDetail ¶
type ModelFieldDetail struct {
Name string `json:"name"`
DataType string `json:"datatype"`
SQLName string `json:"sqlname"`
SQLDataType string `json:"sqldatatype"`
SQLKey string `json:"sqlkey"`
Nullable bool `json:"nullable"`
FieldValue reflect.Value `json:"-"`
}
func GetModelColumnDetail ¶
func GetModelColumnDetail(record reflect.Value) []ModelFieldDetail
GetModelColumnDetail - Get a list of columns in the SQL declaration of the model This function recursively processes embedded structs to include their fields
type PrimaryKeyNameProvider ¶ added in v0.0.23
type PrimaryKeyNameProvider interface {
GetIDName() string
}
type RelationType ¶ added in v0.0.95
type RelationType string
RelationType represents the type of database relationship
const ( RelationHasMany RelationType = "has-many" // 1:N - use separate query RelationBelongsTo RelationType = "belongs-to" // N:1 - use JOIN RelationHasOne RelationType = "has-one" // 1:1 - use JOIN RelationManyToMany RelationType = "many-to-many" // M:N - use separate query RelationUnknown RelationType = "unknown" )
func GetRelationType ¶ added in v0.0.95
func GetRelationType(model interface{}, fieldName string) RelationType
GetRelationType inspects the model's struct tags to determine the relationship type It checks both Bun and GORM tags to identify the relationship cardinality
func (RelationType) ShouldUseJoin ¶ added in v0.0.95
func (rt RelationType) ShouldUseJoin() bool
ShouldUseJoin returns true if the relation type should use a JOIN instead of separate query