utils

package
v0.8.4-b3 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxRecurDepth is the maximum recursion depth for nested environment variables.
	MaxRecurDepth = 3
)

Variables

This section is empty.

Functions

func CreateFileHandler added in v0.8.4

func CreateFileHandler(logFile string) (*os.File, error)

func CreateLogger

func CreateLogger(logFile string) (logger *slog.Logger, err error)

CreateLogger create logger by log file If log file directory not exist, create it

func GetElemType

func GetElemType(valueOf reflect.Value) reflect.Type

func GetElemValue

func GetElemValue(valueOf reflect.Value) reflect.Value

func GetFieldNames

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

func GetTableID

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

func GetTableModel

func GetTableModel(fieldOf reflect.Value) reflect.Value

func GetTagValue

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

func HasTagValue

func HasTagValue(tag string, key string) bool

func IsFieldHasSchema

func IsFieldHasSchema(valueOf reflect.Value, i int) bool

IsFieldHasSchema check if field has schema tag or schema suffix

func IsTableModel

func IsTableModel(fieldOf reflect.Value) bool

func MakeDirForFile

func MakeDirForFile(filename string) (err error)

MakeDirForFile create directory for file if not exist

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 parse table name by type

func ParseTableNameByValue

func ParseTableNameByValue(valueOf reflect.Value) string

ParseTableNameByValue parse table name by value

func TableNameMethod

func TableNameMethod(valueOf reflect.Value) string

TableNameMethod try to get table name from method TableNames If method TableNames is not found, return empty string

func TableNamePattern

func TableNamePattern(name string) string

TableNamePattern is the default name patterning for mapping struct to table

func TitleCase

func TitleCase(word string) string

func ToSnakeCase

func ToSnakeCase(name string) string

ToSnakeCase convert camelCase or PascalCase to snake_case

Types

type CoMap

type CoMap[K int | int64 | string, V any] struct {
	// contains filtered or unexported fields
}

CoMap is a thread-safe concurrent map with read-write locking.

func NewCoMap

func NewCoMap[K int | int64 | string, V any]() *CoMap[K, V]

func (*CoMap[K, V]) Delete

func (m *CoMap[K, V]) Delete(key K)

func (*CoMap[K, V]) Each

func (m *CoMap[K, V]) Each() iter.Seq2[K, *V]

func (*CoMap[K, V]) Get

func (m *CoMap[K, V]) Get(key K) (*V, bool)

func (*CoMap[K, V]) Keys

func (m *CoMap[K, V]) Keys() []K

func (*CoMap[K, V]) Set

func (m *CoMap[K, V]) Set(key K, value *V)

func (*CoMap[K, V]) Size

func (m *CoMap[K, V]) Size() int

func (*CoMap[K, V]) Update

func (m *CoMap[K, V]) Update(data map[K]*V)

type Entry added in v0.8.4

type Entry struct {
	Key   string
	Value string
}

Entry represents an environment variable entry with a key and a value.

func (*Entry) Bool added in v0.8.4

func (t *Entry) Bool() bool

Bool converts the entry's value to a boolean. It supports "yes", "no", "true", and "false" as valid boolean values. If the conversion fails, it returns false.

func (*Entry) Int added in v0.8.4

func (t *Entry) Int() int

Int converts the entry's value to an integer. If the conversion fails, it returns 0.

func (*Entry) Int64 added in v0.8.4

func (t *Entry) Int64() int64

Int64 converts the entry's value to a 64-bit integer. If the conversion fails, it returns 0.

func (*Entry) Str added in v0.8.4

func (t *Entry) Str() string

Str returns the string value of the entry.

type Environ added in v0.8.4

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

Environ represents an environment variable manager. It stores environment variables in a map and supports loading from a file and the system environment.

func NewEnv added in v0.8.4

func NewEnv() *Environ

NewEnv creates a new Environ instance and loads environment variables from the default .env file.

func NewEnvWithFile added in v0.8.4

func NewEnvWithFile(filename string) *Environ

NewEnvWithFile creates a new Environ instance and loads environment variables from the specified file. It initializes the storage map and attempts to Load the file. If the file cannot be opened or read, the error is ignored.

func (*Environ) Get added in v0.8.4

func (v *Environ) Get(key string) string

Get retrieves and expands the value of an environment variable by key.

func (*Environ) GetBool added in v0.8.4

func (v *Environ) GetBool(key string, fallback ...bool) bool

GetBool retrieves the boolean value of an environment variable by key. It supports "yes", "no", "true", and "false" as valid boolean values. If the variable is not found or the value cannot be converted to a boolean, it returns the fallback value.

func (*Environ) GetInt added in v0.8.4

func (v *Environ) GetInt(key string, fallback ...int) int

GetInt retrieves the integer value of an environment variable by key. If the variable is not found or the value cannot be converted to an integer, it returns the fallback value.

func (*Environ) GetInt64 added in v0.8.4

func (v *Environ) GetInt64(key string, fallback ...int64) int64

GetInt64 retrieves the 64-bit integer value of an environment variable by key. If the variable is not found or the value cannot be converted to a 64-bit integer, it returns the fallback value.

func (*Environ) GetStr added in v0.8.4

func (v *Environ) GetStr(key string, fallback ...string) string

GetStr retrieves the string value of an environment variable by key. If the variable is not found, it returns the fallback value.

func (*Environ) Load added in v0.8.4

func (v *Environ) Load(reader io.ReadCloser, err error) error

Load loads environment variables from a file and stores them in the internal storage.

func (*Environ) Lookup added in v0.8.4

func (v *Environ) Lookup(key string) (Entry, bool)

Lookup searches for an environment variable by key. It first checks the internal storage. If not found, it checks the system environment variables. If the variable is found, it returns the Entry and true; otherwise, it returns an empty Entry and false. If the variable is found in the system environment, it is added to the internal storage.

func (*Environ) ScanLines added in v0.8.4

func (v *Environ) ScanLines(reader io.Reader) error

ScanLines reads environment variables from a reader and stores them in the internal storage. It skips comments (lines starting with #) and empty lines. It splits each line into key-value pairs using the first '=' character. It trims whitespace from keys and values and removes surrounding quotes if present. If there is an error reading the file, it returns the error.

Jump to

Keyboard shortcuts

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