| 1 | // Copyright 2018 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package types |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "reflect" |
| 20 | "strconv" |
| 21 | "strings" |
| 22 | |
| 23 | "github.com/google/cel-go/common/types/ref" |
| 24 | |
| 25 | anypb "google.golang.org/protobuf/types/known/anypb" |
| 26 | structpb "google.golang.org/protobuf/types/known/structpb" |
| 27 | wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" |
| 28 | ) |
| 29 | |
| 30 | // Bool type that implements ref.Val and supports comparison and negation. |
| 31 | type Bool bool |
| 32 | |
| 33 | var ( |
| 34 | // boolWrapperType golang reflected type for protobuf bool wrapper type. |
| 35 | boolWrapperType = reflect.TypeOf(&wrapperspb.BoolValue{}) |
| 36 | ) |
| 37 | |
| 38 | // Boolean constants |
| 39 | const ( |
| 40 | False = Bool(false) |
| 41 | True = Bool(true) |
| 42 | ) |
| 43 | |
| 44 | // Compare implements the traits.Comparer interface method. |
| 45 | func (b Bool) Compare(other ref.Val) ref.Val { |
| 46 | otherBool, ok := other.(Bool) |
| 47 | if !ok { |
| 48 | return ValOrErr(other, "no such overload") |
| 49 | } |
| 50 | if b == otherBool { |
| 51 | return IntZero |
| 52 | } |
| 53 | if !b && otherBool { |
| 54 | return IntNegOne |
| 55 | } |
| 56 | return IntOne |
| 57 | } |
| 58 | |
| 59 | // ConvertToNative implements the ref.Val interface method. |
| 60 | func (b Bool) ConvertToNative(typeDesc reflect.Type) (any, error) { |
| 61 | switch typeDesc.Kind() { |
| 62 | case reflect.Bool: |
| 63 | return reflect.ValueOf(b).Convert(typeDesc).Interface(), nil |
| 64 | case reflect.Ptr: |
| 65 | switch typeDesc { |
| 66 | case anyValueType: |
| 67 | // Primitives must be wrapped to a wrapperspb.BoolValue before being packed into an Any. |
| 68 | return anypb.New(wrapperspb.Bool(bool(b))) |
| 69 | case boolWrapperType: |
| 70 | // Convert the bool to a wrapperspb.BoolValue. |
| 71 | return wrapperspb.Bool(bool(b)), nil |
| 72 | case JSONValueType: |
| 73 | // Return the bool as a new structpb.Value. |
| 74 | return structpb.NewBoolValue(bool(b)), nil |
| 75 | default: |
| 76 | if typeDesc.Elem().Kind() == reflect.Bool { |
| 77 | p := bool(b) |
| 78 | return &p, nil |
| 79 | } |
| 80 | } |
| 81 | case reflect.Interface: |
| 82 | bv := b.Value() |
| 83 | if reflect.TypeOf(bv).Implements(typeDesc) { |
| 84 | return bv, nil |
| 85 | } |
| 86 | if reflect.TypeOf(b).Implements(typeDesc) { |
| 87 | return b, nil |
| 88 | } |
| 89 | } |
| 90 | return nil, fmt.Errorf("type conversion error from bool to '%v'", typeDesc) |
| 91 | } |
| 92 | |
| 93 | // ConvertToType implements the ref.Val interface method. |
| 94 | func (b Bool) ConvertToType(typeVal ref.Type) ref.Val { |
| 95 | switch typeVal { |
| 96 | case StringType: |
| 97 | return String(strconv.FormatBool(bool(b))) |
| 98 | case BoolType: |
| 99 | return b |
| 100 | case TypeType: |
| 101 | return BoolType |
| 102 | } |
| 103 | return NewErr("type conversion error from '%v' to '%v'", BoolType, typeVal) |
| 104 | } |
| 105 | |
| 106 | // Equal implements the ref.Val interface method. |
| 107 | func (b Bool) Equal(other ref.Val) ref.Val { |
| 108 | otherBool, ok := other.(Bool) |
| 109 | return Bool(ok && b == otherBool) |
| 110 | } |
| 111 | |
| 112 | // IsZeroValue returns true if the boolean value is false. |
| 113 | func (b Bool) IsZeroValue() bool { |
| 114 | return b == False |
| 115 | } |
| 116 | |
| 117 | // Negate implements the traits.Negater interface method. |
| 118 | func (b Bool) Negate() ref.Val { |
| 119 | return !b |
| 120 | } |
| 121 | |
| 122 | // Type implements the ref.Val interface method. |
| 123 | func (b Bool) Type() ref.Type { |
| 124 | return BoolType |
| 125 | } |
| 126 | |
| 127 | // Value implements the ref.Val interface method. |
| 128 | func (b Bool) Value() any { |
| 129 | return bool(b) |
| 130 | } |
| 131 | |
| 132 | func (b Bool) format(sb *strings.Builder) { |
| 133 | if b { |
| 134 | sb.WriteString("true") |
| 135 | } else { |
| 136 | sb.WriteString("false") |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // IsBool returns whether the input ref.Val or ref.Type is equal to BoolType. |
| 141 | func IsBool(elem ref.Val) bool { |
| 142 | switch v := elem.(type) { |
| 143 | case Bool: |
| 144 | return true |
| 145 | case ref.Val: |
| 146 | return v.Type() == BoolType |
| 147 | default: |
| 148 | return false |
| 149 | } |
| 150 | } |