orderedmap

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

orderedmap

Build status go.dev reference Go Report Card codecov

Overview

A Golang Map that keeps track of the insert order of items.

The current version can be used as a replacement for map[string]interface{} and allows JSON unmarshalling into it. No manual adding of items is currently possible.

Usage

Unmarshall and marshall JSON:

var m Map
input := `{"423":"abc","231":"dbh","152":"xyz"}`

m.UnmarshalJSON([]byte(input))

output, _ := m.MarshalJSON()
# unlike the standard Golang map, the output will be exact the same and not in random key order

Documentation

Overview

Package orderedmap provides a Map that preserves the order of key value pairs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Value interface{}
	// contains filtered or unexported fields
}

Entry represents a map value entry.

func (*Entry) MarshalJSON

func (s *Entry) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Entry) UnmarshalJSON

func (s *Entry) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Map

type Map struct {
	// Data holds the map entries in unsorted order.
	Data map[string]Entry
	// Keys contains the map keys in sorted order.
	Keys []string
}

Map implements a map that keeps the item order.

func (*Map) Len

func (m *Map) Len() int

Len returns the number of elements within the map.

func (*Map) MarshalJSON

func (m *Map) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Map) Range

func (m *Map) Range(f func(key string, value interface{}) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

func (*Map) UnmarshalJSON

func (m *Map) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

Jump to

Keyboard shortcuts

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