cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 1 | // 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. |
| 14 | package com.google.devtools.build.lib.packages; |
| 15 | |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 16 | import com.google.common.base.Joiner; |
| 17 | import com.google.common.base.Objects; |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Ordering; |
gregce | cf3a923 | 2020-07-20 15:17:52 -0700 | [diff] [blame] | 19 | import com.google.devtools.build.lib.starlarkbuildapi.core.StructApi; |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 20 | import java.util.ArrayList; |
| 21 | import java.util.Collections; |
| 22 | import java.util.List; |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 23 | import javax.annotation.Nullable; |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 24 | import net.starlark.java.eval.EvalException; |
| 25 | import net.starlark.java.eval.Printer; |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 26 | import net.starlark.java.eval.Starlark; |
adonovan | 267bdaa | 2020-11-04 11:32:24 -0800 | [diff] [blame] | 27 | import net.starlark.java.eval.Structure; |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 28 | |
adonovan | 11e5fab | 2019-12-06 12:03:49 -0800 | [diff] [blame] | 29 | /** |
adonovan | 7202d10 | 2019-12-09 13:58:57 -0800 | [diff] [blame] | 30 | * 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 |
gregce | b100b1d | 2020-05-20 10:22:17 -0700 | [diff] [blame] | 36 | * subclass's {@code StarlarkMethod(structField=true)} annotations, and {@code StarlarkInfo} |
| 37 | * supplies fields from the map provided at its construction. |
adonovan | 11e5fab | 2019-12-06 12:03:49 -0800 | [diff] [blame] | 38 | * |
| 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 | */ |
adonovan | 267bdaa | 2020-11-04 11:32:24 -0800 | [diff] [blame] | 43 | public abstract class StructImpl implements Info, Structure, StructApi { |
adonovan | a11e2d0 | 2019-12-06 07:11:35 -0800 | [diff] [blame] | 44 | |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 45 | /** |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 46 | * Returns the result of {@link #getValue(String)}, cast as the given type, throwing {@link |
| 47 | * EvalException} if the cast fails. |
| 48 | */ |
Googler | 8d927b8 | 2024-04-15 03:40:47 -0700 | [diff] [blame] | 49 | @Nullable |
adonovan | 7202d10 | 2019-12-09 13:58:57 -0800 | [diff] [blame] | 50 | public final <T> T getValue(String key, Class<T> type) throws EvalException { |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 51 | Object obj = getValue(key); |
| 52 | if (obj == null) { |
| 53 | return null; |
| 54 | } |
adonovan | 8580390 | 2020-04-16 14:46:57 -0700 | [diff] [blame] | 55 | try { |
| 56 | return type.cast(obj); |
adonovan | dd00046 | 2020-11-06 12:57:38 -0800 | [diff] [blame] | 57 | } catch (ClassCastException unused) { |
adonovan | 8580390 | 2020-04-16 14:46:57 -0700 | [diff] [blame] | 58 | throw Starlark.errorf( |
adonovan | 8bee707 | 2020-04-29 11:59:53 -0700 | [diff] [blame] | 59 | "for %s field, got %s, want %s", key, Starlark.type(obj), Starlark.classType(type)); |
adonovan | 8580390 | 2020-04-16 14:46:57 -0700 | [diff] [blame] | 60 | } |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
Googler | 8d927b8 | 2024-04-15 03:40:47 -0700 | [diff] [blame] | 64 | * 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 | /** |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 82 | * Returns the error message format to use for unknown fields. |
| 83 | * |
| 84 | * <p>By default, it is the one specified by the provider. |
| 85 | */ |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 86 | @Override |
| 87 | public String getErrorMessageForUnknownField(String name) { |
adonovan | b58650a | 2020-11-10 10:27:37 -0800 | [diff] [blame] | 88 | 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: " |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 95 | + Joiner.on(", ").join(Ordering.natural().sortedCopy(getFieldNames())); |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 96 | } |
| 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 | } |
adonovan | a11e2d0 | 2019-12-06 07:11:35 -0800 | [diff] [blame] | 107 | if (!this.getProvider().equals(other.getProvider())) { |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 108 | 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<>(); |
adonovan | a11e2d0 | 2019-12-06 07:11:35 -0800 | [diff] [blame] | 127 | objectsToHash.add(getProvider()); |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 128 | for (String field : fields) { |
| 129 | objectsToHash.add(field); |
| 130 | objectsToHash.add(getValueOrNull(field)); |
| 131 | } |
| 132 | return Objects.hashCode(objectsToHash.toArray()); |
| 133 | } |
| 134 | |
| 135 | /** |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 136 | * 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). |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 138 | */ |
| 139 | @Override |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 140 | public void repr(Printer printer) { |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 141 | 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 | |
Googler | 3d50c6a | 2022-07-06 03:50:04 -0700 | [diff] [blame] | 156 | @Nullable |
adonovan | e71be21 | 2019-12-06 10:07:32 -0800 | [diff] [blame] | 157 | private Object getValueOrNull(String name) { |
| 158 | try { |
| 159 | return getValue(name); |
| 160 | } catch (EvalException e) { |
| 161 | return null; |
| 162 | } |
| 163 | } |
| 164 | |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 165 | @Override |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 166 | public String toString() { |
Googler | b1e232d | 2019-11-22 15:29:43 -0800 | [diff] [blame] | 167 | return Starlark.repr(this); |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 168 | } |
| 169 | } |