go_objectutils

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

README

Go Object Utils

Go Go Report Card GoDoc

Extensive utilities for extracting typed values from map[string]interface{} (commonly used for JSON objects in Go) with type safety, default values, and error handling.

This library is designed to help working with loosely typed data structures (like parsed JSON or YAML) in a safer and more ergonomic way, providing a bridge between dynamic data and Go's static typing.

Installation

go get github.com/arran4/go-objectutils

Concept: Error Handling Patterns

This library offers three primary ways to access data, allowing you to choose the best strategy for your specific use case (e.g., failing fast, safe handling, or default fallbacks).

1. Error Returning (Get*)

Use Case: General application logic where missing data is expected or recoverable.

These functions return the value and an error. The error will be *MissingFieldError if the key doesn't exist, or *InvalidTypeError if the value cannot be converted to the target type.

val, err := go_objectutils.GetString(data, "key")
if err != nil {
    // Handle error
}
2. Panic (MustGet*)

Use Case: Configuration loading, scripts, or scenarios where the application cannot proceed without this data. Fails fast.

These functions return the value directly but will panic if an error occurs. This keeps the code concise when safety is guaranteed by other means or when crashing is the desired behavior on failure.

val := go_objectutils.MustGetString(data, "key")
3. Default Values (Get*OrDefault)

Use Case: Optional configuration, UI rendering where defaults are acceptable.

These functions return the value if found and valid; otherwise, they return the provided default value. Errors are swallowed.

val := go_objectutils.GetStringOrDefault(data, "key", "default")
4. Pointers (Get*Ptr)

Use Case: Distinguishing between a "zero value" (e.g., "", 0, false) and a "missing/null" value.

valPtr, err := go_objectutils.GetStringPtr(data, "key")
if valPtr == nil {
    // Key was missing or null
}

Function Listing

Strings
Function Description
GetString Returns string or error.
MustGetString Returns string or panics.
GetStringOrDefault Returns string or default value.
GetStringPtr Returns *string or error.
MustGetStringPtr Returns *string or panics.
GetStringPtrOrDefault Returns *string or default value.
GetStringRegex Returns string validated by regex or error.
MustGetStringRegex Returns string validated by regex or panics.
GetStringRegexOrDefault Returns string validated by regex or default value.
GetStringRegexPtr Returns *string validated by regex or error.
MustGetStringRegexPtr Returns *string validated by regex or panics.
GetStringRegexPtrOrDefault Returns *string validated by regex or default value.
Numbers (Generics)

Supports int, int8...int64, uint...uint64, float32, float64. Note: Also handles automatic conversion from string representations of numbers.

Function Description
GetNumber[T] Returns number of type T or error.
MustGetNumber[T] Returns number of type T or panics.
GetNumberOrDefault[T] Returns number of type T or default value.
GetNumberPtr[T] Returns *T or error.
MustGetNumberPtr[T] Returns *T or panics.
GetNumberPtrOrDefault[T] Returns *T or default value.
Booleans
Function Description
GetBoolean Returns bool or error.
MustGetBoolean Returns bool or panics.
GetBooleanOrDefault Returns bool or default value.
GetBooleanPtr Returns *bool or error.
MustGetBooleanPtr Returns *bool or panics.
GetBooleanPtrOrDefault Returns *bool or default value.
Dates

Parses time.Time from strings (RFC3339) or timestamps (int/float milliseconds).

Function Description
GetDate Returns time.Time or error.
MustGetDate Returns time.Time or panics.
GetDateOrDefault Returns time.Time or default value.
GetDatePtr Returns *time.Time or error.
MustGetDatePtr Returns *time.Time or panics.
GetDatePtrOrDefault Returns *time.Time or default value.
BigInt

Extracts math/big.Int from strings or numbers.

