ValToConstant converts a CEL-native ref.Val to a protobuf Constant. Only simple scalar types are supported by this method.
(v ref.Val)
| 638 | // |
| 639 | // Only simple scalar types are supported by this method. |
| 640 | func 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. |
| 664 | func ConstantToVal(c *exprpb.Constant) (ref.Val, error) { |