package types
import (
"fmt"
"reflect"
"google.golang.org/protobuf/proto"
"github.com/google/cel-go/common/types/ref"
anypb "google.golang.org/protobuf/types/known/anypb"
structpb "google.golang.org/protobuf/types/known/structpb"
)
type Null structpb.NullValue
var (
NullValue = Null(structpb.NullValue_NULL_VALUE)
nullReflectType = reflect.TypeOf(NullValue)
)
func (n Null) ConvertToNative(typeDesc reflect.Type) (any, error) {
switch typeDesc.Kind() {
case reflect.Int32:
switch typeDesc {
case jsonNullType:
return structpb.NullValue_NULL_VALUE, nil
case nullReflectType:
return n, nil
}
case reflect.Ptr:
switch typeDesc {
case anyValueType:
pb, err := n.ConvertToNative(jsonValueType)
if err != nil {
return nil, err
}
return anypb.New(pb.(proto.Message))
case jsonValueType:
return structpb.NewNullValue(), nil
case boolWrapperType, byteWrapperType, doubleWrapperType, floatWrapperType,
int32WrapperType, int64WrapperType, stringWrapperType, uint32WrapperType,
uint64WrapperType:
return nil, nil
}
case reflect.Interface:
nv := n.Value()
if reflect.TypeOf(nv).Implements(typeDesc) {
return nv, nil
}
if reflect.TypeOf(n).Implements(typeDesc) {
return n, nil
}
}
return nil, fmt.Errorf("type conversion error from '%v' to '%v'", NullType, typeDesc)
}
func (n Null) ConvertToType(typeVal ref.Type) ref.Val {
switch typeVal {
case StringType:
return String("null")
case NullType:
return n
case TypeType:
return NullType
}
return NewErr("type conversion error from '%s' to '%s'", NullType, typeVal)
}
func (n Null) Equal(other ref.Val) ref.Val {
return Bool(NullType == other.Type())
}
func (n Null) IsZeroValue() bool {
return true
}
func (n Null) Type() ref.Type {
return NullType
}
func (n Null) Value() any {
return structpb.NullValue_NULL_VALUE
}