Function Description
GetBigInt Returns *big.Int or error.
MustGetBigInt Returns *big.Int or panics.
GetBigIntOrDefault Returns *big.Int or default value.
Arrays / Slices
Function Description
GetStringArray Returns []string or error.
MustGetStringArray Returns []string or panics.
GetStringArrayOrDefault Returns []string or default value.
GetDateArray Returns []time.Time or error.
MustGetDateArray Returns []time.Time or panics.
GetDateArrayOrDefault Returns []time.Time or default value.
GetObjectArray[T] Returns []T or error. Useful for lists of sub-objects.
MustGetObjectArray[T] Returns []T or panics.
GetObjectArrayOrDefault[T] Returns []T or default value.
Objects / Maps
Function Description
GetObject[T] Returns object cast to type T (usually map[string]interface{}) or error.
MustGetObject[T] Returns object cast to type T or panics.
GetObjectOrDefault[T] Returns object cast to type T or default value.
GetObjectPtr[T] Returns *T or error.
MustGetObjectPtr[T] Returns *T or panics.
GetObjectPtrOrDefault[T] Returns *T or default value.
GetMap[K, V] Returns map[K]V or error.
MustGetMap[K, V] Returns map[K]V or panics.

Use Cases & Examples

Scenario 1: Parsing Configuration

When loading a configuration file (e.g., parsed from JSON), you often want to fail if required fields are missing and use defaults for others.

configData := map[string]interface{}{
    "host": "localhost",
    "port": 8080,
    // "timeout": missing, will use default
}

// Fail if host is missing
host := go_objectutils.MustGetString(configData, "host")

// Fail if port is missing or not a number
port := go_objectutils.MustGetNumber[int](configData, "port")

// Use default if timeout is missing
timeout := go_objectutils.GetNumberOrDefault[int](configData, "timeout", 30)
Scenario 2: Handling API Responses

When processing an external API response, you should handle potential schema changes gracefully using the error-returning functions.

response := map[string]interface{}{
    "user": map[string]interface{}{
        "id": "12345",
        "active": true,
    },
    "tags": []interface{}{"admin", "editor"},
}

// Safely access nested object
if userMap, err := go_objectutils.GetObject[map[string]interface{}](response, "user"); err == nil {
    // Note: ID is a string in JSON but we want an int
    id, _ := go_objectutils.GetNumber[int](userMap, "id")

    active := go_objectutils.GetBooleanOrDefault(userMap, "active", false)
    fmt.Printf("User ID: %d, Active: %v\n", id, active)
}

// Safely access array
tags, _ := go_objectutils.GetStringArrayOrDefault(response, "tags", []string{})
Scenario 3: Nullable Fields (Pointers)

Use pointer variants to check if a field was explicitly set to null or missing, versus just being the zero value.

updateData := map[string]interface{}{
    "description": "",   // User wants to clear description
    "age": nil,         // User didn't update age
}

// Check description
descPtr, _ := go_objectutils.GetStringPtr(updateData, "description")
if descPtr != nil {
    fmt.Printf("Updating description to: '%s'\n", *descPtr)
}

// Check age
agePtr, _ := go_objectutils.GetNumberPtr[int](updateData, "age")
if agePtr == nil {
    fmt.Println("Age not updated")
}
Scenario 4: Custom Object Mapping

You can use GetObjectArray with a constructor function approach (via legacy helpers or manual iteration) or simple type casting if your structures match.

data := map[string]interface{}{
    "users": []interface{}{
        map[string]interface{}{"name": "Alice"},
        map[string]interface{}{"name": "Bob"},
    },
}

// Extract list of user maps
users := go_objectutils.GetObjectArrayOrDefault[map[string]interface{}](data, "users", nil)

for _, u := range users {
    name := go_objectutils.GetStringOrDefault(u, "name", "Unknown")
    fmt.Println(name)
}

Legacy Functions

