-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtuple2.go
More file actions
240 lines (202 loc) · 9.94 KB
/
Copy pathtuple2.go
File metadata and controls
240 lines (202 loc) · 9.94 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package tuple
import (
"encoding/json"
"fmt"
"golang.org/x/exp/constraints"
)
// T2 is a tuple type holding 2 generic values.
type T2[Ty1, Ty2 any] struct {
V1 Ty1
V2 Ty2
}
// Len returns the number of values held by the tuple.
func (t T2[Ty1, Ty2]) Len() int {
return 2
}
// Values returns the values held by the tuple.
func (t T2[Ty1, Ty2]) Values() (Ty1, Ty2) {
return t.V1, t.V2
}
// Array returns an array of the tuple values.
func (t T2[Ty1, Ty2]) Array() [2]any {
return [2]any{
t.V1,
t.V2,
}
}
// Slice returns a slice of the tuple values.
func (t T2[Ty1, Ty2]) Slice() []any {
a := t.Array()
return a[:]
}
// String returns the string representation of the tuple.
func (t T2[Ty1, Ty2]) String() string {
return tupString(t.Slice())
}
// GoString returns a Go-syntax representation of the tuple.
func (t T2[Ty1, Ty2]) GoString() string {
return tupGoString(t.Slice())
}
// New2 creates a new tuple holding 2 generic values.
func New2[Ty1, Ty2 any](v1 Ty1, v2 Ty2) T2[Ty1, Ty2] {
return T2[Ty1, Ty2]{
V1: v1,
V2: v2,
}
}
// FromArray2 returns a tuple from an array of length 2.
// If any of the values can not be converted to the generic type, an error is returned.
func FromArray2[Ty1, Ty2 any](arr [2]any) (T2[Ty1, Ty2], error) {
v1, ok := arr[0].(Ty1)
if !ok {
return T2[Ty1, Ty2]{}, fmt.Errorf("value at array index 0 expected to have type %s but has type %T", typeName[Ty1](), arr[0])
}
v2, ok := arr[1].(Ty2)
if !ok {
return T2[Ty1, Ty2]{}, fmt.Errorf("value at array index 1 expected to have type %s but has type %T", typeName[Ty2](), arr[1])
}
return New2(v1, v2), nil
}
// FromArray2X returns a tuple from an array of length 2.
// If any of the values can not be converted to the generic type, the function panics.
func FromArray2X[Ty1, Ty2 any](arr [2]any) T2[Ty1, Ty2] {
return FromSlice2X[Ty1, Ty2](arr[:])
}
// FromSlice2 returns a tuple from a slice of length 2.
// If the length of the slice doesn't match, or any of the values can not be converted to the generic type, an error is returned.
func FromSlice2[Ty1, Ty2 any](values []any) (T2[Ty1, Ty2], error) {
if len(values) != 2 {
return T2[Ty1, Ty2]{}, fmt.Errorf("slice length %d must match number of tuple values 2", len(values))
}
v1, ok := values[0].(Ty1)
if !ok {
return T2[Ty1, Ty2]{}, fmt.Errorf("value at slice index 0 expected to have type %s but has type %T", typeName[Ty1](), values[0])
}
v2, ok := values[1].(Ty2)
if !ok {
return T2[Ty1, Ty2]{}, fmt.Errorf("value at slice index 1 expected to have type %s but has type %T", typeName[Ty2](), values[1])
}
return New2(v1, v2), nil
}
// FromSlice2X returns a tuple from a slice of length 2.
// If the length of the slice doesn't match, or any of the values can not be converted to the generic type, the function panics.
func FromSlice2X[Ty1, Ty2 any](values []any) T2[Ty1, Ty2] {
if len(values) != 2 {
panic(fmt.Errorf("slice length %d must match number of tuple values 2", len(values)))
}
v1 := values[0].(Ty1)
v2 := values[1].(Ty2)
return New2(v1, v2)
}
// Equal2 returns whether the host tuple is equal to the other tuple.
// All tuple elements of the host and guest parameters must match the "comparable" built-in constraint.
// To test equality of tuples that hold custom Equalable values, use the Equal2E function.
// To test equality of tuples that hold custom Comparable values, use the Equal2C function.
// Otherwise, use Equal or reflect.DeepEqual to test tuples of any types.
func Equal2[Ty1, Ty2 comparable](host, guest T2[Ty1, Ty2]) bool {
return host.V1 == guest.V1 && host.V2 == guest.V2
}
// Equal2E returns whether the host tuple is semantically equal to the guest tuple.
// All tuple elements of the host and guest parameters must match the Equalable constraint.
// To test equality of tuples that hold built-in "comparable" values, use the Equal2 function.
// To test equality of tuples that hold custom Comparable values, use the Equal2C function.
// Otherwise, use Equal or reflect.DeepEqual to test tuples of any types.
func Equal2E[Ty1 Equalable[Ty1], Ty2 Equalable[Ty2]](host, guest T2[Ty1, Ty2]) bool {
return host.V1.Equal(guest.V1) && host.V2.Equal(guest.V2)
}
// Equal2C returns whether the host tuple is semantically less than, equal to, or greater than the guest tuple.
// All tuple elements of the host and guest parameters must match the Comparable constraint.
// To test equality of tuples that hold built-in "comparable" values, use the Equal2 function.
// To test equality of tuples that hold custom Equalable values, use the Equal2E function.
// Otherwise, use Equal or reflect.DeepEqual to test tuples of any types.
func Equal2C[Ty1 Comparable[Ty1], Ty2 Comparable[Ty2]](host, guest T2[Ty1, Ty2]) bool {
return host.V1.CompareTo(guest.V1).EQ() && host.V2.CompareTo(guest.V2).EQ()
}
// Compare2 returns whether the host tuple is semantically less than, equal to, or greater than the guest tuple.
// All tuple elements of the host and guest parameters must match the "Ordered" constraint.
// To compare tuples that hold custom comparable values, use the Compare2C function.
func Compare2[Ty1, Ty2 constraints.Ordered](host, guest T2[Ty1, Ty2]) OrderedComparisonResult {
return multiCompare(
func() OrderedComparisonResult { return compareOrdered(host.V1, guest.V1) },
func() OrderedComparisonResult { return compareOrdered(host.V2, guest.V2) },
)
}
// Compare2C returns whether the host tuple is semantically less than, equal to, or greater than the guest tuple.
// All tuple elements of the host and guest parameters must match the Comparable constraint.
// To compare tuples that hold built-in "Ordered" values, use the Compare2 function.
func Compare2C[Ty1 Comparable[Ty1], Ty2 Comparable[Ty2]](host, guest T2[Ty1, Ty2]) OrderedComparisonResult {
return multiCompare(
func() OrderedComparisonResult { return host.V1.CompareTo(guest.V1) },
func() OrderedComparisonResult { return host.V2.CompareTo(guest.V2) },
)
}
// LessThan2 returns whether the host tuple is semantically less than the guest tuple.
// All tuple elements of the host and guest parameters must match the "Ordered" constraint.
// To compare tuples that hold custom comparable values, use the LessThan2C function.
func LessThan2[Ty1, Ty2 constraints.Ordered](host, guest T2[Ty1, Ty2]) bool {
return Compare2(host, guest).LT()
}
// LessThan2C returns whether the host tuple is semantically less than the guest tuple.
// All tuple elements of the host and guest parameters must match the Comparable constraint.
// To compare tuples that hold built-in "Ordered" values, use the LessThan2 function.
func LessThan2C[Ty1 Comparable[Ty1], Ty2 Comparable[Ty2]](host, guest T2[Ty1, Ty2]) bool {
return Compare2C(host, guest).LT()
}
// LessOrEqual2 returns whether the host tuple is semantically less than or equal to the guest tuple.
// All tuple elements of the host and guest parameters must match the "Ordered" constraint.
// To compare tuples that hold custom comparable values, use the LessOrEqual2C function.
func LessOrEqual2[Ty1, Ty2 constraints.Ordered](host, guest T2[Ty1, Ty2]) bool {
return Compare2(host, guest).LE()
}
// LessOrEqual2C returns whether the host tuple is semantically less than or equal to the guest tuple.
// All tuple elements of the host and guest parameters must match the Comparable constraint.
// To compare tuples that hold built-in "Ordered" values, use the LessOrEqual2 function.
func LessOrEqual2C[Ty1 Comparable[Ty1], Ty2 Comparable[Ty2]](host, guest T2[Ty1, Ty2]) bool {
return Compare2C(host, guest).LE()
}
// GreaterThan2 returns whether the host tuple is semantically greater than the guest tuple.
// All tuple elements of the host and guest parameters must match the "Ordered" constraint.
// To compare tuples that hold custom comparable values, use the GreaterThan2C function.
func GreaterThan2[Ty1, Ty2 constraints.Ordered](host, guest T2[Ty1, Ty2]) bool {
return Compare2(host, guest).GT()
}
// GreaterThan2C returns whether the host tuple is semantically greater than the guest tuple.
// All tuple elements of the host and guest parameters must match the Comparable constraint.
// To compare tuples that hold built-in "Ordered" values, use the GreaterThan2 function.
func GreaterThan2C[Ty1 Comparable[Ty1], Ty2 Comparable[Ty2]](host, guest T2[Ty1, Ty2]) bool {
return Compare2C(host, guest).GT()
}
// GreaterOrEqual2 returns whether the host tuple is semantically greater than or equal to the guest tuple.
// All tuple elements of the host and guest parameters must match the "Ordered" constraint.
// To compare tuples that hold custom comparable values, use the GreaterOrEqual2C function.
func GreaterOrEqual2[Ty1, Ty2 constraints.Ordered](host, guest T2[Ty1, Ty2]) bool {
return Compare2(host, guest).GE()
}
// GreaterOrEqual2C returns whether the host tuple is semantically greater than or equal to the guest tuple.
// All tuple elements of the host and guest parameters must match the Comparable constraint.
// To compare tuples that hold built-in "Ordered" values, use the GreaterOrEqual2 function.
func GreaterOrEqual2C[Ty1 Comparable[Ty1], Ty2 Comparable[Ty2]](host, guest T2[Ty1, Ty2]) bool {
return Compare2C(host, guest).GE()
}
// MarshalJSON marshals the tuple into a JSON array.
func (t T2[Ty1, Ty2]) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Slice())
}
// MarshalJSON unmarshals the tuple from a JSON array.
func (t *T2[Ty1, Ty2]) UnmarshalJSON(data []byte) error {
// Working with json.RawMessage instead of any enables custom struct support.
var slice []json.RawMessage
if err := json.Unmarshal(data, &slice); err != nil {
return fmt.Errorf("unable to unmarshal json array for tuple: %w", err)
}
if len(slice) != 2 {
return fmt.Errorf("unmarshalled json array length %d must match number of tuple values 2", len(slice))
}
if err := json.Unmarshal(slice[0], &t.V1); err != nil {
return fmt.Errorf("value %q at slice index 0 failed to unmarshal: %w", string(slice[0]), err)
}
if err := json.Unmarshal(slice[1], &t.V2); err != nil {
return fmt.Errorf("value %q at slice index 1 failed to unmarshal: %w", string(slice[1]), err)
}
return nil
}