-
Notifications
You must be signed in to change notification settings - Fork 6
/
primitive.go
59 lines (48 loc) · 1.6 KB
/
primitive.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
52
53
54
55
56
57
58
59
package batchkeyset
import (
"fmt"
"github.com/PapaCharlie/go-restli/v2/restlicodec"
)
type primitiveKeySet[T restlicodec.ComparablePrimitive] struct {
originalKeys map[T]struct{}
}
func (s *primitiveKeySet[T]) AddKey(t T) error {
if _, ok := s.originalKeys[t]; ok {
return fmt.Errorf("go-restli: Cannot add key %+v twice to BatchKeySet", t)
}
s.originalKeys[t] = struct{}{}
return nil
}
func (s *primitiveKeySet[T]) LocateOriginalKey(key T) (originalKey T, found bool) {
_, found = s.originalKeys[key]
return key, found
}
func (s *primitiveKeySet[T]) LocateOriginalKeyFromReader(keyReader restlicodec.Reader) (originalKey T, err error) {
originalKey, err = restlicodec.UnmarshalRestLi[T](keyReader)
if err != nil {
return originalKey, err
}
_, ok := s.LocateOriginalKey(originalKey)
if !ok {
err = fmt.Errorf("go-restli: Unknown key returned by batch method: %q", keyReader)
}
return originalKey, err
}
func (s *primitiveKeySet[T]) Encode(paramNameWriter func(string) restlicodec.Writer) error {
return encode[T](s, paramNameWriter)
}
func (s *primitiveKeySet[T]) EncodeQueryParams() (params string, err error) {
return generateRawQuery[T](s)
}
func (s *primitiveKeySet[T]) encodeKeys() ([]string, error) {
encodedKeys := make([]string, 0, len(s.originalKeys))
for k := range s.originalKeys {
w := restlicodec.NewRestLiQueryParamsWriter()
_ = restlicodec.MarshalRestLi(k, w)
encodedKeys = append(encodedKeys, w.Finalize())
}
return encodedKeys, nil
}
func NewPrimitiveKeySet[T restlicodec.ComparablePrimitive]() BatchKeySet[T] {
return &primitiveKeySet[T]{originalKeys: map[T]struct{}{}}
}