utils

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFreePort added in v0.3.0

func GetFreePort() (int, error)

GetFreePort asks the OS to allocate an unused port on localhost. Note: There is a small race window between when the port is returned and when it is actually used by the caller. In high-contention scenarios, the port may be taken by another process.

func IsFromLocalhost

func IsFromLocalhost(req *http.Request) bool

func MapToStruct

func MapToStruct[T any](m map[string]any) *T

MapToStruct deserializes a map[string]any back into a typed struct pointer. Returns nil if m is nil or empty.

func PrettyJSON

func PrettyJSON(v any) json.RawMessage

func RequestHost added in v0.2.1

func RequestHost(req *http.Request) string

func StructToMap

func StructToMap[T any](v *T) map[string]any

StructToMap serializes a pointer to any json-tagged struct into a map[string]any. Returns nil if v is nil.

func StructToMapLC added in v0.1.0

func StructToMapLC(v any, opts ...Option) map[string]any

StructToMapLC converts a struct to map[string]any using reflection, converting PascalCase field names to camelCase (first character lowercased). Embedded structs are flattened into the parent map. Optional behaviors (ID suffix conversion, omit-empty) can be enabled via Option. Returns nil if v is nil or not a struct.

func WriteJSONResponse

func WriteJSONResponse(w http.ResponseWriter, status int, v any) error

Types

type Option added in v0.1.1

type Option func(*options)

Option configures the behavior of StructToMapLC.

func WithIDSuffix added in v0.1.1

func WithIDSuffix() Option

WithIDSuffix converts field names ending in "ID" to end in "Id". e.g. "UserID" becomes "userId" instead of "userID".

func WithNilMapToEmpty added in v0.1.2

func WithNilMapToEmpty() Option

WithNilMapToEmpty converts nil map fields to empty maps {} instead of null.

func WithNilSliceToEmpty added in v0.1.2

func WithNilSliceToEmpty() Option

WithNilSliceToEmpty converts nil slice fields to empty slices [] instead of null.

func WithOmitEmpty added in v0.1.1

func WithOmitEmpty() Option

WithOmitEmpty skips fields with empty values: empty string, nil pointer, nil or zero-length slice. Numeric and boolean fields are never omitted.

type SplitLevelHandler

type SplitLevelHandler struct {
	StdHandler slog.Handler
	ErrHandler slog.Handler
}

SplitLevelHandler routes log records to stdout for levels below Error, and to stderr for Error and above.

func (*SplitLevelHandler) Enabled

func (h *SplitLevelHandler) Enabled(_ context.Context, level slog.Level) bool

func (*SplitLevelHandler) Handle

func (h *SplitLevelHandler) Handle(ctx context.Context, r slog.Record) error

func (*SplitLevelHandler) WithAttrs

func (h *SplitLevelHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*SplitLevelHandler) WithGroup

func (h *SplitLevelHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

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