-
Notifications
You must be signed in to change notification settings - Fork 245
/
encoding.go
72 lines (61 loc) · 2.32 KB
/
encoding.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
65
66
67
68
69
70
71
72
package types
import (
"fmt"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
)
// EncodeParameterTypes converts the map of internal caveat types into a map of types for storing
// the caveat in the core.
func EncodeParameterTypes(parametersAndTypes map[string]VariableType) map[string]*core.CaveatTypeReference {
encoded := make(map[string]*core.CaveatTypeReference, len(parametersAndTypes))
for name, varType := range parametersAndTypes {
encoded[name] = EncodeParameterType(varType)
}
return encoded
}
// EncodeParameterType converts an internal caveat type into a storable core type.
func EncodeParameterType(varType VariableType) *core.CaveatTypeReference {
childTypes := make([]*core.CaveatTypeReference, 0, len(varType.childTypes))
for _, childType := range varType.childTypes {
childTypes = append(childTypes, EncodeParameterType(childType))
}
return &core.CaveatTypeReference{
TypeName: varType.localName,
ChildTypes: childTypes,
}
}
// DecodeParameterType decodes the core caveat parameter type into an internal caveat type.
func DecodeParameterType(parameterType *core.CaveatTypeReference) (*VariableType, error) {
typeDef, ok := definitions[parameterType.TypeName]
if !ok {
return nil, fmt.Errorf("unknown caveat parameter type `%s`", parameterType.TypeName)
}
if len(parameterType.ChildTypes) != int(typeDef.childTypeCount) {
return nil, fmt.Errorf(
"caveat parameter type `%s` requires %d child types; found %d",
parameterType.TypeName,
len(parameterType.ChildTypes),
typeDef.childTypeCount,
)
}
childTypes := make([]VariableType, 0, typeDef.childTypeCount)
for _, encodedChildType := range parameterType.ChildTypes {
childType, err := DecodeParameterType(encodedChildType)
if err != nil {
return nil, err
}
childTypes = append(childTypes, *childType)
}
return typeDef.asVariableType(childTypes)
}
// DecodeParameterTypes decodes the core caveat parameter types into internal caveat types.
func DecodeParameterTypes(parameters map[string]*core.CaveatTypeReference) (map[string]VariableType, error) {
parameterTypes := make(map[string]VariableType, len(parameters))
for paramName, paramType := range parameters {
decodedType, err := DecodeParameterType(paramType)
if err != nil {
return nil, err
}
parameterTypes[paramName] = *decodedType
}
return parameterTypes, nil
}