orderedmap

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: BSD-2-Clause-Views Imports: 10 Imported by: 3 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Generic orderedmap with (Un)MarshalJSON

Go Report Card MIT License Go Reference

go get

go get github.com/shu-go/orderedmap

Codes

New

var m *orderedmap.OrderedMap[int, int]
m = orderedmap.New[int, int]()

// or simply,
m := orderedmap.New[int, int]()

Set

m.Set(1, 100)
m.Set(9, 900)
m.Set(2, 200)

m.Keys() //=> []int{1, 9, 2}
Re-Order
m.PreserveOrder(false)
m.Set(1, 100)

m.Keys() //=> []int{9, 2, 1}

Get

v, found := m.Get(1)

GetDefault

v := m.Get(8, 1234)

Keys

keys := m.Keys() // [1, 9, 2]

Delete

m.Delete(2)
m.Delete(10) // no error

m.Keys() //=> [1, 9]

Contains

m.Contains(8) //=> false
m.Contains(1) //=> true

JSON

MarshalJSON
data, err := json.Marshal(m) //=> `{"1":100,"9":900}`
UnmarshalJSON
data := []byte(`{"100":1000,"200":2000}`)
err := json.Unmarshal(data, &m) // CLEARED and Unmarshalled

// m.UnmarshalJSON(data) is faster.

Sort

m := orderedmap.New[string, any]()
m.UnmarshalJSON([]byte(`{"a":1,"z":999,"b":2}`))

m.Keys() //=> ["a", "z", "b"]

m.Sort(func(i, j string) bool {
    // i,j are keys
    return i < j
})

m.Keys() //=> ["a", "b", "z"]

Format

m := orderedmap.New[string, any]()
m.UnmarshalJSON([]byte(`{"a":1,"z":999,"b":2}`))

fmt.Sprint(m) //=> OrderedMap[a:1 z:999 b:2]

fmt.Sprintf("%#v", m) //=> OrderedMap[string]interface {}{"a":1, "z":999, "b":2}

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/shu-go/orderedmap"
)

func main() {
	type myStruct struct {
		V1 string
		V2 int
	}
	m := orderedmap.New[string, myStruct]()

	m.Set("ichi", myStruct{
		V1: "one",
		V2: 1,
	})
	m.Set("ni", myStruct{
		V1: "two",
		V2: 2,
	})
	m.Set("san", myStruct{
		V1: "three",
		V2: 3,
	})

	v, found := m.Get("ichi")
	fmt.Println(found)
	fmt.Println(v)

	fmt.Println()

	fmt.Println(m.Contains("ni"))
	fmt.Println(m.Contains("go"))
}
Output:
true
{one 1}

true
false
Example (Marshal)
package main

import (
	"encoding/json"
	"fmt"

	"github.com/shu-go/orderedmap"
)

func main() {
	m := orderedmap.New[int, string]()
	m.Set(5, "go")
	m.Set(9, "ku")
	m.Set(6, "ro-")
	m.Set(3, "san")

	b, _ /*err*/ := json.MarshalIndent(m, "", "  ")

	fmt.Println(string(b))
}
Output:
{
  "5": "go",
  "9": "ku",
  "6": "ro-",
  "3": "san"
}
Example (MarshalYAML)
package main

import (
	"fmt"

	"gopkg.in/yaml.v3"

	"github.com/shu-go/orderedmap"
)

func main() {
	m := orderedmap.New[int, string]()
	m.Set(5, "go")
	m.Set(9, "ku")
	m.Set(6, "ro-")
	m.Set(3, "san")

	b, _ /*err*/ := yaml.Marshal(m)

	fmt.Println(string(b))
}
Output:
"5": go
"9": ku
"6": ro-
"3": san
Example (Sort)
package main

import (
	"encoding/json"
	"fmt"

	"github.com/shu-go/orderedmap"
)

func main() {
	m := orderedmap.New[string, int]()
	m.Set("5", 1)
	m.Set("9", 2)
	m.Set("6", 3)
	m.Set("3", 4)

	m.Sort(func(i, j string) bool {
		return i < j
	})

	b, _ /*err*/ := json.MarshalIndent(m, "", "  ")

	fmt.Println(string(b))
}
Output:
{
  "3": 4,
  "5": 1,
  "6": 3,
  "9": 2
}
Example (Unmarshal)
package main

import (
	"encoding/json"
	"fmt"

	"github.com/shu-go/orderedmap"
)