The library includes several legacy functions for backward compatibility. These are generally more verbose aliases or specific utility wrappers.

  • GetStringPropOrDefault -> GetStringOrDefault
  • GetNumberPropOrDefault -> GetNumberOrDefault
  • GetBooleanPropOrDefault -> GetBooleanOrDefault
  • GetDatePropOrDefault -> GetDateOrDefault
  • GetObjectPropOrDefault -> GetObjectOrDefault
  • GetStringPropOrThrow, GetNumberPropOrThrow, etc. -> Custom error message wrappers around Get*.
  • Get*PropOrDefaultFunction -> Uses a function to generate the default value (lazy evaluation).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBigInt

func GetBigInt(props map[string]interface{}, prop string) (*big.Int, error)

GetBigInt retrieves a big.Int property. It supports strings, int64, float64 (if integer).

func GetBigIntOrDefault

func GetBigIntOrDefault(props map[string]interface{}, prop string, defaultValue *big.Int) *big.Int

GetBigIntOrDefault retrieves a big.Int property or returns a default value.

func GetBigIntPropOrDefault

func GetBigIntPropOrDefault(props map[string]interface{}, prop string, defaultValue int64) int64

GetBigIntPropOrDefault aliases GetNumberOrDefault[int64]

func GetBigIntPropOrDefaultFunction

func GetBigIntPropOrDefaultFunction(props map[string]interface{}, prop string, defaultFunction func() int64) int64

GetBigIntPropOrDefaultFunction

func GetBigIntPropOrThrow

func GetBigIntPropOrThrow(props map[string]interface{}, prop string, message ...string) int64

GetBigIntPropOrThrow

func GetBoolean

func GetBoolean(props map[string]interface{}, prop string) (bool, error)

GetBoolean retrieves a boolean property.

func GetBooleanArray

func GetBooleanArray(props map[string]interface{}, prop string) ([]bool, error)

GetBooleanArray retrieves a boolean array property.

func GetBooleanArrayOrDefault

func GetBooleanArrayOrDefault(props map[string]interface{}, prop string, defaultValue []bool) []bool

GetBooleanArrayOrDefault retrieves a boolean array property or returns a default value.

func GetBooleanArrayPtr

func GetBooleanArrayPtr(props map[string]interface{}, prop string) (*[]bool, error)

GetBooleanArrayPtr retrieves a boolean array property as a pointer.

func GetBooleanArrayPtrOrDefault

func GetBooleanArrayPtrOrDefault(props map[string]interface{}, prop string, defaultValue *[]bool) *[]bool

GetBooleanArrayPtrOrDefault retrieves a boolean array property as a pointer or returns a default value.

func GetBooleanFunctionPropOrDefault

func GetBooleanFunctionPropOrDefault(props map[string]interface{}, prop string, constructorFunc func(interface{}) bool, defaultValue bool) bool

GetBooleanFunctionPropOrDefault constructs a boolean using a function or returns default.

func GetBooleanFunctionPropOrDefaultFunction

func GetBooleanFunctionPropOrDefaultFunction(props map[string]interface{}, prop string, constructorFunc func(interface{}) bool, defaultFunction func() bool) bool

GetBooleanFunctionPropOrDefaultFunction

func GetBooleanOrDefault

func GetBooleanOrDefault(props map[string]interface{}, prop string, defaultValue bool) bool

GetBooleanOrDefault retrieves a boolean property or returns a default value.

func GetBooleanPointerArray

func GetBooleanPointerArray(props map[string]interface{}, prop string) ([]*bool, error)

GetBooleanPointerArray retrieves a property as a slice of boolean pointers.

func GetBooleanPointerArrayPtr

func GetBooleanPointerArrayPtr(props map[string]interface{}, prop string) (*[]*bool, error)

GetBooleanPointerArrayPtr retrieves a property as a pointer to a slice of boolean pointers.

func GetBooleanPropOrDefault

func GetBooleanPropOrDefault(props map[string]interface{}, prop string, defaultValue bool) bool

GetBooleanPropOrDefault is an alias for GetBooleanOrDefault.

