diff options
author | Francois-Rene Rideau <tunes@google.com> | 2015-10-29 14:26:59 +0000 |
---|---|---|
committer | Lukacs Berki <lberki@google.com> | 2015-10-30 09:45:20 +0000 |
commit | 028e319d2c8055e771cc5797abcc52a58a3f6b37 (patch) | |
tree | d6a68abc04878c217764eda030bb136fac754ae0 /src | |
parent | bc042c6ce45438170cb5d421fbe3c5d33279a2fd (diff) |
Add Skylark support for string_list_dict
Convert to Skylark values when destructuring a sequence or map.
--
MOS_MIGRATED_REVID=106591523
Diffstat (limited to 'src')
4 files changed, 53 insertions, 10 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/SkylarkAttr.java b/src/main/java/com/google/devtools/build/lib/rules/SkylarkAttr.java index b633ea6422..5d8775378e 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/SkylarkAttr.java +++ b/src/main/java/com/google/devtools/build/lib/rules/SkylarkAttr.java @@ -708,6 +708,42 @@ public final class SkylarkAttr { }; @SkylarkSignature( + name = "string_list_dict", + doc = + "Creates an attribute of type dictionary, mapping from string to list of string. " + + "Its default value is dict().", + objectType = SkylarkAttr.class, + returnType = Attribute.Builder.class, + optionalNamedOnly = { + @Param(name = DEFAULT_ARG, type = Map.class, defaultValue = "{}", doc = DEFAULT_DOC), + @Param(name = MANDATORY_ARG, type = Boolean.class, defaultValue = "False", doc = MANDATORY_DOC + ), + @Param(name = NON_EMPTY_ARG, type = Boolean.class, defaultValue = "False", doc = NON_EMPTY_DOC + ) + }, + useAst = true, + useEnvironment = true + ) + private static BuiltinFunction stringListDict = + new BuiltinFunction("string_list_dict") { + public Attribute.Builder<?> invoke( + Map<?, ?> defaultO, + Boolean mandatory, + Boolean nonEmpty, + FuncallExpression ast, + Environment env) + throws EvalException { + env.checkLoadingPhase("attr.string_list_dict", ast.getLocation()); + return createAttribute( + EvalUtils.optionMap( + DEFAULT_ARG, defaultO, MANDATORY_ARG, mandatory, NON_EMPTY_ARG, nonEmpty), + Type.STRING_LIST_DICT, + ast, + env); + } + }; + + @SkylarkSignature( name = "license", doc = "Creates an attribute of type license. Its default value is NO_LICENSE.", // TODO(bazel-team): Implement proper license support for Skylark. diff --git a/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java b/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java index e14fbad18a..3cc778984b 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java @@ -888,14 +888,14 @@ public class MethodLibrary { mandatoryPositionals = { @Param(name = "self", type = Map.class, doc = "This object."), @Param(name = "key", type = Object.class, doc = "The index or key to access.")}, - useLocation = true) + useLocation = true, useEnvironment = true) private static BuiltinFunction dictIndexOperator = new BuiltinFunction("$index") { public Object invoke(Map<?, ?> self, Object key, - Location loc) throws EvalException, ConversionException { + Location loc, Environment env) throws EvalException, ConversionException { if (!self.containsKey(key)) { throw new EvalException(loc, Printer.format("Key %r not found in dictionary", key)); } - return self.get(key); + return SkylarkType.convertToSkylark(self.get(key), env); } }; @@ -905,15 +905,15 @@ public class MethodLibrary { mandatoryPositionals = { @Param(name = "self", type = MutableList.class, doc = "This list."), @Param(name = "key", type = Object.class, doc = "The index or key to access.")}, - useLocation = true) + useLocation = true, useEnvironment = true) private static BuiltinFunction listIndexOperator = new BuiltinFunction("$index") { public Object invoke(MutableList self, Object key, - Location loc) throws EvalException, ConversionException { + Location loc, Environment env) throws EvalException, ConversionException { if (self.isEmpty()) { throw new EvalException(loc, "List is empty"); } int index = getListIndex(key, self.size(), loc); - return self.getList().get(index); + return SkylarkType.convertToSkylark(self.getList().get(index), env); } }; @@ -923,15 +923,15 @@ public class MethodLibrary { mandatoryPositionals = { @Param(name = "self", type = Tuple.class, doc = "This tuple."), @Param(name = "key", type = Object.class, doc = "The index or key to access.")}, - useLocation = true) + useLocation = true, useEnvironment = true) private static BuiltinFunction tupleIndexOperator = new BuiltinFunction("$index") { public Object invoke(Tuple self, Object key, - Location loc) throws EvalException, ConversionException { + Location loc, Environment env) throws EvalException, ConversionException { if (self.isEmpty()) { throw new EvalException(loc, "tuple is empty"); } int index = getListIndex(key, self.size(), loc); - return self.getList().get(index); + return SkylarkType.convertToSkylark(self.getList().get(index), env); } }; diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java index 67b7db60f5..74a3eae6ab 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java @@ -227,7 +227,7 @@ public abstract class SkylarkList implements Iterable<Object>, SkylarkValue { * @param contents the contents of the list * @return a Skylark list containing the specified arguments as elements. */ - public static MutableList of(Environment env, Object... contents) { + public static MutableList of(@Nullable Environment env, Object... contents) { return new MutableList(ImmutableList.copyOf(contents), env); } diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java index 1c57974cbb..23458c7c4b 100644 --- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java +++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java @@ -132,6 +132,13 @@ public class SkylarkRuleClassFunctionsTest extends SkylarkTestCase { } @Test + public void testStringListDictAttr() throws Exception { + Object result = evalRuleClassCode("attr.string_list_dict(default = {'a': ['b', 'c']})"); + Attribute attr = ((Attribute.Builder<?>) result).build("a1"); + assertEquals(Type.STRING_LIST_DICT, attr.getType()); + } + + @Test public void testAttrAllowedFileTypesAnyFile() throws Exception { Object result = evalRuleClassCode("attr.label_list(allow_files = True)"); Attribute attr = ((Attribute.Builder<?>) result).build("a1"); |