github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/caveats/context.go (about) 1 package caveats 2 3 import ( 4 "maps" 5 "time" 6 7 "google.golang.org/protobuf/types/known/structpb" 8 9 "github.com/authzed/spicedb/pkg/caveats/types" 10 ) 11 12 // ConvertContextToStruct converts the given context values into a context struct. 13 func ConvertContextToStruct(contextValues map[string]any) (*structpb.Struct, error) { 14 cloned := maps.Clone(contextValues) 15 cloned = convertCustomValues(cloned).(map[string]any) 16 return structpb.NewStruct(cloned) 17 } 18 19 func convertCustomValues(value any) any { 20 switch v := value.(type) { 21 case map[string]any: 22 for key, value := range v { 23 v[key] = convertCustomValues(value) 24 } 25 return v 26 27 case []any: 28 for index, current := range v { 29 v[index] = convertCustomValues(current) 30 } 31 return v 32 33 case time.Time: 34 return v.Format(time.RFC3339) 35 36 case time.Duration: 37 return v.String() 38 39 case types.CustomType: 40 return v.SerializedString() 41 42 default: 43 return v 44 } 45 }