MCPcopy Create free account
hub / github.com/cel-expr/cel-go / ValToConstant

Function ValToConstant

common/ast/conversion.go:640–661  ·  view source on GitHub ↗

ValToConstant converts a CEL-native ref.Val to a protobuf Constant. Only simple scalar types are supported by this method.

(v ref.Val)

Source from the content-addressed store, hash-verified

638//
639// Only simple scalar types are supported by this method.
640func ValToConstant(v ref.Val) (*exprpb.Constant, error) {
641 if v == nil {
642 return nil, nil
643 }
644 switch v.Type() {
645 case types.BoolType:
646 return &exprpb.Constant{ConstantKind: &exprpb.Constant_BoolValue{BoolValue: v.Value().(bool)}}, nil
647 case types.BytesType:
648 return &exprpb.Constant{ConstantKind: &exprpb.Constant_BytesValue{BytesValue: v.Value().([]byte)}}, nil
649 case types.DoubleType:
650 return &exprpb.Constant{ConstantKind: &exprpb.Constant_DoubleValue{DoubleValue: v.Value().(float64)}}, nil
651 case types.IntType:
652 return &exprpb.Constant{ConstantKind: &exprpb.Constant_Int64Value{Int64Value: v.Value().(int64)}}, nil
653 case types.NullType:
654 return &exprpb.Constant{ConstantKind: &exprpb.Constant_NullValue{NullValue: structpb.NullValue_NULL_VALUE}}, nil
655 case types.StringType:
656 return &exprpb.Constant{ConstantKind: &exprpb.Constant_StringValue{StringValue: v.Value().(string)}}, nil
657 case types.UintType:
658 return &exprpb.Constant{ConstantKind: &exprpb.Constant_Uint64Value{Uint64Value: v.Value().(uint64)}}, nil
659 }
660 return nil, fmt.Errorf("unsupported constant kind: %v", v.Type())
661}
662
663// ConstantToVal converts a protobuf Constant to a CEL-native ref.Val.
664func ConstantToVal(c *exprpb.Constant) (ref.Val, error) {

Callers 4

TestConvertValFunction · 0.92
TestValToConstantErrorFunction · 0.92
protoLiteralFunction · 0.85
ReferenceInfoToProtoFunction · 0.85

Calls 2

TypeMethod · 0.65
ValueMethod · 0.65

Tested by 2

TestConvertValFunction · 0.74
TestValToConstantErrorFunction · 0.74