Skip to content

Commit 82e6fab

Browse files
committed
docs: update README and examples
1 parent 154f620 commit 82e6fab

2 files changed

Lines changed: 175 additions & 6 deletions

File tree

README.md

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,103 @@
11
# orderedmap
22
![Coverage](https://img.shields.io/badge/Coverage-65.2%25-yellow)
33

4-
An alternative generic ordered map in Go with de/serializing from/to JSON.
4+
A generic ordered map implementation in Go that preserves insertion order and supports JSON serialization/deserialization.
55

6-
## How to use
6+
## Installation
77

8-
See the [example code](./example_test.go).
8+
```bash
9+
go get github.com/kazamori/orderedmap
10+
```
11+
12+
## Features
13+
14+
- Generic type support with `OrderedMap[K, V]`
15+
- Preserves insertion order of keys
16+
- JSON marshaling/unmarshaling with order preservation
17+
- O(1) key lookup
18+
19+
## Quick Start
20+
21+
```go
22+
package main
23+
24+
import (
25+
"encoding/json"
26+
"fmt"
27+
28+
"github.com/kazamori/orderedmap"
29+
)
30+
31+
func main() {
32+
// Create a new ordered map
33+
m := orderedmap.New[string, any]()
34+
m.Set("name", "Alice")
35+
m.Set("age", 30)
36+
m.Set("active", true)
37+
38+
// Iterate in insertion order
39+
for _, p := range m.Pairs() {
40+
fmt.Printf("%s: %v\n", p.Key, p.Value)
41+
}
42+
43+
// Get a value
44+
if val, ok := m.Get("name"); ok {
45+
fmt.Println("Name:", val)
46+
}
47+
48+
// Serialize to JSON (preserves order)
49+
data, _ := json.Marshal(m)
50+
fmt.Println(string(data))
51+
// Output: {"name":"Alice","age":30,"active":true}
52+
}
53+
```
954

10-
## test CLI
55+
## JSON Unmarshaling
1156

12-
For testing, deserialize JSON and then serialize it while preserving the order.
57+
Parse JSON while preserving the original key order:
1358

59+
```go
60+
jsonData := []byte(`{"z":"last","a":"first","m":"middle"}`)
61+
m := orderedmap.New[string, any]()
62+
json.Unmarshal(jsonData, m)
63+
64+
for _, p := range m.Pairs() {
65+
fmt.Printf("%s: %v\n", p.Key, p.Value)
66+
}
67+
// Output:
68+
// z: last
69+
// a: first
70+
// m: middle
1471
```
15-
$ ./bin/cli -data '{"s":"test","i":3,"a":[{"f":3.14},{"b":true}]}'
72+
73+
## API Reference
74+
75+
| Function/Method | Description |
76+
|----------------|-------------|
77+
| `New[K, V]()` | Create a new empty ordered map |
78+
| `WithCapacity[K, V](size)` | Create with pre-allocated capacity |
79+
| `NewFromMap[M, K, V](m)` | Create from a standard Go map |
80+
| `Set(key, value)` | Add or update a key-value pair |
81+
| `Get(key)` | Retrieve a value by key |
82+
| `Pairs()` | Get all pairs in insertion order |
83+
| `String()` | JSON string representation |
84+
| `ToMap[M, K, V](om)` | Convert to standard Go map |
85+
86+
## Examples
87+
88+
See [example_test.go](./example_test.go) for more usage examples.
89+
90+
## CLI Tool
91+
92+
A CLI tool is included for testing JSON round-trip serialization:
93+
94+
```bash
95+
make build
96+
./bin/cli -data '{"s":"test","i":3,"a":[{"f":3.14},{"b":true}]}'
97+
```
98+
99+
Output:
100+
```json
16101
{
17102
"s": "test",
18103
"i": 3,

example_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package orderedmap_test
22

33
import (
4+
"encoding/json"
45
"fmt"
56

67
"github.com/kazamori/orderedmap"
@@ -23,3 +24,86 @@ func Example() {
2324
// ================================
2425
// {"key1":"value1","key2":3,"key3":[1.41421356,3.14]}
2526
}
27+
28+
func Example_get() {
29+
m := orderedmap.New[string, int]()
30+
m.Set("one", 1)
31+
m.Set("two", 2)
32+
m.Set("three", 3)
33+
34+
if val, ok := m.Get("two"); ok {
35+
fmt.Println("Found:", val)
36+
}
37+
if _, ok := m.Get("missing"); !ok {
38+
fmt.Println("Key not found")
39+
}
40+
// Output:
41+
// Found: 2
42+
// Key not found
43+
}
44+
45+
func Example_jsonUnmarshal() {
46+
jsonData := []byte(`{"z":"last","a":"first","m":"middle"}`)
47+
m := orderedmap.New[string, any]()
48+
if err := json.Unmarshal(jsonData, m); err != nil {
49+
panic(err)
50+
}
51+
52+
// Keys are preserved in original JSON order
53+
for _, p := range m.Pairs() {
54+
fmt.Printf("%s: %v\n", p.Key, p.Value)
55+
}
56+
// Output:
57+
// z: last
58+
// a: first
59+
// m: middle
60+
}
61+
62+
func Example_jsonMarshal() {
63+
m := orderedmap.New[string, any]()
64+
m.Set("name", "Alice")
65+
m.Set("age", 30)
66+
m.Set("active", true)
67+
68+
data, err := json.Marshal(m)
69+
if err != nil {
70+
panic(err)
71+
}
72+
fmt.Println(string(data))
73+
// Output:
74+
// {"name":"Alice","age":30,"active":true}
75+
}
76+
77+
func Example_nestedJSON() {
78+
jsonData := []byte(`{"user":{"name":"Bob","score":100},"items":["a","b","c"]}`)
79+
m := orderedmap.New[string, any]()
80+
if err := json.Unmarshal(jsonData, m); err != nil {
81+
panic(err)
82+
}
83+
84+
fmt.Println(m.String())
85+
// Output:
86+
// {"user":{"name":"Bob","score":100},"items":["a","b","c"]}
87+
}
88+
89+
func Example_withCapacity() {
90+
// Pre-allocate capacity for better performance
91+
m := orderedmap.WithCapacity[string, int](100)
92+
m.Set("first", 1)
93+
m.Set("second", 2)
94+
fmt.Println(m.String())
95+
// Output:
96+
// {"first":1,"second":2}
97+
}
98+
99+
func Example_toMap() {
100+
om := orderedmap.New[string, int]()
101+
om.Set("a", 1)
102+
om.Set("b", 2)
103+
104+
// Convert to standard Go map
105+
m := orderedmap.ToMap[map[string]int](om)
106+
fmt.Println(m["a"], m["b"])
107+
// Output:
108+
// 1 2
109+
}

0 commit comments

Comments
 (0)