| 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 |
"strings" |
| 21 |
|
| 22 |
"google.golang.org/protobuf/proto" |
| 23 |
|
| 24 |
"github.com/google/cel-go/common/types/ref" |
| 25 |
|
| 26 |
anypb "google.golang.org/protobuf/types/known/anypb" |
| 27 |
structpb "google.golang.org/protobuf/types/known/structpb" |
| 28 |
) |
| 29 |
|
| 30 |
// Null type implementation. |
| 31 |
type Null structpb.NullValue |
| 32 |
|
| 33 |
var ( |
| 34 |
// NullValue singleton. |
| 35 |
NullValue = Null(structpb.NullValue_NULL_VALUE) |
| 36 |
|
| 37 |
// golang reflect type for Null values. |
| 38 |
nullReflectType = reflect.TypeOf(NullValue) |
| 39 |
|
| 40 |
protoIfaceType = reflect.TypeOf((*proto.Message)(nil)).Elem() |
| 41 |
) |
| 42 |
|
| 43 |
// ConvertToNative implements ref.Val.ConvertToNative. |
| 44 |
func (n Null) ConvertToNative(typeDesc reflect.Type) (any, error) { |
| 45 |
switch typeDesc.Kind() { |
| 46 |
case reflect.Int32: |
| 47 |
switch typeDesc { |
| 48 |
case JSONNullType: |
| 49 |
return structpb.NullValue_NULL_VALUE, nil |
| 50 |
case nullReflectType: |
| 51 |
return n, nil |
| 52 |
} |
| 53 |
case reflect.Ptr: |
| 54 |
switch typeDesc { |
| 55 |
case anyValueType: |
| 56 |
// Convert to a JSON-null before packing to an Any field since the enum value for JSON |
| 57 |
// null cannot be packed directly. |
| 58 |
pb, err := n.ConvertToNative(JSONValueType) |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
return anypb.New(pb.(proto.Message)) |
| 63 |
case JSONValueType: |
| 64 |
return structpb.NewNullValue(), nil |
| 65 |
case boolWrapperType, byteWrapperType, doubleWrapperType, floatWrapperType, |
| 66 |
int32WrapperType, int64WrapperType, stringWrapperType, uint32WrapperType, |
| 67 |
uint64WrapperType, durationValueType, timestampValueType, protoIfaceType: |
| 68 |
return nil, nil |
| 69 |
case JSONListType, JSONStructType: |
| 70 |
// skip handling |
| 71 |
default: |
| 72 |
if typeDesc.Implements(protoIfaceType) { |
| 73 |
return nil, nil |
| 74 |
} |
| 75 |
} |
| 76 |
case reflect.Interface: |
| 77 |
nv := n.Value() |
| 78 |
if reflect.TypeOf(nv).Implements(typeDesc) { |
| 79 |
return nv, nil |
| 80 |
} |
| 81 |
if reflect.TypeOf(n).Implements(typeDesc) { |
| 82 |
return n, nil |
| 83 |
} |
| 84 |
} |
| 85 |
// If the type conversion isn't supported return an error. |
| 86 |
return nil, fmt.Errorf("type conversion error from '%v' to '%v'", NullType, typeDesc) |
| 87 |
} |
| 88 |
|
| 89 |
// ConvertToType implements ref.Val.ConvertToType. |
| 90 |
func (n Null) ConvertToType(typeVal ref.Type) ref.Val { |
| 91 |
switch typeVal { |
| 92 |
case StringType: |
| 93 |
return String("null") |
| 94 |
case NullType: |
| 95 |
return n |
| 96 |
case TypeType: |
| 97 |
return NullType |
| 98 |
} |
| 99 |
return NewErr("type conversion error from '%s' to '%s'", NullType, typeVal) |
| 100 |
} |
| 101 |
|
| 102 |
// Equal implements ref.Val.Equal. |
| 103 |
func (n Null) Equal(other ref.Val) ref.Val { |
| 104 |
return Bool(NullType == other.Type()) |
| 105 |
} |
| 106 |
|
| 107 |
// IsZeroValue returns true as null always represents an absent value. |
| 108 |
func (n Null) IsZeroValue() bool { |
| 109 |
return true |
| 110 |
} |
| 111 |
|
| 112 |
// Type implements ref.Val.Type. |
| 113 |
func (n Null) Type() ref.Type { |
| 114 |
return NullType |
| 115 |
} |
| 116 |
|
| 117 |
// Value implements ref.Val.Value. |
| 118 |
func (n Null) Value() any { |
| 119 |
return structpb.NullValue_NULL_VALUE |
| 120 |
} |
| 121 |
|
| 122 |
func (n Null) format(sb *strings.Builder) { |
| 123 |
sb.WriteString("null") |
| 124 |
} |