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

Method checkCreateStruct

checker/checker.go:490–554  ·  view source on GitHub ↗
(e ast.Expr)

Source from the content-addressed store, hash-verified

488}
489
490func (c *checker) checkCreateStruct(e ast.Expr) {
491 msgVal := e.AsStruct()
492 // Determine the type of the message.
493 resultType := types.ErrorType
494 ident := c.env.resolveTypeIdent(msgVal.TypeName())
495 if ident == nil {
496 c.errors.undeclaredReference(
497 e.ID(), c.location(e), c.env.container.Name(), msgVal.TypeName())
498 c.setType(e, types.ErrorType)
499 return
500 }
501 // Ensure the type name is fully qualified in the AST.
502 typeName := ident.Name()
503 if msgVal.TypeName() != typeName {
504 e.SetKindCase(c.NewStruct(e.ID(), typeName, msgVal.Fields()))
505 msgVal = e.AsStruct()
506 }
507 c.setReference(e, ast.NewIdentReference(typeName, nil))
508 identKind := ident.Type().Kind()
509 if identKind != types.ErrorKind {
510 if identKind != types.TypeKind {
511 c.errors.notAType(e.ID(), c.location(e), ident.Type().DeclaredTypeName())
512 } else {
513 resultType = ident.Type().Parameters()[0]
514 // Backwards compatibility test between well-known types and message types
515 // In this context, the type is being instantiated by its protobuf name which
516 // is not ideal or recommended, but some users expect this to work.
517 if isWellKnownType(resultType) {
518 typeName = getWellKnownTypeName(resultType)
519 } else if resultType.Kind() == types.StructKind {
520 typeName = resultType.DeclaredTypeName()
521 } else {
522 c.errors.notAMessageType(e.ID(), c.location(e), resultType.DeclaredTypeName())
523 resultType = types.ErrorType
524 }
525 }
526 }
527 c.setType(e, resultType)
528
529 // Check the field initializers.
530 for _, f := range msgVal.Fields() {
531 field := f.AsStructField()
532 fieldName := field.Name()
533 value := field.Value()
534 c.check(value)
535
536 fieldType := types.ErrorType
537 ft, found := c.lookupFieldType(f.ID(), typeName, fieldName)
538 if found {
539 fieldType = ft
540 }
541
542 valType := c.getType(value)
543 if field.IsOptional() {
544 var isOptional bool
545 valType, isOptional = maybeUnwrapOptional(valType)
546 if !isOptional && !isDyn(valType) {
547 c.errors.typeMismatch(value.ID(), c.location(value), types.NewOptionalType(valType), valType)

Callers 1

checkMethod · 0.95

Calls 15

locationMethod · 0.95
setTypeMethod · 0.95
setReferenceMethod · 0.95
checkMethod · 0.95
lookupFieldTypeMethod · 0.95
getTypeMethod · 0.95
isAssignableMethod · 0.95
locationByIDMethod · 0.95
NewIdentReferenceFunction · 0.92
NewOptionalTypeFunction · 0.92
isWellKnownTypeFunction · 0.85
getWellKnownTypeNameFunction · 0.85

Tested by

no test coverage detected