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()));
}
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);
}
}
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));
}
}
Aggregations