-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
64 lines (54 loc) · 1.4 KB
/
utils.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
60
61
62
63
64
package redis
import (
"encoding"
"fmt"
"net"
"time"
"github.com/redis/go-redis/v9"
"go.autokitteh.dev/autokitteh/sdk/sdktypes"
"go.autokitteh.dev/autokitteh/sdk/sdkvalues"
)
func unwrap(v sdktypes.Value) (any, error) {
u, err := sdkvalues.DefaultValueWrapper.Unwrap(v)
if err != nil {
return nil, err
}
// All supported redis types, adapted from https://github.com/redis/go-redis/blob/master/internal/proto/writer.go#L62.
switch u.(type) {
case struct{}: // Nothing unwraps to struct{}{}
u = nil
case nil, string, []byte,
int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64,
bool,
time.Time, time.Duration:
// just using unwrapped is fine, no need to modify it.
case encoding.BinaryMarshaler:
// TODO: figure out how it's used in the redis client.
return nil, fmt.Errorf("unhandled type")
case net.IP:
// TODO: return w.bytes(v)
return nil, fmt.Errorf("unhandled type")
default:
return nil, fmt.Errorf("unhandled type")
}
return u, nil
}
type resulter[R any] interface {
Result() (R, error)
}
func returnCmd[R any, C resulter[R]](cmd C) (sdktypes.Value, error) {
ret, err := cmd.Result()
switch {
case err == redis.Nil:
return sdktypes.NewNothingValue(), nil
case err != nil:
return nil, err
}
wrapped, err := sdkvalues.DefaultValueWrapper.Wrap(ret)
if err != nil {
return nil, fmt.Errorf("wrap: %w", err)
}
return wrapped, nil
}