forked from go-edn/edn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_arbitrary_keys_test.go
51 lines (38 loc) · 1.01 KB
/
example_arbitrary_keys_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package edn_test
import (
"fmt"
"gopkg.in/edn.v1"
)
type Point3 struct {
X, Y, Z int64
}
type Unit struct {
Type edn.Keyword
HP int
}
// EDN, in contrast to JSON, supports arbitrary values as keys.
func Example_arbitraryKeys() {
input := `{{:x 1 :y 2 :z 3} "Greybeard"
{:y 10 :x 1 :z -10} "Blackwind"}`
var locStarships map[Point3]string
err := edn.UnmarshalString(input, &locStarships)
if err != nil {
panic(err)
}
p := Point3{1, 10, -10}
fmt.Printf("Starship at location %v is %s\n", p, locStarships[p])
input = `{[0 2] {:type :scout :hp 55}
[-3 10] {:type :villager :hp 25}
[5 5] {:type :bowman :hp 32}
[5 6] {:type :bowman :hp 29}}`
var locUnits map[[2]int]Unit
err = edn.UnmarshalString(input, &locUnits)
if err != nil {
panic(err)
}
loc := [2]int{5, 5}
fmt.Printf("Unit at location %v is %+v\n", loc, locUnits[loc])
// Output:
// Starship at location {1 10 -10} is Blackwind
// Unit at location [5 5] is {Type::bowman HP:32}
}