4
4
[ ![ Coverage Status] ( https://coveralls.io/repos/github/barweiss/go-tuple/badge.svg )] ( https://coveralls.io/github/barweiss/go-tuple )
5
5
[ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/barweiss/go-tuple )] ( https://goreportcard.com/report/github.com/barweiss/go-tuple )
6
6
[ ![ Go Reference] ( https://pkg.go.dev/badge/github.com/barweiss/go-tuple.svg )] ( https://pkg.go.dev/github.com/barweiss/go-tuple )
7
- [ ![ Mentioned in Awesome Go] ( https://awesome.re/mentioned-badge.svg )] ( https://github.com/avelino/awesome-go )
7
+ [ ![ Mentioned in Awesome Go] ( https://awesome.re/mentioned-badge.svg )] ( https://github.com/avelino/awesome-go )
8
8
9
9
Go 1.18+ tuple implementation.
10
10
@@ -79,6 +79,35 @@ tup := tuple.New2(5, "hi!")
79
79
a , b := tup.Values ()
80
80
```
81
81
82
+ ## JSON Marshalling
83
+
84
+ Tuples are marshalled and unmarshalled as JSON arrays.
85
+
86
+ ``` go
87
+ type User struct {
88
+ Name string ` json:"name"`
89
+ Age int ` json:"age,omitempty"`
90
+ }
91
+
92
+ type MyJSON struct {
93
+ Users []tuple.T2 [string , User ] ` json:"users"`
94
+ }
95
+
96
+ func main () {
97
+ data := MyJSON{
98
+ Users: []tuple.T2 [string , User]{
99
+ tuple.New2 (" foo" , User{Name: " foo" , Age: 42 }),
100
+ tuple.New2 (" bar" , User{Name: " bar" , Age: 21 }),
101
+ tuple.New2 (" baz" , User{Name: " baz" }),
102
+ },
103
+ }
104
+
105
+ marshalled , _ := json.Marshal (data)
106
+ fmt.Printf (" %s \n " , string (marshalled))
107
+ // Outputs: {"users":[["foo",{"name":"foo","age":42}],["bar",{"name":"bar","age":21}],["baz",{"name":"baz"}]]}
108
+ }
109
+ ```
110
+
82
111
## Comparison
83
112
84
113
Tuples are compared from the first element to the last.
@@ -101,6 +130,7 @@ fmt.Println(tups) // [["bar", -4, 43], ["foo", 2, -23], ["foo", 72, 15]].
101
130
```
102
131
103
132
---
133
+
104
134
** NOTE**
105
135
106
136
In order to compare tuples, all tuple elements must match ` constraints.Ordered ` .
@@ -165,7 +195,7 @@ func main() {
165
195
}
166
196
```
167
197
168
- In order to call the complex types variation of the comparable functions, __ all __ tuple types must match the ` Comparable ` constraint.
198
+ In order to call the complex types variation of the comparable functions, ** all ** tuple types must match the ` Comparable ` constraint.
169
199
170
200
While this is not ideal, this a known inconvenience given the current type parameters capabilities in Go.
171
201
Some solutions have been porposed for this issue ([ lesser] ( https://github.com/lelysses/lesser ) , for example, beatifully articulates the issue),
0 commit comments