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

Method combine

policy/composer.go:586–633  ·  view source on GitHub ↗

combine assembles a new compositionStep from the target output step an an input output step. optional.combine(optional) // optional (optional && conditional).combine(non-optional) // optional (optional && unconditional).combine(non-optional) // non-optional The last combination case indicates that

(step compositionStep)

Source from the content-addressed store, hash-verified

584// The last combination case indicates that an optional value in one case should be resolved
585// to a non-optional value as
586func (s optionalCompositionStep) combine(step compositionStep) compositionStep {
587 if step == nil {
588 // This is likely unreachable for an optional step, but worth adding as a safeguard
589 return s
590 }
591 ctx := s.ctx
592 trueCondition := ctx.NewLiteral(types.True)
593 if step.isOptional() {
594 // Introduce a ternary to capture the conditional return when combining a
595 // conditional optional with another optional.
596 if s.isConditional() {
597 return newOptionalCompositionStep(ctx,
598 trueCondition,
599 ctx.NewCall(operators.Conditional,
600 s.condition(),
601 s.expr(),
602 step.expr()),
603 )
604 }
605 // When an optional is unconditionally combined with another optional, rely
606 // on the optional 'or' to fall-through from one optional to another.
607 if !isOptionalNone(step.expr()) {
608 return newOptionalCompositionStep(ctx,
609 trueCondition,
610 ctx.NewMemberCall("or", s.expr(), step.expr()))
611 }
612 // Otherwise, the current step 's' is unconditional and effectively prunes away
613 // the other input 'step'.
614 return s
615 }
616 if s.isConditional() {
617 // Introduce a ternary to capture the conditional return while wrapping the
618 // non-optional result from a lower step into an optional value.
619 return newOptionalCompositionStep(ctx,
620 trueCondition,
621 ctx.NewCall(operators.Conditional,
622 s.condition(),
623 s.expr(),
624 ctx.NewCall("optional.of", step.expr())))
625 }
626 // If the current step is unconditional and the step is non-optional, attempt
627 // to convert to the optional step 's' to a non-optional value using `orValue`
628 // with the 'step' expression value.
629 return newNonOptionalCompositionStep(ctx,
630 trueCondition,
631 ctx.NewMemberCall("orValue", s.expr(), step.expr()),
632 )
633}
634
635func isOptionalNone(e ast.Expr) bool {
636 return e.Kind() == ast.CallKind &&

Callers

nothing calls this directly

Calls 10

isOptionalNoneFunction · 0.85
NewLiteralMethod · 0.65
isOptionalMethod · 0.65
isConditionalMethod · 0.65
NewCallMethod · 0.65
conditionMethod · 0.65
exprMethod · 0.65
NewMemberCallMethod · 0.65

Tested by

no test coverage detected