func GetBooleanPropOrDefaultFunction

func GetBooleanPropOrDefaultFunction(props map[string]interface{}, prop string, defaultFunction func() bool) bool

GetBooleanPropOrDefaultFunction retrieves a boolean property or returns a value from a default function.

func GetBooleanPropOrThrow

func GetBooleanPropOrThrow(props map[string]interface{}, prop string, message ...string) bool

GetBooleanPropOrThrow retrieves a boolean property or panics if missing/invalid.

func GetBooleanPtr

func GetBooleanPtr(props map[string]interface{}, prop string) (*bool, error)

GetBooleanPtr retrieves a boolean property as a pointer.

func GetBooleanPtrOrDefault

func GetBooleanPtrOrDefault(props map[string]interface{}, prop string, defaultValue *bool) *bool

GetBooleanPtrOrDefault retrieves a boolean property as a pointer or returns a default value.

func GetDate

func GetDate(props map[string]interface{}, prop string) (time.Time, error)

GetDate retrieves a date property.

func GetDateArray

func GetDateArray(props map[string]interface{}, prop string) ([]time.Time, error)

GetDateArray retrieves a date array property.

func GetDateArrayOrDefault

func GetDateArrayOrDefault(props map[string]interface{}, prop string, defaultValue []time.Time) []time.Time

GetDateArrayOrDefault retrieves a date array property or returns a default value.

func GetDateArrayPropOrDefault

func GetDateArrayPropOrDefault(props map[string]interface{}, prop string, defaultValue []time.Time) []time.Time

GetDateArrayPropOrDefault

func GetDateArrayPtr

func GetDateArrayPtr(props map[string]interface{}, prop string) (*[]time.Time, error)

GetDateArrayPtr retrieves a date array property as a pointer.

func GetDateArrayPtrOrDefault

func GetDateArrayPtrOrDefault(props map[string]interface{}, prop string, defaultValue *[]time.Time) *[]time.Time

GetDateArrayPtrOrDefault retrieves a date array property as a pointer or returns a default value.

func GetDateOrDefault

func GetDateOrDefault(props map[string]interface{}, prop string, defaultValue time.Time) time.Time

GetDateOrDefault retrieves a date property or returns a default value.

func GetDatePointerArray

func GetDatePointerArray(props map[string]interface{}, prop string) ([]*time.Time, error)

GetDatePointerArray retrieves a property as a slice of date pointers.

func GetDatePointerArrayPtr

func GetDatePointerArrayPtr(props map[string]interface{}, prop string) (*[]*time.Time, error)

GetDatePointerArrayPtr retrieves a property as a pointer to a slice of date pointers.

func GetDatePropOrDefault

func GetDatePropOrDefault(props map[string]interface{}, prop string, defaultValue time.Time) time.Time

GetDatePropOrDefault is an alias for GetDateOrDefault.

func GetDatePropOrDefaultFunction

func GetDatePropOrDefaultFunction(props map[string]interface{}, prop string, defaultFunction func() time.Time) time.Time

GetDatePropOrDefaultFunction

func GetDatePropOrThrow

func GetDatePropOrThrow(props map[string]interface{}, prop string, message ...string) time.Time

GetDatePropOrThrow

func GetDatePtr

func GetDatePtr(props map[string]interface{}, prop string) (*time.Time, error)

GetDatePtr retrieves a date property as a pointer.

func GetDatePtrOrDefault

func GetDatePtrOrDefault(props map[string]interface{}, prop string, defaultValue *time.Time) *time.Time

GetDatePtrOrDefault retrieves a date property as a pointer or returns a default value.

func GetMap

func GetMap[K comparable, V any](props map[string]interface{}, prop string) (map[K]V, error)

GetMap retrieves a map property.

func GetMapPropOrDefault

func GetMapPropOrDefault[K comparable, V any](props map[string]interface{}, prop string, defaultValue map[K]V) map[K]V

