Search in sources :

Example 1 with SkylarkValue

use of com.google.devtools.build.lib.skylarkinterface.SkylarkValue in project bazel by bazelbuild.

the class PackageFactory method skylarkifyValue.

/**
   * Converts back to type that will work in BUILD and skylark,
   * such as string instead of label, SkylarkList instead of List,
   * Returns null if we don't want to export the value.
   *
   * <p>All of the types returned are immutable. If we want, we can change this to
   * immutable in the future, but this is the safe choice for now.
   */
@Nullable
private static Object skylarkifyValue(Object val, Package pkg) throws NotRepresentableException {
    // from Java native types to Skylark types should be part of the Type class hierarchy,
    if (val == null) {
        return null;
    }
    if (val instanceof Boolean) {
        return val;
    }
    if (val instanceof Integer) {
        return val;
    }
    if (val instanceof String) {
        return val;
    }
    if (val instanceof TriState) {
        switch((TriState) val) {
            case AUTO:
                return Integer.valueOf(-1);
            case YES:
                return Integer.valueOf(1);
            case NO:
                return Integer.valueOf(0);
        }
    }
    if (val instanceof Label) {
        Label l = (Label) val;
        if (l.getPackageName().equals(pkg.getName())) {
            return ":" + l.getName();
        }
        return l.getCanonicalForm();
    }
    if (val instanceof List) {
        List<Object> l = new ArrayList<>();
        for (Object o : (List) val) {
            Object elt = skylarkifyValue(o, pkg);
            if (elt == null) {
                continue;
            }
            l.add(elt);
        }
        return SkylarkList.Tuple.copyOf(l);
    }
    if (val instanceof Map) {
        Map<Object, Object> m = new TreeMap<>();
        for (Map.Entry<?, ?> e : ((Map<?, ?>) val).entrySet()) {
            Object key = skylarkifyValue(e.getKey(), pkg);
            Object mapVal = skylarkifyValue(e.getValue(), pkg);
            if (key == null || mapVal == null) {
                continue;
            }
            m.put(key, mapVal);
        }
        return m;
    }
    if (val.getClass().isAnonymousClass()) {
        // Filter them until we invent something more clever.
        return null;
    }
    if (val instanceof SkylarkValue) {
        return val;
    }
    if (val instanceof License) {
        // TODO(bazel-team): convert License.getLicenseTypes() to a list of strings.
        return null;
    }
    if (val instanceof BuildType.SelectorList) {
        return val;
    }
    // if we add more types that we can represent.
    throw new NotRepresentableException(String.format("cannot represent %s (%s) in skylark", val.toString(), val.getClass().toString()));
}
Also used : Label(com.google.devtools.build.lib.cmdline.Label) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) GlobList(com.google.devtools.build.lib.syntax.GlobList) MutableList(com.google.devtools.build.lib.syntax.SkylarkList.MutableList) List(java.util.List) SkylarkList(com.google.devtools.build.lib.syntax.SkylarkList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ClassObject(com.google.devtools.build.lib.syntax.ClassObject) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BuildLangTypedAttributeValuesMap(com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap) TreeMap(java.util.TreeMap) SkylarkValue(com.google.devtools.build.lib.skylarkinterface.SkylarkValue) Nullable(javax.annotation.Nullable)

Example 2 with SkylarkValue

use of com.google.devtools.build.lib.skylarkinterface.SkylarkValue in project bazel by bazelbuild.

the class SkylarkRuleConfiguredTargetBuilder method addOutputGroups.

private static void addOutputGroups(Object value, Location loc, RuleConfiguredTargetBuilder builder) throws EvalException {
    Map<String, SkylarkValue> outputGroups = SkylarkType.castMap(value, String.class, SkylarkValue.class, "output_groups");
    for (String outputGroup : outputGroups.keySet()) {
        SkylarkValue objects = outputGroups.get(outputGroup);
        NestedSet<Artifact> artifacts = convertToOutputGroupValue(loc, outputGroup, objects);
        builder.addOutputGroup(outputGroup, artifacts);
    }
}
Also used : Artifact(com.google.devtools.build.lib.actions.Artifact) SkylarkValue(com.google.devtools.build.lib.skylarkinterface.SkylarkValue)

Example 3 with SkylarkValue

use of com.google.devtools.build.lib.skylarkinterface.SkylarkValue in project bazel by bazelbuild.

the class SkylarkAspectFactory method addOutputGroups.

private static void addOutputGroups(Object value, Location loc, ConfiguredAspect.Builder builder) throws EvalException {
    Map<String, SkylarkValue> outputGroups = SkylarkType.castMap(value, String.class, SkylarkValue.class, "output_groups");
    for (String outputGroup : outputGroups.keySet()) {
        SkylarkValue objects = outputGroups.get(outputGroup);
        builder.addOutputGroup(outputGroup, SkylarkRuleConfiguredTargetBuilder.convertToOutputGroupValue(loc, outputGroup, objects));
    }
}
Also used : SkylarkValue(com.google.devtools.build.lib.skylarkinterface.SkylarkValue)

Aggregations

SkylarkValue (com.google.devtools.build.lib.skylarkinterface.SkylarkValue)3 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 Label (com.google.devtools.build.lib.cmdline.Label)1 BuildLangTypedAttributeValuesMap (com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap)1 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)1 GlobList (com.google.devtools.build.lib.syntax.GlobList)1 SkylarkList (com.google.devtools.build.lib.syntax.SkylarkList)1 MutableList (com.google.devtools.build.lib.syntax.SkylarkList.MutableList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Nullable (javax.annotation.Nullable)1