| 1 |
// Copyright 2022 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 |
"errors" |
| 19 |
"fmt" |
| 20 |
"reflect" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"github.com/google/cel-go/common/types/ref" |
| 24 |
) |
| 25 |
|
| 26 |
var ( |
| 27 |
// OptionalType indicates the runtime type of an optional value. |
| 28 |
OptionalType = NewOpaqueType("optional_type") |
| 29 |
|
| 30 |
// OptionalNone is a sentinel value which is used to indicate an empty optional value. |
| 31 |
OptionalNone = &Optional{} |
| 32 |
) |
| 33 |
|
| 34 |
// OptionalOf returns an optional value which wraps a concrete CEL value. |
| 35 |
func OptionalOf(value ref.Val) *Optional { |
| 36 |
return &Optional{value: value} |
| 37 |
} |
| 38 |
|
| 39 |
// Optional value which points to a value if non-empty. |
| 40 |
type Optional struct { |
| 41 |
value ref.Val |
| 42 |
} |
| 43 |
|
| 44 |
// HasValue returns true if the optional has a value. |
| 45 |
func (o *Optional) HasValue() bool { |
| 46 |
return o.value != nil |
| 47 |
} |
| 48 |
|
| 49 |
// GetValue returns the wrapped value contained in the optional. |
| 50 |
func (o *Optional) GetValue() ref.Val { |
| 51 |
if !o.HasValue() { |
| 52 |
return NewErr("optional.none() dereference") |
| 53 |
} |
| 54 |
return o.value |
| 55 |
} |
| 56 |
|
| 57 |
// ConvertToNative implements the ref.Val interface method. |
| 58 |
func (o *Optional) ConvertToNative(typeDesc reflect.Type) (any, error) { |
| 59 |
if !o.HasValue() { |
| 60 |
return nil, errors.New("optional.none() dereference") |
| 61 |
} |
| 62 |
return o.value.ConvertToNative(typeDesc) |
| 63 |
} |
| 64 |
|
| 65 |
// ConvertToType implements the ref.Val interface method. |
| 66 |
func (o *Optional) ConvertToType(typeVal ref.Type) ref.Val { |
| 67 |
switch typeVal { |
| 68 |
case OptionalType: |
| 69 |
return o |
| 70 |
case TypeType: |
| 71 |
return OptionalType |
| 72 |
} |
| 73 |
return NewErr("type conversion error from '%s' to '%s'", OptionalType, typeVal) |
| 74 |
} |
| 75 |
|
| 76 |
// Equal determines whether the values contained by two optional values are equal. |
| 77 |
func (o *Optional) Equal(other ref.Val) ref.Val { |
| 78 |
otherOpt, isOpt := other.(*Optional) |
| 79 |
if !isOpt { |
| 80 |
return False |
| 81 |
} |
| 82 |
if !o.HasValue() { |
| 83 |
return Bool(!otherOpt.HasValue()) |
| 84 |
} |
| 85 |
if !otherOpt.HasValue() { |
| 86 |
return False |
| 87 |
} |
| 88 |
return o.value.Equal(otherOpt.value) |
| 89 |
} |
| 90 |
|
| 91 |
func (o *Optional) String() string { |
| 92 |
if o.HasValue() { |
| 93 |
return fmt.Sprintf("optional(%v)", o.GetValue()) |
| 94 |
} |
| 95 |
return "optional.none()" |
| 96 |
} |
| 97 |
|
| 98 |
func (o *Optional) format(sb *strings.Builder) { |
| 99 |
if o.HasValue() { |
| 100 |
sb.WriteString(`optional.of(`) |
| 101 |
formatTo(sb, o.GetValue()) |
| 102 |
sb.WriteString(`)`) |
| 103 |
} else { |
| 104 |
sb.WriteString("optional.none()") |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// Type implements the ref.Val interface method. |
| 109 |
func (o *Optional) Type() ref.Type { |
| 110 |
return OptionalType |
| 111 |
} |
| 112 |
|
| 113 |
// Value returns the underlying 'Value()' of the wrapped value, if present. |
| 114 |
func (o *Optional) Value() any { |
| 115 |
if o.value == nil { |
| 116 |
return nil |
| 117 |
} |
| 118 |
return o.value.Value() |
| 119 |
} |