GetMapPropOrDefault

func GetNumber

func GetNumber[T NumberConstraint](props map[string]interface{}, prop string) (T, error)

GetNumber retrieves a numeric property.

func GetNumberArray

func GetNumberArray[T NumberConstraint](props map[string]interface{}, prop string) ([]T, error)

GetNumberArray retrieves a number array property.

func GetNumberArrayOrDefault

func GetNumberArrayOrDefault[T NumberConstraint](props map[string]interface{}, prop string, defaultValue []T) []T

GetNumberArrayOrDefault retrieves a number array property or returns a default value.

func GetNumberArrayPtr

func GetNumberArrayPtr[T NumberConstraint](props map[string]interface{}, prop string) (*[]T, error)

GetNumberArrayPtr retrieves a number array property as a pointer.

func GetNumberArrayPtrOrDefault

func GetNumberArrayPtrOrDefault[T NumberConstraint](props map[string]interface{}, prop string, defaultValue *[]T) *[]T

GetNumberArrayPtrOrDefault retrieves a number array property as a pointer or returns a default value.

func GetNumberOrDefault

func GetNumberOrDefault[T NumberConstraint](props map[string]interface{}, prop string, defaultValue T) T

GetNumberOrDefault retrieves a numeric property or returns a default value.

func GetNumberPointerArray

func GetNumberPointerArray[T NumberConstraint](props map[string]interface{}, prop string) ([]*T, error)

GetNumberPointerArray retrieves a property as a slice of number pointers.

func GetNumberPointerArrayPtr

func GetNumberPointerArrayPtr[T NumberConstraint](props map[string]interface{}, prop string) (*[]*T, error)

GetNumberPointerArrayPtr retrieves a property as a pointer to a slice of number pointers.

func GetNumberPropOrDefault

func GetNumberPropOrDefault[T NumberConstraint](props map[string]interface{}, prop string, defaultValue T) T

GetNumberPropOrDefault is an alias for GetNumberOrDefault.

func GetNumberPropOrDefaultFunction

func GetNumberPropOrDefaultFunction[T NumberConstraint](props map[string]interface{}, prop string, defaultFunction func() T) T

GetNumberPropOrDefaultFunction retrieves a numeric property or returns a value from a default function.

func GetNumberPropOrThrow

func GetNumberPropOrThrow[T NumberConstraint](props map[string]interface{}, prop string, message ...string) T

GetNumberPropOrThrow retrieves a numeric property or panics if missing/invalid.

func GetNumberPtr

func GetNumberPtr[T NumberConstraint](props map[string]interface{}, prop string) (*T, error)

GetNumberPtr retrieves a numeric property as a pointer.

func GetNumberPtrOrDefault

func GetNumberPtrOrDefault[T NumberConstraint](props map[string]interface{}, prop string, defaultValue *T) *T

GetNumberPtrOrDefault retrieves a numeric property as a pointer or returns a default value.

func GetObject

func GetObject[T any](props map[string]interface{}, prop string) (T, error)

GetObject retrieves an object property (as T).

func GetObjectArray

func GetObjectArray[T any](props map[string]interface{}, prop string) ([]T, error)

GetObjectArray retrieves an object array property.

func GetObjectArrayFunctionPropOrDefault

func GetObjectArrayFunctionPropOrDefault[T any](props map[string]interface{}, prop string, constructorFunc func(map[string]interface{}) T, defaultValue []T) []T

GetObjectArrayFunctionPropOrDefault

func GetObjectArrayOrDefault

func GetObjectArrayOrDefault[T any](props map[string]interface{}, prop string, defaultValue []T) []T

GetObjectArrayOrDefault retrieves an object array property or returns a default value.

func GetObjectArrayPropOrDefault

func GetObjectArrayPropOrDefault[T any](props map[string]interface{}, prop string, defaultValue []T) []T

GetObjectArrayPropOrDefault

