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

Method Validate

cel/validator.go:351–406  ·  view source on GitHub ↗

Validate validates that all lists and map literals have homogeneous types, i.e. don't contain dyn types. This validator makes an exception for list and map literals which occur at any level of nesting within string format calls.

(_ *Env, c ValidatorConfig, a *ast.AST, iss *Issues)

Source from the content-addressed store, hash-verified

349// This validator makes an exception for list and map literals which occur at any level of nesting within
350// string format calls.
351func (v homogeneousAggregateLiteralValidator) Validate(_ *Env, c ValidatorConfig, a *ast.AST, iss *Issues) {
352 var exemptedFunctions []string
353 exemptedFunctions = c.GetOrDefault(HomogeneousAggregateLiteralExemptFunctions, exemptedFunctions).([]string)
354 root := ast.NavigateAST(a)
355 listExprs := ast.MatchDescendants(root, ast.KindMatcher(ast.ListKind))
356 for _, listExpr := range listExprs {
357 if inExemptFunction(listExpr, exemptedFunctions) {
358 continue
359 }
360 l := listExpr.AsList()
361 elements := l.Elements()
362 optIndices := l.OptionalIndices()
363 var elemType *Type
364 for i, e := range elements {
365 et := a.GetType(e.ID())
366 if isOptionalIndex(i, optIndices) {
367 et = et.Parameters()[0]
368 }
369 if elemType == nil {
370 elemType = et
371 continue
372 }
373 if !elemType.IsEquivalentType(et) {
374 v.typeMismatch(iss, e.ID(), elemType, et)
375 break
376 }
377 }
378 }
379 mapExprs := ast.MatchDescendants(root, ast.KindMatcher(ast.MapKind))
380 for _, mapExpr := range mapExprs {
381 if inExemptFunction(mapExpr, exemptedFunctions) {
382 continue
383 }
384 m := mapExpr.AsMap()
385 entries := m.Entries()
386 var keyType, valType *Type
387 for _, e := range entries {
388 mapEntry := e.AsMapEntry()
389 key, val := mapEntry.Key(), mapEntry.Value()
390 kt, vt := a.GetType(key.ID()), a.GetType(val.ID())
391 if mapEntry.IsOptional() {
392 vt = vt.Parameters()[0]
393 }
394 if keyType == nil && valType == nil {
395 keyType, valType = kt, vt
396 continue
397 }
398 if !keyType.IsEquivalentType(kt) {
399 v.typeMismatch(iss, key.ID(), keyType, kt)
400 }
401 if !valType.IsEquivalentType(vt) {
402 v.typeMismatch(iss, val.ID(), valType, vt)
403 }
404 }
405 }
406}
407
408func inExemptFunction(e ast.NavigableExpr, exemptFunctions []string) bool {

Callers

nothing calls this directly

Calls 15

IsEquivalentTypeMethod · 0.95
typeMismatchMethod · 0.95
NavigateASTFunction · 0.92
MatchDescendantsFunction · 0.92
KindMatcherFunction · 0.92
inExemptFunctionFunction · 0.85
ParametersMethod · 0.80
isOptionalIndexFunction · 0.70
GetOrDefaultMethod · 0.65
AsListMethod · 0.65
ElementsMethod · 0.65
OptionalIndicesMethod · 0.65

Tested by

no test coverage detected