-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue.go
29 lines (23 loc) · 857 Bytes
/
value.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
package ordmap
import (
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
)
// Value is a value with an index.
type Value[V any] struct {
V V
idx int
}
// UnmarshalJSONFrom unmarshals a value by just decoding the value.
// The index is set by the caller.
func (cs *Value[_]) UnmarshalJSONFrom(dec *jsontext.Decoder, opts json.Options) error {
return json.UnmarshalDecode(dec, &cs.V, opts)
}
// MarshalJSONTo marshals a value by encoding just the value and ignoring the index.
func (v Value[_]) MarshalJSONTo(enc *jsontext.Encoder, opts json.Options) error {
return json.MarshalEncode(enc, v.V, opts)
}
// getIndex returns the index of a value.
func getIndex[V any](v Value[V]) int { return v.idx }
// setIndex sets the index of a value.
func setIndex[V any](v Value[V], i int) Value[V] { v.idx = i; return v }