select
selects the first enum value if the specified condition is true
, and the second enum value if the condition is false
. It is used when performing field binding for enums that map multiple enums with an alias.
If you try to return an enum value without using this function, an error will occur due to type mismatch.
e.g.) true ? pkg.EnumType.Enum_VALUE_A : pkgv2.EnumType.ENUM_VALUE_B
grpc-federation: ERROR: <input>:1:6: found no matching overload for '_?_:_' applied to '(bool, pkg.EnumType(int), pkgv2.EnumType(int))'
| true ? pkg.EnumType.value('ENUM_VALUE_A') : pkgv2.EnumType.value('ENUM_VALUE_B')
| .....^
select(cond bool, <first-enum-value>, <second-enum-value>) grpc.federation.private.EnumSelector
cond
: condition to select enum valuefirst-enum-value
: first enum value. this is selected if the condition is true.second-enum-value
: second enum value. this is selected if the condition is false.
grpc.federation.enum.select(true, pkg.EnumType.ENUM_VALUE_A, pkgv2.EnumType.ENUM_VALUE_B)
For all enum types, you can use the name
method to obtain the name of the enum value as a string.
pkg.EnumType.name(<enum-value>) string
<enum-value>
: int or enum typed value
In the following case, "ENUM_VALUE_1"
( string typed value) is returned.
foo.EnumType.name(foo.EnumType.ENUM_VALUE_1)
package foo;
enum EnumType {
ENUM_VALUE_UNKNOWN = 0;
ENUM_VALUE_1 = 1;
}
For all enum types, you can use the value
method to obtain the enum typed value from name.
pkg.EnumType.value(enumValueName string) EnumValue
enumValueName
: name of the enum valueEnumValue
: enum typed value. This value can be used as an argument for theselect
function
In the following case, foo.EnumType.ENUM_VALUE_1
( enum typed value) is returned.
foo.EnumType.value('ENUM_VALUE_1')
package foo;
enum EnumType {
ENUM_VALUE_UNKNOWN = 0;
ENUM_VALUE_1 = 1;
}
For all enum types, you can use the from
method to obtain the enum typed value from int value.
pkg.EnumType.from(enumValue int) EnumValue
enumValue
: int typed value of the enum valueEnumValue
: enum typed value. This value can be used as an argument for theselect
function
In the following case, foo.EnumType.ENUM_VALUE_1
( enum typed value) is returned.
Note
Here, you might be confused by the difference between foo.EnumType.ENUM_VALUE_1
appearing in the expression and the returned foo.EnumType.ENUM_VALUE_1
value.
As a basic principle, CEL treats enum values as int types when specified directly.
The from
method is used to convert this to a typed enum value.
Although the result looks the same, it is actually typed and can be used as an argument for the select
function.
foo.EnumType.from(foo.EnumType.ENUM_VALUE_1)
package foo;
enum EnumType {
ENUM_VALUE_UNKNOWN = 0;
ENUM_VALUE_1 = 1;
}
If you use attr
to hold multiple name-value pairs corresponding to an enum value, you can get value from name
.
pkg.EnumType.attr(enumValue EnumValue, name string) string
enumValue
: the enum valuename
: string value to search attribute.
In the following case, Foo.text
value is foo
.
package mypkg;
message Foo {
option (grpc.federation.message) = {
def { name: "v" by: "Type.value('VALUE_1')" }
};
string text = 1 [(grpc.federation.field).by = "Type.attr(v, 'attr_x')"];
}
enum Type {
VALUE_1 = 1 [(grpc.federation.enum_value).attr = {
name: "attr_x"
value: "foo"
}];
VALUE_2 = 2 [(grpc.federation.enum_value).attr = {
name: "attr_x"
value: "bar"
}];
}