// Copyright 2016 The Bazel Authors. All rights reserved.
//
// 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.devtools.build.lib.packages;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Ordering;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;
import com.google.devtools.build.lib.syntax.ClassObject;
import com.google.devtools.build.lib.syntax.Environment;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.Printer;
import com.google.devtools.build.lib.syntax.SkylarkType;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
/** An instance (in the Skylark sense, not Java) of a {@link Provider}. */
@SkylarkModule(
name = "struct",
category = SkylarkModuleCategory.BUILTIN,
doc =
"A generic object with fields. See the global struct"
+ "
function for more details."
+ "
Structs fields cannot be reassigned once the struct is created. Two structs are " + "equal if they have the same fields and if corresponding field values are equal." ) public abstract class Info implements ClassObject, SkylarkValue, Serializable { /** The {@link Provider} that describes the type of this instance. */ private final Provider provider; /** * The Skylark location where this provider instance was created. * *
Built-in provider instances may use {@link Location#BUILTIN}. */ @VisibleForSerialization protected final Location location; /** * Constructs an {@link Info}. * * @param provider the provider describing the type of this instance * @param location the Skylark location where this instance is created. If null, defaults to * {@link Location#BUILTIN}. */ protected Info(Provider provider, @Nullable Location location) { this.provider = Preconditions.checkNotNull(provider); this.location = location == null ? Location.BUILTIN : location; } /** * Preprocesses a map of field values to convert the field names and field values to * Skylark-acceptable names and types. * *
This preserves the order of the map entries.
*/
protected static ImmutableMap Builtin provider instances may return {@link Location#BUILTIN}.
*/
public Location getCreationLoc() {
return location;
}
public Provider getProvider() {
return provider;
}
/**
* Returns whether the given field name exists.
*
* This conceptually extends the API for {@link ClassObject}.
*/
public abstract boolean hasField(String name);
/**
* Wraps {@link ClassObject#getValue(String)}, returning null in cases where
* {@link EvalException} would have been thrown.
*/
@VisibleForTesting
public Object getValueOrNull(String name) {
try {
return getValue(name);
} catch (EvalException e) {
return null;
}
}
/**
* Returns the result of {@link #getValue(String)}, cast as the given type, throwing {@link
* EvalException} if the cast fails.
*/
public Overrides {@link ClassObject#getFieldNames()}, but does not allow {@link EvalException} to
* be thrown.
*/
@Override
public abstract ImmutableCollection By default, it is the one specified by the provider.
*/
protected String getErrorMessageFormatForUnknownField() {
return provider.getErrorMessageFormatForUnknownField();
}
@Override
public String getErrorMessageForUnknownField(String name) {
String suffix = "Available attributes: "
+ Joiner.on(", ").join(Ordering.natural().sortedCopy(getFieldNames()));
return String.format(getErrorMessageFormatForUnknownField(), name) + "\n" + suffix;
}
@Override
public boolean equals(Object otherObject) {
if (!(otherObject instanceof Info)) {
return false;
}
Info other = (Info) otherObject;
if (this == other) {
return true;
}
if (!this.provider.equals(other.provider)) {
return false;
}
// Compare objects' fields and their values
if (!this.getFieldNames().equals(other.getFieldNames())) {
return false;
}
for (String field : getFieldNames()) {
if (!Objects.equal(this.getValueOrNull(field), other.getValueOrNull(field))) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
List