Skip to content

shu-go/orderedmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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}

About

Generic orderedmap with (Un)MarshalJSON

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages