utils

package
v0.9.9-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultAbbreviations added in v0.9.4

func DefaultAbbreviations() map[string]string

DefaultAbbreviations returns the default set of common abbreviations.

func GetElemType

func GetElemType(valueOf reflect.Value) reflect.Type

GetElemType returns the element type of a pointer If the value is a pointer, it returns the element type Otherwise, it returns the type itself

func GetElemValue

func GetElemValue(valueOf reflect.Value) reflect.Value

GetElemValue returns the element value of a pointer If the value is a non-nil pointer, it returns the element Otherwise, it returns the value itself

func GetFieldNames

func GetFieldNames(typeOf reflect.Type) (names []string)

GetFieldNames returns a slice of field names for the given struct type. It only includes exported fields.

func GetTableID

func GetTableID(typeOf reflect.Type) (reflect.StructField, bool)

GetTableID returns the struct field with the name "ID" in the given table model type. If no such field exists, it returns an empty StructField and false.

func GetTableModel

func GetTableModel(fieldOf reflect.Value) reflect.Value

GetTableModel returns the "Model" field of the given table model field. If the field is not a table model, or if the "Model" field does not exist, it returns an invalid reflect.Value.

func GetTagValue

func GetTagValue(tag string, key string) (string, bool)

GetTagValue gets tag value by key It returns the value and a boolean indicating if the key was found Supports both semicolon and comma as separators, and both "key:value" and "key=value" formats

func HasTagValue

func HasTagValue(tag string, key string) bool

HasTagValue checks if tag has key value It returns true if the tag contains the specified key Supports both semicolon and comma as separators

func IsTableModel

func IsTableModel(fieldOf reflect.Value) bool

IsTableModel checks if the given field is a table model. A table model is a struct type that has a field named "Model" of type *T.

func LoadAbbreviationsFromData added in v0.9.8

func LoadAbbreviationsFromData(data []byte) error

LoadAbbreviationsFromData loads custom abbreviations from JSON bytes. The data should contain a JSON object mapping Go abbreviation names to their snake_case forms, e.g. {"ETag": "etag", "CRM": "crm"}.

func LoadAbbreviationsFromFile added in v0.9.8

func LoadAbbreviationsFromFile(path string) error

LoadAbbreviationsFromFile loads custom abbreviations from a JSON file. The file should contain a JSON object mapping Go abbreviation names to their snake_case forms, e.g. {"ETag": "etag", "CRM": "crm"}. If the file does not exist, no error is returned.

func ParseSchemaTag

func ParseSchemaTag(tag string) (schema, prefix string)

ParseSchemaTag parses the schema tag and returns the schema name and table prefix. Example: "public;prefix:t_" returns ("public", "t_") Example: "auth" returns ("auth", "")

func ParseTableNameByType

func ParseTableNameByType(typeOf reflect.Type) string

ParseTableNameByType parses table name by type It uses the TableName method if available, otherwise uses the type name

func ParseTableNameByValue

func ParseTableNameByValue(valueOf reflect.Value) string

ParseTableNameByValue parses table name by value If method TableName is found, return its value Otherwise, parse table name by type name

func RegisterAbbreviations added in v0.9.4

func RegisterAbbreviations(abbrs map[string]string)

RegisterAbbreviations adds custom abbreviations for ToSnakeCase. Abbreviations are merged with the defaults; custom ones override defaults. For example, RegisterAbbreviations(map[string]string{"CRM": "crm"}) makes "CRMSystem" convert to "crm_system".

func TableNameMethod

func TableNameMethod(valueOf reflect.Value) string

TableNameMethod tries to get table name from method TableName If method TableName is not found, return empty string

func TableNamePattern

func TableNamePattern(name string) string

TableNamePattern is the default name patterning for mapping struct to table It converts the name to snake_case format

func ToAnySlice added in v0.8.6

func ToAnySlice(value any) ([]any, int)

ToAnySlice converts a value to a slice of any type.

func ToCamelCase added in v0.8.6

func ToCamelCase(s string) string

ToCamelCase converts snake_case to CamelCase

func ToPlural added in v0.8.6

func ToPlural(name string) string

ToPlural converts a singular name to its plural form.

func ToSingular added in v0.8.6

func ToSingular(name string) string

ToSingular converts a plural name to its singular form.

func ToSnakeCase

func ToSnakeCase(word string) string

ToSnakeCase converts camelCase or PascalCase to snake_case. Registered abbreviations are kept together (e.g. "PRs" -> "prs" not "p_rs").

func ToTitleCase added in v0.8.6

func ToTitleCase(word string) string

ToTitleCase converts a string to title case It capitalizes the first letter of each word

Types

type Imports added in v0.8.6

type Imports struct {
	// contains filtered or unexported fields
}

func NewImports added in v0.8.6

func NewImports() *Imports

func (*Imports) AddStdPackage added in v0.8.6

func (s *Imports) AddStdPackage(pkg string)

func (*Imports) AddThirdPackage added in v0.8.6

func (s *Imports) AddThirdPackage(pkg string)

func (*Imports) GetStdPackage added in v0.8.6

func (s *Imports) GetStdPackage() []string

func (*Imports) GetThirdPackage added in v0.8.6

func (s *Imports) GetThirdPackage() []string

func (*Imports) String added in v0.8.6

func (s *Imports) String() string

func (*Imports) WriteTo added in v0.8.6

func (s *Imports) WriteTo(buf *bytes.Buffer)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL