-
Notifications
You must be signed in to change notification settings - Fork 278
/
context.go
45 lines (35 loc) · 901 Bytes
/
context.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
package caveats
import (
"maps"
"time"
"google.golang.org/protobuf/types/known/structpb"
"github.com/authzed/spicedb/pkg/caveats/types"
)
// ConvertContextToStruct converts the given context values into a context struct.
func ConvertContextToStruct(contextValues map[string]any) (*structpb.Struct, error) {
cloned := maps.Clone(contextValues)
cloned = convertCustomValues(cloned).(map[string]any)
return structpb.NewStruct(cloned)
}
func convertCustomValues(value any) any {
switch v := value.(type) {
case map[string]any:
for key, value := range v {
v[key] = convertCustomValues(value)
}
return v
case []any:
for index, current := range v {
v[index] = convertCustomValues(current)
}
return v
case time.Time:
return v.Format(time.RFC3339)
case time.Duration:
return v.String()
case types.CustomType:
return v.SerializedString()
default:
return v
}
}