Contribute to Starlarky by following the steps below.
The first step to fixing a problem or adding a feature is defining it. Start by creating an issue to describe what you are trying to do and what value it will add. Provide the test scenarios that need to be satisfied for this issue to be solved.
Write some code and open a new PR. In the description, link the PR to the issue you have created, and describe the changes introduced along with the documentation necessary.
Not sure where to start? We recommend using this guide as a starting point.
Once the CI checks are complete, debug any issues that may cause these checks to fail.
When your PR is ready, a member of the VGS Starlarky team will be there to help review and merge your changes!
Before writing any code, please read the Starlarky README to understand the structure of this project.
This guide will only focus on extending Larky, VGS's Starlark additions.
In this section, we want to add a new Larky (example) module ex_module
to an existing Larky namespace stdlib
.
-
Larky modules must belong to a namespace.
-
The Larky module is defined and implemented in the
ex_module.star
file, and stored inlarky/src/main/resources/stdlib/
.load("@stdlib/larky", "larky") def _method1(param1,param2): return param1 + " " + param2 ex_module = larky.struct( method1=_method1, )
-
More complex modules (e.g., that require external dependencies and / or tooling not found in Starlarky) can use Java support.
-
The Java module is implemented in the
ExampleModule.java
file, and stored inlarky/src/main/java/com/verygood/security/larky/modules/
-
Java modules
-
(as of now) live in the
@stdlib
namespace by default. -
are declared using the class annotation
@StarlarkBuiltin
.@StarlarkBuiltin( name = "ex_module", category = "BUILTIN", doc = "Module Description" )
-
implement
StarlarkValue
.public class ExampleModule implements StarlarkValue {
-
have an internal object
INSTANCE
public static final ExampleModule INSTANCE = new ExampleModule();
used by the ModuleSupplier to invoke module methods at runtime, and should be added to the
STD_MODULES
module map.public static final ImmutableSet<StarlarkValue> STD_MODULES = ImmutableSet.of( ExampleModule.INSTANCE, ... )
-
methods are declared using the method annotation
@StarlarkMethod
.@StarlarkMethod( name = "method1", doc = "Method Description", parameters = { @Param( name = "param1", doc = "param1 description", allowedTypes = { @ParamType(type = String.class) }), @Param( name = "param2", doc = "param2 description", named = true, defaultValue = "None", allowedTypes = { @ParamType(type=String.class), }) } ) public String method1(String param1, String param2) { return param1 + " " + param2; }
-
-
The module should be tested using Larky script (.star) tests, stored in
larky/src/test/resources/stdlib_tests/test_ex_module.star
. -
Larky tests in the
stdlib
namespace are run from a Java test file, stored inlarky/src/test/java/com/verygood/security/larky/StdLibTests.java
. -
The example module can be imported and used in a Larky
.star
scripts as follows.load("@stdlib//ex_module", "ex_module") ex_module.method1("param1","param2");
-
Larky supports unit tests and assertions.
load("@vendor//asserts", "asserts") load("@stdlib//unittest", "unittest") load("@stdlib//ex_module", "ex_module") def _test_method1(): result = ex_module.method1("param1","param2") exp_result = "param1 param2" asserts.assert_that(result).is_equal_to(exp_result) def _suite(): _suite = unittest.TestSuite() _suite.addTest(unittest.FunctionTestCase(_test_method1)) return _suite _runner = unittest.TextTestRunner() _runner.run(_suite())
New modules should be reflected in runlarky
.
Follow these instruction
for more information.
In this section, we will add a new Larky namespace ex_namespace
.
-
Create a directory in
larky/src/main/resources/ex_namespace
and place your.star
module files in there. -
Add the namespace name
ex_namespace//
to the ResouceContentStarFilestartsWithPrefix
method. -
Create a directory in
larky/src/test/resources/ex_namespace_tests
and place yourtest_*.star
test files in there. -
Create a Java test file
ExNamespaceLibTests.java
, placed inlarky/src/test/java/com/verygood/security/larky/
, that extracts and executes Larky tests fromlarky/src/test/resources/ex_namespace_tests
and runs them using aLarkyScript
interpreter.Use the stdlib test file as a reference.