data

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2023 License: Apache-2.0 Imports: 8 Imported by: 7

README

data

Some utilities for handling data in Go.

  • object.go - a map that preserves key order during JSON round trip
  • decimal.go - a wrapper for math/big.Float that marshals to/from JSON
  • timestamp.go - a wrapper for time.Time that marshals to/from JSON as RFC 3339 text.
  • misc.go - some miscellaneous helpers

Documentation

Index

Constants

View Source
const RFC3339Milli = "%d-%02d-%02dT%02d:%02d:%02d.%03dZ"

Variables

View Source
var NOT_FOUND_ERROR = fmt.Errorf("Not found")

Functions

func AsBool

func AsBool(v interface{}) bool

func AsFloat64

func AsFloat64(v interface{}) float64

func AsInt

func AsInt(v interface{}) int

func AsInt64

func AsInt64(v interface{}) int64

func AsMap

func AsMap(v interface{}) map[string]interface{}

func AsSlice added in v0.0.2

func AsSlice(v interface{}) []interface{}

func AsString

func AsString(v interface{}) string

func AsStringSlice added in v0.0.2

func AsStringSlice(v interface{}) []string

func Get

func Get(m map[string]interface{}, key string) interface{}

func GetBool

func GetBool(m map[string]interface{}, key string) bool

func GetInt

func GetInt(m map[string]interface{}, key string) int

func GetInt64

func GetInt64(m map[string]interface{}, key string) int64

func GetMap

func GetMap(m map[string]interface{}, key string) map[string]interface{}

func GetSlice added in v0.0.2

func GetSlice(m map[string]interface{}, key string) []interface{}

func GetString

func GetString(m map[string]interface{}, key string) string

func GetStringSlice added in v0.0.2

func GetStringSlice(m map[string]interface{}, key string) []string

func JsonDecodeAs added in v0.0.2

func JsonDecodeAs[T any](j string, target *T) error

func JsonEncode added in v0.0.2

func JsonEncode(obj interface{}) string

func JsonKeysInOrder

func JsonKeysInOrder(data []byte) ([]string, error)

func Pretty

func Pretty(obj interface{}) string

func ToString

func ToString(obj interface{}) string

func ValueTo added in v0.0.2

func ValueTo[T any](v interface{}, storage *T) error

methods don't do generics

Types

type Decimal

type Decimal struct {
	Value decimal.Decimal
}

func AsDecimal

func AsDecimal(v interface{}) *Decimal

func DecimalFromFloat64 added in v0.0.2

func DecimalFromFloat64(val float64) *Decimal

func DecimalFromInt64 added in v0.0.2

func DecimalFromInt64(val int64) *Decimal

func DecimalFromString added in v0.0.2

func DecimalFromString(text string) (*Decimal, error)

func DecimalValue

func DecimalValue(val *Decimal, defval interface{}) *Decimal

func GetDecimal

func GetDecimal(m map[string]interface{}, key string) *Decimal

func (*Decimal) AsBigFloat

func (d *Decimal) AsBigFloat() *big.Float

func (*Decimal) AsFloat64

func (d *Decimal) AsFloat64() float64

func (*Decimal) AsInt

func (d *Decimal) AsInt() int

func (*Decimal) AsInt32

func (d *Decimal) AsInt32() int32

func (*Decimal) AsInt64

func (d *Decimal) AsInt64() int64

func (Decimal) MarshalJSON

func (d Decimal) MarshalJSON() ([]byte, error)

Encode as a JSON number. The JSON spec allows for arbitrary precision, so this is the correct thing to do.

func (*Decimal) String

func (d *Decimal) String() string

func (*Decimal) UnmarshalJSON

func (d *Decimal) UnmarshalJSON(b []byte) error

type Map added in v0.0.2

type Map[V any] struct {
	// contains filtered or unexported fields
}

func NewMap added in v0.0.2

func NewMap[V any]() *Map[V]

func (*Map[V]) Get added in v0.0.2

func (s *Map[V]) Get(key string) V

func (*Map[V]) Has added in v0.0.2

func (s *Map[V]) Has(key string) bool

func (*Map[V]) Keys added in v0.0.2

func (s *Map[V]) Keys() []string

func (*Map[V]) Length added in v0.0.2

func (s *Map[V]) Length() int

func (Map[V]) MarshalJSON added in v0.0.2

func (s Map[V]) MarshalJSON() ([]byte, error)

func (*Map[V]) Put added in v0.0.2

func (s *Map[V]) Put(key string, val V)

func (*Map[V]) String added in v0.0.2

func (s *Map[V]) String() string

func (*Map[V]) UnmarshalJSON added in v0.0.2

func (s *Map[V]) UnmarshalJSON(data []byte) error

type Object

type Object struct {
	// contains filtered or unexported fields
}

data.Object - a map that preserves the order of the keys (which are always converted

to strings). Values can be anything.

func AsObject

func AsObject(v interface{}) *Object

func JsonDecode added in v0.0.2

func JsonDecode(j string) *Object

func NewObject

func NewObject() *Object

func ObjectFromMap

func ObjectFromMap(m map[string]interface{}) *Object

func ValueOf added in v0.0.2

func ValueOf(v interface{}) *Object

func (*Object) Get

func (s *Object) Get(key string) interface{}

func (*Object) GetArray

func (s *Object) GetArray(key string) []interface{}

func (*Object) GetBool

func (s *Object) GetBool(key string) bool

func (*Object) GetDecimal

func (s *Object) GetDecimal(key string) *Decimal

func (*Object) GetInt

func (s *Object) GetInt(key string) int

func (*Object) GetInt64

func (s *Object) GetInt64(key string) int64

func (*Object) GetMap

func (s *Object) GetMap(key string) map[string]interface{}

func (*Object) GetObject

func (s *Object) GetObject(key string) *Object

func (*Object) GetString

func (s *Object) GetString(key string) string

func (*Object) GetStringSlice added in v0.0.2

func (s *Object) GetStringSlice(key string) []string

func (*Object) Has

func (s *Object) Has(key string) bool

func (*Object) Keys

func (s *Object) Keys() []string

func (*Object) Length

func (s *Object) Length() int

func (Object) MarshalJSON

func (s Object) MarshalJSON() ([]byte, error)

func (*Object) Put

func (s *Object) Put(key string, val interface{})

func (*Object) String

func (s *Object) String() string

func (*Object) UnmarshalJSON

func (s *Object) UnmarshalJSON(data []byte) error

type Timestamp added in v0.0.2

type Timestamp struct {
	time.Time
}

func ParseTimestamp added in v0.0.2

func ParseTimestamp(s string) (Timestamp, error)

func (Timestamp) MarshalJSON added in v0.0.2

func (ts Timestamp) MarshalJSON() ([]byte, error)

func (Timestamp) String added in v0.0.2

func (ts Timestamp) String() string

func (*Timestamp) ToRfc2616String added in v0.0.2

func (ts *Timestamp) ToRfc2616String() string

func (*Timestamp) UnmarshalJSON added in v0.0.2

func (ts *Timestamp) UnmarshalJSON(b []byte) error

Jump to

Keyboard shortcuts

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