typed

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2023 License: MIT Imports: 5 Imported by: 7

Documentation

Overview

A wrapper to make map[string]any a little more type-safe

Index

Constants

This section is empty.

Variables

View Source
var (
	// Used by ToBytes to indicate that the key was not
	// present in the type
	KeyNotFound = errors.New("Key not found")
	Empty       = Typed(nil)
)

Functions

This section is empty.

Types

type Typed

type Typed map[string]any

A Typed type helper for accessing a map

func Json

func Json(data []byte) (Typed, error)

Create a Typed helper from the given JSON bytes

func JsonFile

func JsonFile(path string) (Typed, error)

Create a Typed helper from the JSON within a file

func JsonString

func JsonString(data string) (Typed, error)

Create a Typed helper from the given JSON string

func Must

func Must(data []byte) Typed

Create a Typed helper from the given JSON bytes, panics on error

func New

func New(m map[string]any) Typed

Wrap the map into a Typed

func (Typed) Any added in v0.0.5

func (t Typed) Any(key string) any

func (Typed) AnyIf added in v0.0.5

func (t Typed) AnyIf(key string) (any, bool)

Returns an string at the key and whether or not the key existed and the value was an string

func (Typed) AnyMust added in v0.0.5

func (t Typed) AnyMust(key string) any

Returns an interface or panics

func (Typed) AnyOr added in v0.0.5

func (t Typed) AnyOr(key string, d any) any

Returns a string at the key, or the specified value if it doesn't exist or isn't a strin

func (Typed) Anys added in v0.0.5

func (t Typed) Anys(key string) []any

func (Typed) AnysIf added in v0.0.5

func (t Typed) AnysIf(key string) ([]any, bool)

Returns an string at the key and whether or not the key existed and the value was an string

func (Typed) AnysMust added in v0.0.5

func (t Typed) AnysMust(key string) []any

Returns an interface or panics

func (Typed) AnysOr added in v0.0.5

func (t Typed) AnysOr(key string, d []any) []any

Returns a string at the key, or the specified value if it doesn't exist or isn't a strin

func (Typed) Bool

func (t Typed) Bool(key string) bool

Returns a boolean at the key, or false if it doesn't exist, or if it isn't a bool

func (Typed) BoolIf

func (t Typed) BoolIf(key string) (bool, bool)

Returns a boolean at the key and whether or not the key existed and the value was a bolean

func (Typed) BoolMust

func (t Typed) BoolMust(key string) bool

Returns a bool or panics

func (Typed) BoolOr

func (t Typed) BoolOr(key string, d bool) bool

Returns a boolean at the key, or the specified value if it doesn't exist or isn't a bool

func (Typed) Bools

func (t Typed) Bools(key string) []bool

Returns an slice of boolean, or an nil slice

func (Typed) BoolsIf

func (t Typed) BoolsIf(key string) ([]bool, bool)

Returns a boolean slice + true if valid Returns nil + false otherwise (returns nil+false if one of the values is not a valid boolean)

func (Typed) BoolsOr

func (t Typed) BoolsOr(key string, d []bool) []bool

Returns an slice of boolean, or the specified slice

func (Typed) Bytes

func (t Typed) Bytes(key string) []byte

func (Typed) BytesIf

func (t Typed) BytesIf(key string) ([]byte, bool)

Returns an []byte at the key and whether or not the key existed and the value was an string

func (Typed) BytesMust

func (t Typed) BytesMust(key string) []byte

Returns an []byte or panics

func (Typed) BytesOr

func (t Typed) BytesOr(key string, d []byte) []byte

Returns a []byte at the key, or the specified value if it doesn't exist or isn't a []byte

func (Typed) Exists

func (t Typed) Exists(key string) bool

func (Typed) Float

func (t Typed) Float(key string) float64

func (Typed) FloatIf

func (t Typed) FloatIf(key string) (float64, bool)

Returns an float at the key and whether or not the key existed and the value was an float

func (Typed) FloatMust

func (t Typed) FloatMust(key string) float64

Returns an float or panics

func (Typed) FloatOr

func (t Typed) FloatOr(key string, d float64) float64

Returns a float at the key, or the specified value if it doesn't exist or isn't a float

func (Typed) Floats

func (t Typed) Floats(key string) []float64

Returns an slice of floats, or a nil slice

func (Typed) FloatsIf

func (t Typed) FloatsIf(key string) ([]float64, bool)

Returns a float slice + true if valid Returns nil + false otherwise (returns nil+false if one of the values is not a valid float)

func (Typed) FloatsOr

func (t Typed) FloatsOr(key string, d []float64) []float64

Returns an slice of floats, or the specified slice if the key doesn't exist or isn't a valid []float64

func (Typed) Int

func (t Typed) Int(key string) int

func (Typed) IntIf

func (t Typed) IntIf(key string) (int, bool)

Returns an int at the key and whether or not the key existed and the value was an int

func (Typed) IntMust

func (t Typed) IntMust(key string) int

Returns an int or panics

func (Typed) IntOr

func (t Typed) IntOr(key string, d int) int

Returns a int at the key, or the specified value if it doesn't exist or isn't a int

func (Typed) Ints

func (t Typed) Ints(key string) []int

