-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a
Boolean()
builtin to explicitly cast anything to a boolean.
PiperOrigin-RevId: 576240041
- Loading branch information
1 parent
31854b0
commit fe76537
Showing
7 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
java/src/com/google/template/soy/basicfunctions/BooleanFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2009 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.template.soy.basicfunctions; | ||
|
||
import com.google.template.soy.data.SoyValue; | ||
import com.google.template.soy.plugin.java.restricted.JavaPluginContext; | ||
import com.google.template.soy.plugin.java.restricted.JavaValue; | ||
import com.google.template.soy.plugin.java.restricted.JavaValueFactory; | ||
import com.google.template.soy.plugin.java.restricted.SoyJavaSourceFunction; | ||
import com.google.template.soy.plugin.javascript.restricted.JavaScriptPluginContext; | ||
import com.google.template.soy.plugin.javascript.restricted.JavaScriptValue; | ||
import com.google.template.soy.plugin.javascript.restricted.JavaScriptValueFactory; | ||
import com.google.template.soy.plugin.javascript.restricted.SoyJavaScriptSourceFunction; | ||
import com.google.template.soy.plugin.python.restricted.PythonPluginContext; | ||
import com.google.template.soy.plugin.python.restricted.PythonValue; | ||
import com.google.template.soy.plugin.python.restricted.PythonValueFactory; | ||
import com.google.template.soy.plugin.python.restricted.SoyPythonSourceFunction; | ||
import com.google.template.soy.shared.restricted.Signature; | ||
import com.google.template.soy.shared.restricted.SoyFunctionSignature; | ||
import com.google.template.soy.shared.restricted.SoyPureFunction; | ||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
|
||
/** Soy function that takes the ceiling of a number. */ | ||
@SoyPureFunction | ||
@SoyFunctionSignature( | ||
name = "Boolean", | ||
value = | ||
@Signature( | ||
parameterTypes = {"?"}, | ||
returnType = "bool")) | ||
final class BooleanFunction | ||
implements SoyJavaSourceFunction, SoyJavaScriptSourceFunction, SoyPythonSourceFunction { | ||
|
||
@Override | ||
public JavaScriptValue applyForJavaScriptSource( | ||
JavaScriptValueFactory factory, List<JavaScriptValue> args, JavaScriptPluginContext context) { | ||
return factory.global("Boolean").invoke(args.get(0)); | ||
} | ||
|
||
@Override | ||
public PythonValue applyForPythonSource( | ||
PythonValueFactory factory, List<PythonValue> args, PythonPluginContext context) { | ||
return factory.global("bool").call(args.get(0)); | ||
} | ||
|
||
// lazy singleton pattern, allows other backends to avoid the work. | ||
private static final class Methods { | ||
static final Method BOOLEAN_FN = | ||
JavaValueFactory.createMethod(BasicFunctionsRuntime.class, "booleanFunc", SoyValue.class); | ||
} | ||
|
||
@Override | ||
public JavaValue applyForJavaSource( | ||
JavaValueFactory factory, List<JavaValue> args, JavaPluginContext context) { | ||
return factory.callStaticMethod(Methods.BOOLEAN_FN, args.get(0)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters