github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/caveats/types/encoding.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  
     6  	core "github.com/authzed/spicedb/pkg/proto/core/v1"
     7  )
     8  
     9  // EncodeParameterTypes converts the map of internal caveat types into a map of types for storing
    10  // the caveat in the core.
    11  func EncodeParameterTypes(parametersAndTypes map[string]VariableType) map[string]*core.CaveatTypeReference {
    12  	encoded := make(map[string]*core.CaveatTypeReference, len(parametersAndTypes))
    13  	for name, varType := range parametersAndTypes {
    14  		encoded[name] = EncodeParameterType(varType)
    15  	}
    16  	return encoded
    17  }
    18  
    19  // EncodeParameterType converts an internal caveat type into a storable core type.
    20  func EncodeParameterType(varType VariableType) *core.CaveatTypeReference {
    21  	childTypes := make([]*core.CaveatTypeReference, 0, len(varType.childTypes))
    22  	for _, childType := range varType.childTypes {
    23  		childTypes = append(childTypes, EncodeParameterType(childType))
    24  	}
    25  
    26  	return &core.CaveatTypeReference{
    27  		TypeName:   varType.localName,
    28  		ChildTypes: childTypes,
    29  	}
    30  }
    31  
    32  // DecodeParameterType decodes the core caveat parameter type into an internal caveat type.
    33  func DecodeParameterType(parameterType *core.CaveatTypeReference) (*VariableType, error) {
    34  	typeDef, ok := definitions[parameterType.TypeName]
    35  	if !ok {
    36  		return nil, fmt.Errorf("unknown caveat parameter type `%s`", parameterType.TypeName)
    37  	}
    38  
    39  	if len(parameterType.ChildTypes) != int(typeDef.childTypeCount) {
    40  		return nil, fmt.Errorf(
    41  			"caveat parameter type `%s` requires %d child types; found %d",
    42  			parameterType.TypeName,
    43  			len(parameterType.ChildTypes),
    44  			typeDef.childTypeCount,
    45  		)
    46  	}
    47  
    48  	childTypes := make([]VariableType, 0, typeDef.childTypeCount)
    49  	for _, encodedChildType := range parameterType.ChildTypes {
    50  		childType, err := DecodeParameterType(encodedChildType)
    51  		if err != nil {
    52  			return nil, err
    53  		}
    54  		childTypes = append(childTypes, *childType)
    55  	}
    56  
    57  	return typeDef.asVariableType(childTypes)
    58  }
    59  
    60  // DecodeParameterTypes decodes the core caveat parameter types into internal caveat types.
    61  func DecodeParameterTypes(parameters map[string]*core.CaveatTypeReference) (map[string]VariableType, error) {
    62  	parameterTypes := make(map[string]VariableType, len(parameters))
    63  	for paramName, paramType := range parameters {
    64  		decodedType, err := DecodeParameterType(paramType)
    65  		if err != nil {
    66  			return nil, err
    67  		}
    68  
    69  		parameterTypes[paramName] = *decodedType
    70  	}
    71  	return parameterTypes, nil
    72  }