orderedmap

package module
v0.0.0-...-264e8e3 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

orderedmap

Coverage

An alternative generic ordered map in Go with de/serializing from/to JSON.

How to use

See the example code.

test CLI

For testing, deserialize JSON and then serialize it while preserving the order.

$ ./bin/cli -data '{"s":"test","i":3,"a":[{"f":3.14},{"b":true}]}'
{
  "s": "test",
  "i": 3,
  "a": [
    {
      "f": 3.14
    },
    {
      "b": true
    }
  ]
}

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/kazamori/orderedmap"
)

func main() {
	m := orderedmap.New[string, any]()
	m.Set("key1", "value1")
	m.Set("key2", 3)
	m.Set("key3", []float64{1.41421356, 3.14})
	for _, v := range m.Pairs() {
		fmt.Println(v.String())
	}
	fmt.Println("================================")
	fmt.Println(m.String())
}
Output:

(key1, value1)
(key2, 3)
(key3, [1.41421356 3.14])
================================
{"key1":"value1","key2":3,"key3":[1.41421356,3.14]}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrEndOfJSON    = errors.New("end of JSON")
	ErrNestedObject = errors.New("detect nested object")
	ErrNestedArray  = errors.New("detect nested array")
)

Functions

func ToMap

func ToMap[M ~map[K]V, K comparable, V any](om *OrderedMap[K, V]) M

Types

type OrderedMap

type OrderedMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func New

func New[K comparable, V any]() *OrderedMap[K, V]

func NewFromMap

func NewFromMap[M ~map[K]V, K comparable, V any](m M) *OrderedMap[K, V]

func WithCapacity

func WithCapacity[K comparable, V any](size int) *OrderedMap[K, V]

func (*OrderedMap[K, V]) Get

func (m *OrderedMap[K, V]) Get(key K) (V, bool)

func (OrderedMap[K, V]) MarshalJSON

func (m OrderedMap[K, V]) MarshalJSON() ([]byte, error)

func (*OrderedMap[K, V]) Pairs

func (m *OrderedMap[K, V]) Pairs() []Pair[K, V]

func (*OrderedMap[K, V]) Set

func (m *OrderedMap[K, V]) Set(key K, value V)

func (*OrderedMap[K, V]) String

func (m *OrderedMap[K, V]) String() string

func (*OrderedMap[K, V]) UnmarshalJSON

func (m *OrderedMap[K, V]) UnmarshalJSON(b []byte) error

type Pair

type Pair[K comparable, V any] struct {
	Key   K
	Value V
}

func NewPair

func NewPair[K comparable, V any](k K, v V) *Pair[K, V]

func (*Pair[K, V]) QuotedKey

func (p *Pair[K, V]) QuotedKey() string

func (*Pair[K, V]) String

func (p *Pair[K, V]) String() string

Directories

Path Synopsis
cmd
cli

Jump to

Keyboard shortcuts

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