MCPcopy Create free account
hub / github.com/cel-expr/cel-go / zeroValueExpr

Function zeroValueExpr

cel/inlining.go:192–211  ·  view source on GitHub ↗

zeroValueExpr creates an expression representing the empty or zero value for the given type Note: bytes, lists, maps, and strings are supported via the `SizerType` trait.

(ctx *OptimizerContext, t *Type)

Source from the content-addressed store, hash-verified

190// zeroValueExpr creates an expression representing the empty or zero value for the given type
191// Note: bytes, lists, maps, and strings are supported via the `SizerType` trait.
192func zeroValueExpr(ctx *OptimizerContext, t *Type) (ast.Expr, bool) {
193 // Note: bytes, strings, lists, and maps are covered by the "sizer-type" check
194 switch t.Kind() {
195 case types.BoolKind:
196 return ctx.NewLiteral(types.False), true
197 case types.DoubleKind:
198 return ctx.NewLiteral(types.Double(0)), true
199 case types.DurationKind:
200 return ctx.NewCall(overloads.TypeConvertDuration, ctx.NewLiteral(types.String("0s"))), true
201 case types.IntKind:
202 return ctx.NewLiteral(types.IntZero), true
203 case types.TimestampKind:
204 return ctx.NewCall(overloads.TypeConvertTimestamp, ctx.NewLiteral(types.Int(0))), true
205 case types.StructKind:
206 return ctx.NewStruct(t.TypeName(), []ast.EntryExpr{}), true
207 case types.UintKind:
208 return ctx.NewLiteral(types.Uint(0)), true
209 }
210 return nil, false
211}
212
213// isBindable indicates whether the inlined type can be used within a cel.bind() if the expression
214// being replaced occurs within a presence test. Value types with a size() method or field selection

Callers 1

rewritePresenceExprMethod · 0.85

Calls 9

DoubleTypeAlias · 0.92
StringTypeAlias · 0.92
IntTypeAlias · 0.92
UintTypeAlias · 0.92
KindMethod · 0.65
NewLiteralMethod · 0.65
NewCallMethod · 0.65
NewStructMethod · 0.65
TypeNameMethod · 0.65

Tested by

no test coverage detected