| 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 ref contains the reference interfaces used throughout the types components. |
| 16 |
package ref |
| 17 |
|
| 18 |
import ( |
| 19 |
"reflect" |
| 20 |
) |
| 21 |
|
| 22 |
// Type interface indicate the name of a given type. |
| 23 |
type Type interface { |
| 24 |
// HasTrait returns whether the type has a given trait associated with it. |
| 25 |
// |
| 26 |
// See common/types/traits/traits.go for a list of supported traits. |
| 27 |
HasTrait(trait int) bool |
| 28 |
|
| 29 |
// TypeName returns the qualified type name of the type. |
| 30 |
// |
| 31 |
// The type name is also used as the type's identifier name at type-check and interpretation time. |
| 32 |
TypeName() string |
| 33 |
} |
| 34 |
|
| 35 |
// Val interface defines the functions supported by all expression values. |
| 36 |
// Val implementations may specialize the behavior of the value through the addition of traits. |
| 37 |
type Val interface { |
| 38 |
// ConvertToNative converts the Value to a native Go struct according to the |
| 39 |
// reflected type description, or error if the conversion is not feasible. |
| 40 |
// |
| 41 |
// The ConvertToNative method is intended to be used to support conversion between CEL types |
| 42 |
// and native types during object creation expressions or by clients who need to adapt the, |
| 43 |
// returned CEL value into an equivalent Go value instance. |
| 44 |
// |
| 45 |
// When implementing or using ConvertToNative, the following guidelines apply: |
| 46 |
// - Use ConvertToNative when marshalling CEL evaluation results to native types. |
| 47 |
// - Do not use ConvertToNative within CEL extension functions. |
| 48 |
// - Document whether your implementation supports non-CEL field types, such as Go or Protobuf. |
| 49 |
ConvertToNative(typeDesc reflect.Type) (any, error) |
| 50 |
|
| 51 |
// ConvertToType supports type conversions between CEL value types supported by the expression language. |
| 52 |
ConvertToType(typeValue Type) Val |
| 53 |
|
| 54 |
// Equal returns true if the `other` value has the same type and content as the implementing struct. |
| 55 |
Equal(other Val) Val |
| 56 |
|
| 57 |
// Type returns the TypeValue of the value. |
| 58 |
Type() Type |
| 59 |
|
| 60 |
// Value returns the raw value of the instance which may not be directly compatible with the expression |
| 61 |
// language types. |
| 62 |
Value() any |
| 63 |
} |