dict

package module
v0.0.0-...-0960bed Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2024 License: MIT Imports: 8 Imported by: 0

README

go-dict

Lazy go map value deambiguator.

Installation

go get -u github.com/dcarbone/go-dict

Usage

package main

import (
	"fmt"

	"github.com/dcarbone/go-dict"
)

const wellDocumentedJson = `{
    "definitelyString": false,
    "docsSayInt": "200",
    "billIsSureItsABool": "true"
}`

func main() {
	d, err := dict.UnmarshalJSON([]byte(wellDocumentedJson))
	if err != nil {
		panic(err.Error())
	}

	fmt.Printf("%[1]v (%[1]T)\n", d.MustGetString("definitelyString"))
	fmt.Printf("%[1]v (%[1]T)\n", d.MustGetInt("docsSayInt"))
	fmt.Printf("%[1]v (%[1]T)\n", d.MustGetBool("billIsSureItsABool"))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyNotFound           = errors.New("key not found")
	ErrValueTypeMismatch     = errors.New("value type mismatch")
	ErrCannotCoerceValueType = errors.New("cannot coerce value type")
)

Functions

func CoerceExact

func CoerceExact[T any](v any) (T, error)

func CoerceExactPtr

func CoerceExactPtr[T any](v any) (*T, error)

func Get

func Get[T any](d Dict, key string, cfn CoerceValueFunc[T]) (T, error)

func GetExact

func GetExact[T any](d Dict, key string) (T, error)

func GetExactPtr

func GetExactPtr[T any](d Dict, key string) (*T, error)

func GetNonZeroComparableOr

func GetNonZeroComparableOr[T comparable](d Dict, key string, cfn CoerceValueFunc[T], or T) T

GetNonZeroComparableOr is significantly different from GetOr in that it considers a comparable's zero-val to be "empty", thus returning the value provided to "or".

This is an important distinction.

func GetOr

func GetOr[T any](d Dict, key string, cfn CoerceValueFunc[T], or T) T

func GetPtr

func GetPtr[T any](d Dict, key string, cfn CoercePtrFunc[T]) (*T, error)

func GetSlice

func GetSlice[T any](cfn CoerceValueFunc[T]) func(any) ([]T, error)

func GetSlicePtr

func GetSlicePtr[T any](cfn CoercePtrFunc[T]) func(any) ([]*T, error)

func MustGet

func MustGet[T any](d Dict, key string, cfn CoerceValueFunc[T]) T

func MustGetExact

func MustGetExact[T any](d Dict, key string) T

func MustGetExactPtr

func MustGetExactPtr[T any](d Dict, key string) *T

func MustGetPtr

func MustGetPtr[T any](d Dict, key string, cfn CoercePtrFunc[T]) *T

Types

type CoercePtrFunc

type CoercePtrFunc[T any] func(any) (*T, error)

type CoerceValueFunc

type CoerceValueFunc[T any] func(any) (T, error)

type Dict

type Dict map[string]any

func UnmarshalJSON

func UnmarshalJSON(p []byte) (Dict, error)

func UnmarshalJSONReader

func UnmarshalJSONReader(r io.Reader) (Dict, error)

func (Dict) GetBool

func (d Dict) GetBool(key string) (bool, error)

func (Dict) GetBoolOr

func (d Dict) GetBoolOr(key string, def bool) bool

func (Dict) GetDict

func (d Dict) GetDict(key string) (Dict, error)

func (Dict) GetDictOr

func (d Dict) GetDictOr(key string, def Dict) Dict

func (Dict) GetDuration

func (d Dict) GetDuration(key string) (time.Duration, error)

func (Dict) GetDurationOr

func (d Dict) GetDurationOr(key string, def time.Duration) time.Duration

func (Dict) GetInt

func (d Dict) GetInt(key string) (int, error)

func (Dict) GetIntOr

func (d Dict) GetIntOr(key string, def int) int

func (Dict) GetInts

func (d Dict) GetInts(key string) ([]int, error)

func (Dict) GetIntsOr

func (d Dict) GetIntsOr(key string, def []int) []int

func (Dict) GetString

func (d Dict) GetString(key string) (string, error)

func (Dict) GetStringOr

func (d Dict) GetStringOr(key string, def string) string

func (Dict) GetStrings

func (d Dict) GetStrings(key string) ([]string, error)

func (Dict) GetStringsOr

func (d Dict) GetStringsOr(key string, def []string) []string

func (Dict) MustGetBool

func (d Dict) MustGetBool(key string) bool

func (Dict) MustGetDict

func (d Dict) MustGetDict(key string) Dict

func (Dict) MustGetDuration

func (d Dict) MustGetDuration(key string) time.Duration

func (Dict) MustGetInt

func (d Dict) MustGetInt(key string) int

func (Dict) MustGetInts

func (d Dict) MustGetInts(key string) []int

func (Dict) MustGetString

func (d Dict) MustGetString(key string) string

func (Dict) MustGetStrings

func (d Dict) MustGetStrings(key string) []string

func (Dict) ShallowMerge

func (d Dict) ShallowMerge(other Dict) Dict

ShallowMerge clones the local Dict, overwriting any / all top-level keys with values from "other". This does _not_ recursively merge nested structures.

Jump to

Keyboard shortcuts

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