Returns an slice of ints, or the specified slice Some conversion is done to handle the fact that JSON ints are represented as floats.

func (Typed) Ints64

func (t Typed) Ints64(key string) []int64

Returns an slice of ints64, or the specified slice Some conversion is done to handle the fact that JSON ints are represented as floats.

func (Typed) Ints64If

func (t Typed) Ints64If(key string) ([]int64, bool)

Returns a boolean slice + true if valid Returns nil + false otherwise (returns nil+false if one of the values is not a valid boolean)

func (Typed) Ints64Or

func (t Typed) Ints64Or(key string, d []int64) []int64

Returns an slice of ints, or the specified slice if the key doesn't exist or isn't a valid []int. Some conversion is done to handle the fact that JSON ints are represented as floats.

func (Typed) IntsIf

func (t Typed) IntsIf(key string) ([]int, bool)

Returns a int slice + true if valid Returns nil + false otherwise (returns nil+false if one of the values is not a valid int)

func (Typed) IntsOr

func (t Typed) IntsOr(key string, d []int) []int

Returns an slice of ints, or the specified slice if the key doesn't exist or isn't a valid []int. Some conversion is done to handle the fact that JSON ints are represented as floats.

func (Typed) Json

func (t Typed) Json(key string) (Typed, error)

func (Typed) JsonMust

func (t Typed) JsonMust(key string) Typed

func (Typed) Keys

func (t Typed) Keys() []string

func (Typed) Map

func (t Typed) Map(key string) map[string]any

Returns a map[string]any at the key or a nil map if the key doesn't exist or if the key isn't a map[string]interface

func (Typed) MapIf

func (t Typed) MapIf(key string) (map[string]any, bool)

Returns a map[string]interface at the key and whether or not the key existed and the value was an map[string]any

func (Typed) MapOr

func (t Typed) MapOr(key string, d map[string]any) map[string]any

Returns a map[string]any at the key or the specified default if the key doesn't exist or if the key isn't a map[string]interface

func (Typed) Maps

func (t Typed) Maps(key string) []map[string]any

Returns an slice of map[string]interfaces, or a nil slice

func (Typed) Object

func (t Typed) Object(key string) Typed

Returns a Typed helper at the key If the key doesn't exist, a default Typed helper is returned (which will return default values for any subsequent sub queries)

func (Typed) ObjectIf

func (t Typed) ObjectIf(key string) (Typed, bool)

Returns a Typed helper at the key and whether or not the key existed and the value was an map[string]any

func (Typed) ObjectMust

func (t Typed) ObjectMust(key string) Typed

Returns an typed object or panics

func (Typed) ObjectOr

func (t Typed) ObjectOr(key string, d map[string]any) Typed

Returns a Typed helper at the key or the specified default if the key doesn't exist or if the key isn't a map[string]any

func (Typed) Objects

func (t Typed) Objects(key string) []Typed

Returns an slice of Typed helpers, or a nil slice

func (Typed) ObjectsIf

func (t Typed) ObjectsIf(key string) ([]Typed, bool)

Returns a slice of Typed helpers and true if exists, otherwise; nil and false.

func (Typed) ObjectsMust

func (t Typed) ObjectsMust(key string) []Typed

func (Typed) String

func (t Typed) String(key string) string

func (Typed) StringBool

func (t Typed) StringBool(key string) map[string]bool

Returns an map[string]bool

func (Typed) StringFloat

func (t Typed) StringFloat(key string) map[string]float64

Returns an map[string]float64

func (Typed) StringIf

func (t Typed) StringIf(key string) (string, bool)

Returns an string at the key and whether or not the key existed and the value was an string

func (Typed) StringInt

func (t Typed) StringInt(key string) map[string]int

Returns an map[string]int Some work is done to handle the fact that JSON ints are represented as floats.

func (Typed) StringMust

func (t Typed) StringMust(key string) string

Returns an string or panics

func (Typed) StringObject

func (t Typed) StringObject(key string) map[string]Typed

Returns an map[string]Typed

func (Typed) StringOr

func (t Typed) StringOr(key string, d string) string

Returns a string at the key, or the specified value if it doesn't exist or isn't a string

func (Typed) StringString

func (t Typed) StringString(key string) map[string]string

Returns an map[string]string

func (Typed) Strings

func (t Typed) Strings(key string) []string

Returns an slice of strings, or a nil slice

func (Typed) StringsIf

func (t Typed) StringsIf(key string) ([]string, bool)

Returns a string slice + true if valid Returns nil + false otherwise (returns nil+false if one of the values is not a valid string)

func (Typed) StringsOr

func (t Typed) StringsOr(key string, d []string) []string

Returns an slice of strings, or the specified slice if the key doesn't exist or isn't a valid []string

func (Typed) Time

func (t Typed) Time(key string) time.Time

func (Typed) TimeIf

func (t Typed) TimeIf(key string) (time.Time, bool)

Returns an time.time at the key and whether or not the key existed and the value was a time.Time

func (Typed) TimeMust

func (t Typed) TimeMust(key string) time.Time

Returns a time.Time or panics

func (Typed) TimeOr

func (t Typed) TimeOr(key string, d time.Time) time.Time

Returns a time at the key, or the specified value if it doesn't exist or isn't a time

Jump to

Keyboard shortcuts

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