private Expression coerceTypeForSwitchComparison ( ExprRootNode expr ) { Expression switchOn = translateExpr ( expr ) ; SoyType type = expr . getType ( ) ; // If the type is possibly a sanitized content type then we need to toString it. if ( SoyTypes . makeNullable ( StringType . getInstance ( ) ) . isAssignableFrom ( type ) || type . equals ( AnyType . getInstance ( ) ) || type . equals ( UnknownType . getInstance ( ) ) ) { CodeChunk . Generator codeGenerator = templateTranslationContext . codeGenerator ( ) ; Expression tmp = codeGenerator . declarationBuilder ( ) . setRhs ( switchOn ) . build ( ) . ref ( ) ; return Expression . ifExpression ( GOOG_IS_OBJECT . call ( tmp ) , tmp . dotAccess ( "toString" ) . call ( ) ) . setElse ( tmp ) . build ( codeGenerator ) ; } // For everything else just pass through. switching on objects/collections is unlikely to // have reasonably defined behavior. return switchOn ; }
to strings .