omap

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: ISC Imports: 7 Imported by: 0

README

Go Reference

Golang Ordered Map

Golang Ordered Map is a map data structure that maintains the order of the keys. It also supports JSON and YAML marshalling.

Installation

go get github.com/nicolasparada/go-omap

Usage

package main

import (
	omap "github.com/nicolasparada/go-omap"
)

func main() {
	data := []byte(`{ "name": "John", "age": 30, "active": true }`)

	var unordered map[string]any{}
	if err := json.Unmarshal(data, &unordered); err != nil {
		panic(err)
	}

	var ordered omap.Map[string, any]
	if err := json.Unmarshal(data, &ordered); err != nil {
		panic(err)
	}

	json.NewEncoder(os.Stdout).Encode(unordered) // will print in undefined order
	json.NewEncoder(os.Stdout).Encode(ordered) // will always print: {"name":"John","age":30,"active":true}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

type Map[K comparable, V any] []Pair[K, V]

func New

func New[K comparable, V any]() Map[K, V]

func (*Map[K, V]) Delete

func (om *Map[K, V]) Delete(key K)

func (Map[K, V]) Get

func (om Map[K, V]) Get(key K) (V, bool)

func (Map[K, V]) Has

func (om Map[K, V]) Has(key K) bool

func (Map[K, V]) MarshalJSON

func (om Map[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface to marshall a sorted list of key-value pairs into an object.

func (Map[K, V]) MarshalYAML

func (om Map[K, V]) MarshalYAML() (any, error)

MarshalYAML implements yaml.Marshaler interface to marshall a sorted list of properties into an object.

func (*Map[K, V]) Set

func (om *Map[K, V]) Set(key K, val V)

func (*Map[K, V]) UnmarshalJSON

func (om *Map[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler interface to unmarshal an object into a sorted list of key-value pairs.

func (*Map[K, V]) UnmarshalYAML

func (om *Map[K, V]) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler interface to unmarshal an object into a sorted list of properties.

type Pair

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

func (Pair[K, V]) Equal

func (p Pair[K, V]) Equal(other Pair[K, V]) bool

Jump to

Keyboard shortcuts

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