func GetObjectArrayPtr

func GetObjectArrayPtr[T any](props map[string]interface{}, prop string) (*[]T, error)

GetObjectArrayPtr retrieves an object array property as a pointer.

func GetObjectArrayPtrOrDefault

func GetObjectArrayPtrOrDefault[T any](props map[string]interface{}, prop string, defaultValue *[]T) *[]T

GetObjectArrayPtrOrDefault retrieves an object array property as a pointer or returns a default value.

func GetObjectFunctionPropOrDefault

func GetObjectFunctionPropOrDefault[T any](props map[string]interface{}, prop string, constructorFunc func(map[string]interface{}) T, defaultValue T) T

GetObjectFunctionPropOrDefault uses a constructor function.

func GetObjectFunctionPropOrThrow

func GetObjectFunctionPropOrThrow[T any](props map[string]interface{}, prop string, constructorFunc func(map[string]interface{}) T, message ...string) T

GetObjectFunctionPropOrThrow

func GetObjectOrDefault

func GetObjectOrDefault[T any](props map[string]interface{}, prop string, defaultValue T) T

GetObjectOrDefault retrieves an object property or returns a default value.

func GetObjectPointerArray

func GetObjectPointerArray[T any](props map[string]interface{}, prop string) ([]*T, error)

GetObjectPointerArray retrieves a property as a slice of object pointers.

func GetObjectPointerArrayPtr

func GetObjectPointerArrayPtr[T any](props map[string]interface{}, prop string) (*[]*T, error)

GetObjectPointerArrayPtr retrieves a property as a pointer to a slice of object pointers.

func GetObjectPropOrDefault

func GetObjectPropOrDefault[T any](props map[string]interface{}, prop string, defaultValue T) T

GetObjectPropOrDefault

func GetObjectPropOrDefaultAllowNull

func GetObjectPropOrDefaultAllowNull[T any](props map[string]interface{}, prop string, defaultValue T) *T

GetObjectPropOrDefaultAllowNull

func GetObjectPropOrThrow

func GetObjectPropOrThrow[T any](props map[string]interface{}, prop string, message ...string) T

GetObjectPropOrThrow

func GetObjectPtr

func GetObjectPtr[T any](props map[string]interface{}, prop string) (*T, error)

GetObjectPtr retrieves an object property as a pointer.

func GetObjectPtrOrDefault

func GetObjectPtrOrDefault[T any](props map[string]interface{}, prop string, defaultValue *T) *T

GetObjectPtrOrDefault retrieves an object property as a pointer or returns a default value.

func GetString

func GetString(props map[string]interface{}, prop string) (string, error)

GetString retrieves a string property. It returns an error if the property is missing or not a string.

func GetStringArray

func GetStringArray(props map[string]interface{}, prop string) ([]string, error)

GetStringArray retrieves a string array property.

func GetStringArrayOrDefault

func GetStringArrayOrDefault(props map[string]interface{}, prop string, defaultValue []string) []string

GetStringArrayOrDefault retrieves a string array property or returns a default value.

func GetStringArrayPropOrDefault

func GetStringArrayPropOrDefault(props map[string]interface{}, prop string, defaultValue []string) []string

GetStringArrayPropOrDefault

func GetStringArrayPropOrThrow

func GetStringArrayPropOrThrow(props map[string]interface{}, prop string, message ...string) []string

GetStringArrayPropOrThrow

func GetStringArrayPtr

func GetStringArrayPtr(props map[string]interface{}, prop string) (*[]string, error)

GetStringArrayPtr retrieves a string array property as a pointer.

func GetStringArrayPtrOrDefault

func GetStringArrayPtrOrDefault(props map[string]interface{}, prop string, defaultValue *[]string) *[]string

GetStringArrayPtrOrDefault retrieves a string array property as a pointer or returns a default value.

func GetStringOrDefault