func main() {
	m := orderedmap.New[int, string]()
	m.Set(999, "dummy")

	s := `{
	  "5": "go",
	  "9": "ku",
	  "6": "ro-",
	  "3": "san"
	}`

	_ /*err*/ = json.Unmarshal([]byte(s), m)

	fmt.Println(m.Get(5))
	fmt.Println(m.Get(9))
	fmt.Println(m.Get(6))
	fmt.Println(m.Get(3))
	fmt.Println(m.Get(999))
	fmt.Println(m.Get(9999))
}
Output:
go true
ku true
ro- true
san true
 false
 false
Example (UnmarshalYAML)
package main

import (
	"fmt"

	"gopkg.in/yaml.v3"

	"github.com/shu-go/orderedmap"
)

func main() {
	// DOES NOT SUPPORT number keys
	m := orderedmap.New[string, string]()
	m.Set("999", "dummy")

	s := `
"5": go
"9": ku
"6": ro-
"3": san
`

	err := yaml.Unmarshal([]byte(s), m)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(m.Get("5"))
	fmt.Println(m.Get("9"))
	fmt.Println(m.Get("6"))
	fmt.Println(m.Get("3"))
	fmt.Println(m.Get("999"))
	fmt.Println(m.Get("9999"))
}
Output:
go true
ku true
ro- true
san true
 false
 false
Example (UnmarshalYAML2)
package main

import (
	"fmt"

	"gopkg.in/yaml.v3"

	"github.com/shu-go/orderedmap"
)

func main() {
	type aaa struct {
		A string
		B bool
		C []int
	}
	m := orderedmap.New[string, aaa]()
	m.Set("rei", aaa{A: "0", B: false, C: []int{}})
	m.Set("ichi", aaa{A: "1", B: true, C: []int{1}})
	m.Set("ni", aaa{A: "2", B: false, C: []int{1, 2}})
	m.Set("san", aaa{A: "3", B: true, C: []int{1, 2, 3}})
	m.Set("shi", aaa{A: "4", B: false, C: []int{1, 2, 3, 4}})

	data, err := yaml.Marshal(m)
	if err != nil {
		fmt.Println("marshal", err)
	}
	fmt.Println(string(data))

	err = yaml.Unmarshal(data, m)
	if err != nil {
		fmt.Println("unmarshal", err)
	}
}
Output:
rei:
    a: "0"
    b: false
    c: []
ichi:
    a: "1"
    b: true
    c:
        - 1
ni:
    a: "2"
    b: false
    c:
        - 1
        - 2
san:
    a: "3"
    b: true
    c:
        - 1
        - 2
        - 3
shi:
    a: "4"
    b: false
    c:
        - 1
        - 2
        - 3
        - 4

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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 (*OrderedMap[K, V]) Contains

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

func (*OrderedMap[K, V]) Delete

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

func (OrderedMap[K, V]) Format

func (m OrderedMap[K, V]) Format(s fmt.State, verb rune)

func (*OrderedMap[K, V]) Get

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

func (*OrderedMap[K, V]) GetDefault

func (m *OrderedMap[K, V]) GetDefault(key K, defvalue V) V

func (*OrderedMap[K, V]) Keys

func (m *OrderedMap[K, V]) Keys() []K

func (*OrderedMap[K, V]) Len

func (m *OrderedMap[K, V]) Len() int

func (*OrderedMap[K, V]) MarshalJSON

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

func (*OrderedMap[K, V]) MarshalYAML

func (m *OrderedMap[K, V]) MarshalYAML() (any, error)

func (*OrderedMap[K, V]) PreserveOrder

func (m *OrderedMap[K, V]) PreserveOrder(b bool)

func (*OrderedMap[K, V]) Set

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

func (*OrderedMap[K, V]) Sort

func (m *OrderedMap[K, V]) Sort(less func(K, K) bool)

func (*OrderedMap[K, V]) UnmarshalJSON

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

func (*OrderedMap[K, V]) UnmarshalYAML

func (m *OrderedMap[K, V]) UnmarshalYAML(value *yaml.Node) error

NOT SUPPORTED: number key, nested OrderedMap

func (*OrderedMap[K, V]) UnorderedMap

func (m *OrderedMap[K, V]) UnorderedMap() map[K]V

type SliceHandler

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

func (SliceHandler) Len

func (h SliceHandler) Len() int

func (SliceHandler) Less

func (h SliceHandler) Less(i, j int) bool

func (SliceHandler) Swap

func (h SliceHandler) Swap(i, j int)

Jump to

Keyboard shortcuts

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