github.com/openfga/openfga@v1.5.4-rc1/internal/condition/types/encoding.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  
     6  	openfgav1 "github.com/openfga/api/proto/openfga/v1"
     7  )
     8  
     9  func DecodeParameterType(conditionParamType *openfgav1.ConditionParamTypeRef) (*ParameterType, error) {
    10  	paramTypedef, ok := paramTypeDefinitions[conditionParamType.GetTypeName()]
    11  	if !ok {
    12  		return nil, fmt.Errorf("unknown condition parameter type `%s`", conditionParamType.GetTypeName())
    13  	}
    14  
    15  	if len(conditionParamType.GetGenericTypes()) != int(paramTypedef.genericTypeCount) {
    16  		return nil, fmt.Errorf(
    17  			"condition parameter type `%s` requires %d generic types; found %d",
    18  			conditionParamType.GetTypeName(),
    19  			len(conditionParamType.GetGenericTypes()),
    20  			paramTypedef.genericTypeCount,
    21  		)
    22  	}
    23  
    24  	genericTypes := make([]ParameterType, 0, paramTypedef.genericTypeCount)
    25  	for _, encodedGenericType := range conditionParamType.GetGenericTypes() {
    26  		genericType, err := DecodeParameterType(encodedGenericType)
    27  		if err != nil {
    28  			return nil, err
    29  		}
    30  
    31  		genericTypes = append(genericTypes, *genericType)
    32  	}
    33  
    34  	return paramTypedef.toParameterType(genericTypes)
    35  }