summaryrefslogtreecommitdiff
path: root/vendor/github.com/authzed/spicedb/pkg/caveats/context.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/authzed/spicedb/pkg/caveats/context.go')
-rw-r--r--vendor/github.com/authzed/spicedb/pkg/caveats/context.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/vendor/github.com/authzed/spicedb/pkg/caveats/context.go b/vendor/github.com/authzed/spicedb/pkg/caveats/context.go
new file mode 100644
index 0000000..5580eb4
--- /dev/null
+++ b/vendor/github.com/authzed/spicedb/pkg/caveats/context.go
@@ -0,0 +1,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
+ }
+}