func GetStringOrDefault(props map[string]interface{}, prop string, defaultValue string) string

GetStringOrDefault retrieves a string property or returns a default value.

func GetStringPointerArray

func GetStringPointerArray(props map[string]interface{}, prop string) ([]*string, error)

GetStringPointerArray retrieves a property as a slice of string pointers.

func GetStringPointerArrayPtr

func GetStringPointerArrayPtr(props map[string]interface{}, prop string) (*[]*string, error)

GetStringPointerArrayPtr retrieves a property as a pointer to a slice of string pointers.

func GetStringPropOrDefault

func GetStringPropOrDefault(props map[string]interface{}, prop string, defaultValue string) string

GetStringPropOrDefault is an alias for GetStringOrDefault

func GetStringPropOrDefaultFunction

func GetStringPropOrDefaultFunction(props map[string]interface{}, prop string, defaultFunction func() string) string

GetStringPropOrDefaultFunction retrieves a string property or returns a value from a default function.

func GetStringPropOrThrow

func GetStringPropOrThrow(props map[string]interface{}, prop string, message ...string) string

GetStringPropOrThrow behaves like MustGetString but allows a custom message.

func GetStringPtr

func GetStringPtr(props map[string]interface{}, prop string) (*string, error)

GetStringPtr retrieves a string property as a pointer.

func GetStringPtrOrDefault

func GetStringPtrOrDefault(props map[string]interface{}, prop string, defaultValue *string) *string

GetStringPtrOrDefault retrieves a string property as a pointer or returns a default value.

func GetStringRegex added in v0.1.3

func GetStringRegex(props map[string]interface{}, prop string, expression string) (string, error)

GetStringRegex retrieves a string property and validates it against a regular expression. It returns an error if the property is missing, not a string, the regex is invalid, or the value doesn't match.

func GetStringRegexOrDefault added in v0.1.3

func GetStringRegexOrDefault(props map[string]interface{}, prop string, expression string, defaultValue string) string

GetStringRegexOrDefault retrieves a string property validated against a regex or returns a default value.

func GetStringRegexPtr added in v0.1.3

func GetStringRegexPtr(props map[string]interface{}, prop string, expression string) (*string, error)

GetStringRegexPtr retrieves a string property as a pointer and validates it against a regular expression.

func GetStringRegexPtrOrDefault added in v0.1.3

func GetStringRegexPtrOrDefault(props map[string]interface{}, prop string, expression string, defaultValue *string) *string

GetStringRegexPtrOrDefault retrieves a string property as a pointer validated against a regex or returns a default value.

func MustGetBigInt

func MustGetBigInt(props map[string]interface{}, prop string) *big.Int

MustGetBigInt retrieves a big.Int property or panics.

func MustGetBoolean

func MustGetBoolean(props map[string]interface{}, prop string) bool

MustGetBoolean retrieves a boolean property or panics.

func MustGetBooleanArray

func MustGetBooleanArray(props map[string]interface{}, prop string) []bool

MustGetBooleanArray retrieves a boolean array property or panics.

func MustGetBooleanArrayPtr

func MustGetBooleanArrayPtr(props map[string]interface{}, prop string) *[]bool

MustGetBooleanArrayPtr retrieves a boolean array property as a pointer or panics.

func MustGetBooleanPtr

func MustGetBooleanPtr(props map[string]interface{}, prop string) *bool

MustGetBooleanPtr retrieves a boolean property as a pointer or panics.

func MustGetDate

func MustGetDate(props map[string]interface{}, prop string) time.Time

MustGetDate retrieves a date property or panics.

func MustGetDateArray

func MustGetDateArray(props map[string]interface{}, prop string) []time.Time

MustGetDateArray retrieves a date array property or panics.

func MustGetDateArrayPtr

func MustGetDateArrayPtr(props map[string]interface{}, prop string) *[]time.Time

MustGetDateArrayPtr retrieves a date array property as a pointer or panics.

