cwsbase

package
v0.3.24 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: MIT Imports: 19 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AvoidNilString

func AvoidNilString(s *string) string

AvoidNilString safely dereferences a string pointer, returning empty string if nil

func ConvertArrayAnyToArrayString

func ConvertArrayAnyToArrayString(arr []any) []string

ConvertArrayAnyToArrayString converts a slice of any to a slice of strings Assumes all elements in the input slice can be cast to string

func DecryptToMap

func DecryptToMap(input string) (map[string]any, error)

DecryptToMap decrypts a base64 encoded string back to a map[string]any using AES-CBC with PKCS5 padding The decryption uses keys from environment variables CRYPTO_KEY_HEX and CRYPTO_IV_HEX

func EncryptMap

func EncryptMap(input map[string]any) (string, error)

EncryptMap encrypts a map[string]any into a base64 encoded string using AES-CBC with PKCS5 padding The encryption uses keys from environment variables CRYPTO_KEY_HEX and CRYPTO_IV_HEX

func GetCurrentTimestamp

func GetCurrentTimestamp() int64

GetCurrentTimestamp returns the current UTC timestamp as an int64

func GetCurrentTimestampString

func GetCurrentTimestampString() string

GetCurrentTimestampString returns the current UTC timestamp as a string

func GetEnv

func GetEnv[T any](key string, defaultVal ...T) T

GetEnv retrieves an environment variable and converts it to the specified type T Supports bool, int, int32, int64, float32, float64, and string types If the environment variable is not set and no default value is provided, the program will exit with a fatal error

func GetLocalizationMessage

func GetLocalizationMessage(code LocalizationCode, strs ...any) string

GetLocalizationMessage retrieves a localized message by code and language The language is determined by the LOCALIZATION_LANGUAGE environment variable (defaults to "en") If additional string parameters are provided, they are formatted into the message using sprintf

func IntToDateTime

func IntToDateTime(unixTime int64) string

IntToDateTime converts a Unix timestamp to a formatted date-time string Returns the time in "2006-01-02 15:04:05" format

func IsStringInSlice added in v0.3.7

func IsStringInSlice(val string, ss []string) bool

IsStringInSlice checks if a string exists in a slice of strings Returns false if the input string is empty

func MaxInt32

func MaxInt32(a int32, b int32) int32

MaxInt32 returns the maximum of two int32 values

func ReadHttpBody

func ReadHttpBody(response http.Response) (map[string]any, error)

ReadHttpBody reads and parses an HTTP response body as JSON into a map[string]any Automatically closes the response body when finished

func SendHttpRequestJson

func SendHttpRequestJson(c context.Context, method string, url string, jsonBody map[string]any, header map[string]string) (*http.Response, error)

SendHttpRequestJson sends an HTTP request with JSON body and custom headers The jsonBody is marshaled to JSON and sent as the request body

func StringToCapital

func StringToCapital(s string) string

StringToCapital capitalizes the first character of a string Returns empty string if input is empty

func StructToMap added in v0.3.7

func StructToMap(obj any) (map[string]any, error)

StructToMap converts any struct to a map[string]any using JSON marshaling/unmarshaling This preserves JSON tags and handles nested structures

func StructToMapEscapeEmpty

func StructToMapEscapeEmpty(obj any) (map[string]any, error)

StructToMapEscapeEmpty converts a struct to a map[string]any while excluding empty values Empty values include nil, empty string, and zero integer values Returns an error if the input is not a struct

func ToSnakeCase added in v0.3.5

func ToSnakeCase(str string) string

ToSnakeCase converts CamelCase or PascalCase strings to snake_case Example: "HelloWorld" becomes "hello_world"

func UpdateLocalizationData

func UpdateLocalizationData(jsonData []byte) error

UpdateLocalizationData updates the global localization map with new data from JSON This function is thread-safe and merges new data with existing localization data The JSON structure should be: {"language": {"code": "message"}}

Types

type EnvironmentInfo

type EnvironmentInfo struct {
	// Env represents the current environment (e.g., "test", "prod")
	Env string
	// DebugMode indicates if debug mode is enabled
	DebugMode bool
	// IsLocal indicates if the application is running in local development mode
	IsLocal bool
}

EnvironmentInfo holds application environment configuration

func GetEnvironmentInfo

func GetEnvironmentInfo() EnvironmentInfo

GetEnvironmentInfo retrieves current environment configuration from environment variables Reads ENV, DEBUG, and IS_LOCAL environment variables

type LocalizationCode

type LocalizationCode string

LocalizationCode represents a unique identifier for localized messages

type LocalizationLanguage

type LocalizationLanguage string

LocalizationLanguage represents a language identifier for localization

const (
	// English language identifier
	English LocalizationLanguage = "en"
	// Taiwanese Traditional Chinese language identifier
	Taiwanese LocalizationLanguage = "zh_tw"
	// Simplified Chinese language identifier
	Chinese LocalizationLanguage = "zh_cn"
)

Supported localization languages

type Stack added in v0.3.0

type Stack[T any] struct {
	// contains filtered or unexported fields
}

Stack is a generic LIFO (Last In, First Out) data structure

func New added in v0.3.0

func New[T any]() Stack[T]

New creates a new empty stack of type T

func (*Stack[T]) Len added in v0.3.0

func (this *Stack[T]) Len() int

Len returns the number of items currently in the stack

func (*Stack[T]) Peek added in v0.3.0

func (this *Stack[T]) Peek() *T

Peek returns a pointer to the top item on the stack without removing it Returns nil if the stack is empty

func (*Stack[T]) Pop added in v0.3.0

func (this *Stack[T]) Pop() *T

Pop removes and returns a pointer to the top item of the stack Returns nil if the stack is empty

func (*Stack[T]) Push added in v0.3.0

func (this *Stack[T]) Push(value *T)

Push adds a new value to the top of the stack

Jump to

Keyboard shortcuts

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