@@ -2,6 +2,7 @@ package redis
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"strconv"
6
7
)
7
8
@@ -19,7 +20,7 @@ type VectorSetCmdable interface {
19
20
VRandMember (ctx context.Context , key string ) * StringCmd
20
21
VRandMemberCount (ctx context.Context , key string , count int ) * StringSliceCmd
21
22
VRem (ctx context.Context , key , element string ) * BoolCmd
22
- VSetAttr (ctx context.Context , key , element string , attr VectorAttributeMarshaller ) * BoolCmd
23
+ VSetAttr (ctx context.Context , key , element string , attr interface {} ) * BoolCmd
23
24
VClearAttributes (ctx context.Context , key , element string ) * BoolCmd
24
25
VSim (ctx context.Context , key string , val Vector ) * StringSliceCmd
25
26
VSimWithScores (ctx context.Context , key string , val Vector ) * VectorScoreSliceCmd
@@ -77,18 +78,6 @@ type VectorScore struct {
77
78
Score float64
78
79
}
79
80
80
- type VectorAttributeMarshaller interface {
81
- Marshall () string
82
- }
83
-
84
- type VectorAttributeRawString string
85
-
86
- func (a * VectorAttributeRawString ) Marshall () string {
87
- return string (* a )
88
- }
89
-
90
- var _ VectorAttributeMarshaller = (* VectorAttributeRawString )(nil )
91
-
92
81
// `VADD key (FP32 | VALUES num) vector element`
93
82
// note: the API is experimental and may be subject to change.
94
83
func (c cmdable ) VAdd (ctx context.Context , key , element string , val Vector ) * BoolCmd {
@@ -242,9 +231,30 @@ func (c cmdable) VRem(ctx context.Context, key, element string) *BoolCmd {
242
231
}
243
232
244
233
// `VSETATTR key element "{ JSON obj }"`
234
+ // The `attr` must be something that can be marshaled to JSON (using encoding/JSON) unless
235
+ // the argument is a string or []byte when we assume that it can be passed directly as JSON.
236
+ //
245
237
// note: the API is experimental and may be subject to change.
246
- func (c cmdable ) VSetAttr (ctx context.Context , key , element string , attr VectorAttributeMarshaller ) * BoolCmd {
247
- cmd := NewBoolCmd (ctx , "vsetattr" , key , element , attr .Marshall ())
238
+ func (c cmdable ) VSetAttr (ctx context.Context , key , element string , attr interface {}) * BoolCmd {
239
+ var attrStr string
240
+ var err error
241
+ switch v := attr .(type ) {
242
+ case string :
243
+ attrStr = v
244
+ case []byte :
245
+ attrStr = string (v )
246
+ default :
247
+ var bytes []byte
248
+ bytes , err = json .Marshal (v )
249
+ if err != nil {
250
+ // If marshalling fails, create the command and set the error; this command won't be executed.
251
+ cmd := NewBoolCmd (ctx , "vsetattr" , key , element , "" )
252
+ cmd .SetErr (err )
253
+ return cmd
254
+ }
255
+ attrStr = string (bytes )
256
+ }
257
+ cmd := NewBoolCmd (ctx , "vsetattr" , key , element , attrStr )
248
258
_ = c (ctx , cmd )
249
259
return cmd
250
260
}
0 commit comments