func MustGetDatePtr

func MustGetDatePtr(props map[string]interface{}, prop string) *time.Time

MustGetDatePtr retrieves a date property as a pointer or panics.

func MustGetMap

func MustGetMap[K comparable, V any](props map[string]interface{}, prop string) map[K]V

MustGetMap retrieves a map property or panics.

func MustGetNumber

func MustGetNumber[T NumberConstraint](props map[string]interface{}, prop string) T

MustGetNumber retrieves a numeric property or panics.

func MustGetNumberArray

func MustGetNumberArray[T NumberConstraint](props map[string]interface{}, prop string) []T

MustGetNumberArray retrieves a number array property or panics.

func MustGetNumberArrayPtr

func MustGetNumberArrayPtr[T NumberConstraint](props map[string]interface{}, prop string) *[]T

MustGetNumberArrayPtr retrieves a number array property as a pointer or panics.

func MustGetNumberPtr

func MustGetNumberPtr[T NumberConstraint](props map[string]interface{}, prop string) *T

MustGetNumberPtr retrieves a numeric property as a pointer or panics.

func MustGetObject

func MustGetObject[T any](props map[string]interface{}, prop string) T

MustGetObject retrieves an object property or panics.

func MustGetObjectArray

func MustGetObjectArray[T any](props map[string]interface{}, prop string) []T

MustGetObjectArray retrieves an object array property or panics.

func MustGetObjectArrayPtr

func MustGetObjectArrayPtr[T any](props map[string]interface{}, prop string) *[]T

MustGetObjectArrayPtr retrieves an object array property as a pointer or panics.

func MustGetObjectPtr

func MustGetObjectPtr[T any](props map[string]interface{}, prop string) *T

MustGetObjectPtr retrieves an object property as a pointer or panics.

func MustGetString

func MustGetString(props map[string]interface{}, prop string) string

MustGetString retrieves a string property or panics.

func MustGetStringArray

func MustGetStringArray(props map[string]interface{}, prop string) []string

MustGetStringArray retrieves a string array property or panics.

func MustGetStringArrayPtr

func MustGetStringArrayPtr(props map[string]interface{}, prop string) *[]string

MustGetStringArrayPtr retrieves a string array property as a pointer or panics.

func MustGetStringPtr

func MustGetStringPtr(props map[string]interface{}, prop string) *string

MustGetStringPtr retrieves a string property as a pointer or panics.

func MustGetStringRegex added in v0.1.3

func MustGetStringRegex(props map[string]interface{}, prop string, expression string) string

MustGetStringRegex retrieves a string property validated against a regex or panics.

func MustGetStringRegexPtr added in v0.1.3

func MustGetStringRegexPtr(props map[string]interface{}, prop string, expression string) *string

MustGetStringRegexPtr retrieves a string property as a pointer validated against a regex or panics.

Types

type InvalidTypeError

type InvalidTypeError struct {
	Prop     string
	Expected string
	Actual   interface{}
	Cause    error
}

InvalidTypeError indicates that a field exists but is not of the expected type.

func (*InvalidTypeError) Error

func (e *InvalidTypeError) Error() string

func (*InvalidTypeError) Unwrap

func (e *InvalidTypeError) Unwrap() error

type MissingFieldError

type MissingFieldError struct {
	Prop string
}

MissingFieldError indicates that a required field is missing from the map.

func (*MissingFieldError) Error

func (e *MissingFieldError) Error() string

type NumberConstraint

type NumberConstraint interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~float32 | ~float64
}

NumberConstraint defines numeric types that we can return.

type RegexMismatchError added in v0.1.3

type RegexMismatchError struct {
	Prop       string
	Value      string
	Expression string
}

RegexMismatchError indicates that a string field does not match the expected regular expression.

func (*RegexMismatchError) Error added in v0.1.3

func (e *RegexMismatchError) Error() string

Jump to

Keyboard shortcuts

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