utils

package
v0.8.7 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxRecurDepth is the maximum recursion depth for nested environment variables
	// It limits how deeply nested environment variables can be expanded
	MaxRecurDepth = 3
)

Variables

This section is empty.

Functions

func CreateFileHandler added in v0.8.4

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

CreateFileHandler creates a file handler for logging It ensures the directory exists and opens the file for writing Returns the file handler or an error

func CreateLogger

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

CreateLogger creates a logger by log file If log file directory doesn't exist, it creates it Returns the created logger or an error

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

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

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 MakeDirForFile

func MakeDirForFile(filename string) (err error)

MakeDirForFile creates directory for file if it doesn't exist It creates all necessary parent directories Returns an error if directory creation fails

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 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

func ToSnakeCaseV0 added in v0.8.6

func ToSnakeCaseV0(word string) string

ToSnakeCaseV0 converts camelCase or PascalCase to snake_case in early version of golang

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

func UniqueStrings added in v0.8.6

func UniqueStrings(words []string) []string

UniqueStrings sort and unique strings in the slice.

func WriteToFile added in v0.8.6

func WriteToFile(buf *bytes.Buffer, outputPath string) error

WriteToFile writes the formatted content to a file.

Types

type CoMap

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

func NewCoMap

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

NewCoMap creates a new thread-safe concurrent map It initializes the map with the specified key and value types

func NewCoMapSize added in v0.8.6

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

NewCoMapSize creates a new thread-safe concurrent map It initializes the map with the specified key and value types

func (*CoMap[K, V]) Delete

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

Delete removes a value from the map by key It uses a write lock for thread safety

func (*CoMap[K, V]) Each

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

Each returns an iterator over all key-value pairs in the map It uses a read lock for thread safety

func (*CoMap[K, V]) Get

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

Get retrieves a value from the map by key It returns the value and a boolean indicating if the key was found

func (*CoMap[K, V]) Keys

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

Keys returns all keys in the map as a slice

func (*CoMap[K, V]) Set

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

Set adds or updates a value in the map

func (*CoMap[K, V]) Size

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

Size returns the number of elements in the map

func (*CoMap[K, V]) SortedKeys added in v0.8.6

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

SortedKeys returns sorted keys from the map

func (*CoMap[K, V]) Update

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

Update updates the map with multiple key-value pairs It copies all entries from the provided map to the current map

type Entry added in v0.8.4

type Entry struct {
	Key   string // Environment variable key
	Value string // Environment variable value
}

Entry represents an environment variable entry with a key and a value It stores a single environment variable's key-value pair

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 It returns the raw string value of the environment variable

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 It initializes the environment manager with 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 It recursively expands nested environment variables up to MaxRecurDepth

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 It reads from the provided reader and handles closing the reader

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

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