ordereddata

package module
v0.0.0-...-020e46b Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: MIT Imports: 11 Imported by: 0

README

Ordered-Data

This simple library implements an ordered map that has:

  • JSON marshal/unmarshal to preserve JSON ordering
  • MustGet* convenience functions that do some basic type conversions, particularly useful when working with JSON data
  • Map value extraction
  • Replace
  • Multi-key removal
  • Clone
  • Stringify

Interfaces

OrderedMap[K comparable, V any] is a general-purpose ordered map. Create an ordered map with NewOrderedMap() or NewOrderedMapN(). Then use its Set() function to assign a value, and Get() to retrieve a value. The rest of the functions in the interface are fairly self-explainitory.

StringMap is a convenience alias for OrderedMap[string, any]. Create a string map with NewStringMap() or NewStringMapN()

Unmarshaling

To unmarshal an interface in Go, you must first initialize what the interface points to, and then json.Unmarshal will fill it.

Example:

	m := NewOrderedMap[string, any]()
	if err := json.Unmarshal(orderedJson1Json, &m); err != nil {
		panic(err)
	}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvaldJsonValue = errors.New("invalid json value")

Functions

This section is empty.

Types

type OrderedMap

type OrderedMap[K comparable, V any] interface {
	Set(k K, v V) (isNew bool)
	Replace(k K, v V)
	Get(k K) (v V, exists bool)
	GetAt(index int) (v V, exists bool)
	MustGet(k K) (v V)
	MustGetString(k K) string
	MustGetInt(k K) int
	MustGetIntOrDefault(k K, defaultInt int) int
	MustGetInt64(k K) int64
	MustGetFloat64(k K) float64
	MustGetFloat64Ptr(k K) (f *float64)
	MustGetBool(k K) bool
	MustGetAt(index int) (v V)
	Map() map[K]V
	Keys() []K
	Len() int
	Values() []V
	Clone() OrderedMap[K, V]
	Copy(other OrderedMap[K, V], keys ...K)
	Remove(k ...K)
	String() string

	encoding.TextUnmarshaler
	json.Marshaler
	json.Unmarshaler
}

func NewOrderedMap

func NewOrderedMap[K comparable, V any]() OrderedMap[K, V]

func NewOrderedMapN

func NewOrderedMapN[K comparable, V any](length int) OrderedMap[K, V]

type StringMap

type StringMap interface {
	Set(k string, v any) (isNew bool)
	Replace(k string, v any)
	Get(k string) (v any, exists bool)
	GetAt(index int) (v any, exists bool)
	MustGet(k string) (v any)
	MustGetString(k string) string
	MustGetInt(k string) int
	MustGetIntOrDefault(k string, defaultInt int) int
	MustGetInt64(k string) int64
	MustGetFloat64(k string) float64
	MustGetFloat64Ptr(k string) (f *float64)
	MustGetBool(k string) bool
	MustGetAt(index int) (v any)
	Map() map[string]any
	Keys() []string
	Len() int
	Values() []any
	Clone() OrderedMap[string, any]
	Copy(other OrderedMap[string, any], keys ...string)
	Remove(k ...string)
	String() string

	encoding.TextUnmarshaler
	json.Marshaler
	json.Unmarshaler
}

func NewStringMap

func NewStringMap() StringMap

func NewStringMapN

func NewStringMapN(length int) StringMap

Jump to

Keyboard shortcuts

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