blob: 5b97b89c8e04c7517273f4cf67b35046b75a0d4b [file] [log] [blame]
cparsons4ebf6c02018-08-17 14:49:36 -07001// Copyright 2016 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.packages;
15
cparsons4ebf6c02018-08-17 14:49:36 -070016import com.google.common.base.Joiner;
17import com.google.common.base.Objects;
cparsons4ebf6c02018-08-17 14:49:36 -070018import com.google.common.collect.Ordering;
gregcecf3a9232020-07-20 15:17:52 -070019import com.google.devtools.build.lib.starlarkbuildapi.core.StructApi;
cparsons4ebf6c02018-08-17 14:49:36 -070020import java.util.ArrayList;
21import java.util.Collections;
22import java.util.List;
cparsons4ebf6c02018-08-17 14:49:36 -070023import javax.annotation.Nullable;
adonovan450c7ad2020-09-14 13:00:21 -070024import net.starlark.java.eval.EvalException;
25import net.starlark.java.eval.Printer;
adonovan450c7ad2020-09-14 13:00:21 -070026import net.starlark.java.eval.Starlark;
adonovan267bdaa2020-11-04 11:32:24 -080027import net.starlark.java.eval.Structure;
cparsons4ebf6c02018-08-17 14:49:36 -070028
adonovan11e5fab2019-12-06 12:03:49 -080029/**
adonovan7202d102019-12-09 13:58:57 -080030 * An abstract base class for Starlark values that have fields, have to_json and to_proto methods,
31 * have an associated provider (type symbol), and may be returned as the result of analysis from one
32 * target to another.
33 *
34 * <p>StructImpl does not specify how the fields are represented; subclasses must define {@code
35 * getValue} and {@code getFieldNames}. For example, {@code NativeInfo} supplies fields from the
gregceb100b1d2020-05-20 10:22:17 -070036 * subclass's {@code StarlarkMethod(structField=true)} annotations, and {@code StarlarkInfo}
37 * supplies fields from the map provided at its construction.
adonovan11e5fab2019-12-06 12:03:49 -080038 *
39 * <p>Two StructImpls are equivalent if they have the same provider and, for each field name
40 * reported by {@code getFieldNames} their corresponding field values are equivalent, or accessing
41 * them both returns an error.
42 */
adonovan267bdaa2020-11-04 11:32:24 -080043public abstract class StructImpl implements Info, Structure, StructApi {
adonovana11e2d02019-12-06 07:11:35 -080044
cparsons4ebf6c02018-08-17 14:49:36 -070045 /**
cparsons4ebf6c02018-08-17 14:49:36 -070046 * Returns the result of {@link #getValue(String)}, cast as the given type, throwing {@link
47 * EvalException} if the cast fails.
48 */
Googler8d927b82024-04-15 03:40:47 -070049 @Nullable
adonovan7202d102019-12-09 13:58:57 -080050 public final <T> T getValue(String key, Class<T> type) throws EvalException {
cparsons4ebf6c02018-08-17 14:49:36 -070051 Object obj = getValue(key);
52 if (obj == null) {
53 return null;
54 }
adonovan85803902020-04-16 14:46:57 -070055 try {
56 return type.cast(obj);
adonovandd000462020-11-06 12:57:38 -080057 } catch (ClassCastException unused) {
adonovan85803902020-04-16 14:46:57 -070058 throw Starlark.errorf(
adonovan8bee7072020-04-29 11:59:53 -070059 "for %s field, got %s, want %s", key, Starlark.type(obj), Starlark.classType(type));
adonovan85803902020-04-16 14:46:57 -070060 }
cparsons4ebf6c02018-08-17 14:49:36 -070061 }
62
63 /**
Googler8d927b82024-04-15 03:40:47 -070064 * Returns the result of {@link #getValue(String)}, cast as the given type, throwing {@link
65 * EvalException} if the cast fails. If the value is {@link Starlark#NONE}, returns null.
66 */
67 @Nullable
68 public final <T> T getNoneableValue(String key, Class<T> type) throws EvalException {
69 Object obj = getValue(key);
70 if (obj == null || obj == Starlark.NONE) {
71 return null;
72 }
73 try {
74 return type.cast(obj);
75 } catch (ClassCastException unused) {
76 throw Starlark.errorf(
77 "for %s field, got %s, want %s", key, Starlark.type(obj), Starlark.classType(type));
78 }
79 }
80
81 /**
cparsons4ebf6c02018-08-17 14:49:36 -070082 * Returns the error message format to use for unknown fields.
83 *
84 * <p>By default, it is the one specified by the provider.
85 */
cparsons4ebf6c02018-08-17 14:49:36 -070086 @Override
87 public String getErrorMessageForUnknownField(String name) {
adonovanb58650a2020-11-10 10:27:37 -080088 return getProvider().getErrorMessageForUnknownField(name) + allAttributesSuffix();
89 }
90
91 final String allAttributesSuffix() {
92 // TODO(adonovan): when is it appropriate for the error to show all attributes,
93 // and when to show a single spelling suggestion (the default)?
94 return "\nAvailable attributes: "
cparsons4ebf6c02018-08-17 14:49:36 -070095 + Joiner.on(", ").join(Ordering.natural().sortedCopy(getFieldNames()));
cparsons4ebf6c02018-08-17 14:49:36 -070096 }
97
98 @Override
99 public boolean equals(Object otherObject) {
100 if (!(otherObject instanceof StructImpl)) {
101 return false;
102 }
103 StructImpl other = (StructImpl) otherObject;
104 if (this == other) {
105 return true;
106 }
adonovana11e2d02019-12-06 07:11:35 -0800107 if (!this.getProvider().equals(other.getProvider())) {
cparsons4ebf6c02018-08-17 14:49:36 -0700108 return false;
109 }
110 // Compare objects' fields and their values
111 if (!this.getFieldNames().equals(other.getFieldNames())) {
112 return false;
113 }
114 for (String field : getFieldNames()) {
115 if (!Objects.equal(this.getValueOrNull(field), other.getValueOrNull(field))) {
116 return false;
117 }
118 }
119 return true;
120 }
121
122 @Override
123 public int hashCode() {
124 List<String> fields = new ArrayList<>(getFieldNames());
125 Collections.sort(fields);
126 List<Object> objectsToHash = new ArrayList<>();
adonovana11e2d02019-12-06 07:11:35 -0800127 objectsToHash.add(getProvider());
cparsons4ebf6c02018-08-17 14:49:36 -0700128 for (String field : fields) {
129 objectsToHash.add(field);
130 objectsToHash.add(getValueOrNull(field));
131 }
132 return Objects.hashCode(objectsToHash.toArray());
133 }
134
135 /**
gregce3377c112020-04-13 09:29:59 -0700136 * Convert the object to string using Starlark syntax. The output tries to be reversible (but
137 * there is no guarantee, it depends on the actual values).
cparsons4ebf6c02018-08-17 14:49:36 -0700138 */
139 @Override
Googler34f70582019-11-25 12:27:34 -0800140 public void repr(Printer printer) {
cparsons4ebf6c02018-08-17 14:49:36 -0700141 boolean first = true;
142 printer.append("struct(");
143 // Sort by key to ensure deterministic output.
144 for (String fieldName : Ordering.natural().sortedCopy(getFieldNames())) {
145 if (!first) {
146 printer.append(", ");
147 }
148 first = false;
149 printer.append(fieldName);
150 printer.append(" = ");
151 printer.repr(getValueOrNull(fieldName));
152 }
153 printer.append(")");
154 }
155
Googler3d50c6a2022-07-06 03:50:04 -0700156 @Nullable
adonovane71be212019-12-06 10:07:32 -0800157 private Object getValueOrNull(String name) {
158 try {
159 return getValue(name);
160 } catch (EvalException e) {
161 return null;
162 }
163 }
164
cparsons4ebf6c02018-08-17 14:49:36 -0700165 @Override
cparsons4ebf6c02018-08-17 14:49:36 -0700166 public String toString() {
Googlerb1e232d2019-11-22 15:29:43 -0800167 return Starlark.repr(this);
cparsons4ebf6c02018-08-17 14:49:36 -0700168 }
169}