|
| 1 | +// Copyright 2023 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 | +// |
| 15 | + |
| 16 | +package com.google.devtools.build.lib.bazel.bzlmod; |
| 17 | + |
| 18 | +import com.google.devtools.build.lib.cmdline.Label; |
| 19 | +import com.google.gson.JsonArray; |
| 20 | +import com.google.gson.JsonElement; |
| 21 | +import com.google.gson.JsonNull; |
| 22 | +import com.google.gson.JsonObject; |
| 23 | +import com.google.gson.JsonParser; |
| 24 | +import com.google.gson.JsonPrimitive; |
| 25 | +import com.google.gson.TypeAdapter; |
| 26 | +import com.google.gson.stream.JsonReader; |
| 27 | +import com.google.gson.stream.JsonWriter; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import net.starlark.java.eval.Dict; |
| 33 | +import net.starlark.java.eval.EvalException; |
| 34 | +import net.starlark.java.eval.Mutability; |
| 35 | +import net.starlark.java.eval.Starlark; |
| 36 | +import net.starlark.java.eval.StarlarkInt; |
| 37 | +import net.starlark.java.eval.StarlarkList; |
| 38 | + |
| 39 | +/** Helps serialize/deserialize {@link AttributeValues}, which contains Starlark values. */ |
| 40 | +public class AttributeValuesAdapter extends TypeAdapter<AttributeValues> { |
| 41 | + |
| 42 | + @Override |
| 43 | + public void write(JsonWriter out, AttributeValues attributeValues) throws IOException { |
| 44 | + JsonObject jsonObject = new JsonObject(); |
| 45 | + for (Map.Entry<String, Object> entry : attributeValues.attributes().entrySet()) { |
| 46 | + jsonObject.add(entry.getKey(), serializeObject(entry.getValue())); |
| 47 | + } |
| 48 | + out.jsonValue(jsonObject.toString()); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public AttributeValues read(JsonReader in) throws IOException { |
| 53 | + JsonObject jsonObject = JsonParser.parseReader(in).getAsJsonObject(); |
| 54 | + Dict.Builder<String, Object> dict = Dict.builder(); |
| 55 | + for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { |
| 56 | + dict.put(entry.getKey(), deserializeObject(entry.getValue())); |
| 57 | + } |
| 58 | + return AttributeValues.create(dict.buildImmutable()); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Starlark Object Types Bool Integer String Label List (Int, label, string) Dict (String,list) & |
| 63 | + * (Label, String) |
| 64 | + */ |
| 65 | + private JsonElement serializeObject(Object obj) { |
| 66 | + if (obj.equals(Starlark.NONE)) { |
| 67 | + return JsonNull.INSTANCE; |
| 68 | + } else if (obj instanceof Boolean) { |
| 69 | + return new JsonPrimitive((Boolean) obj); |
| 70 | + } else if (obj instanceof StarlarkInt) { |
| 71 | + try { |
| 72 | + return new JsonPrimitive(((StarlarkInt) obj).toInt("serialization into the lockfile")); |
| 73 | + } catch (EvalException e) { |
| 74 | + throw new IllegalArgumentException("Unable to parse StarlarkInt to Integer: " + e); |
| 75 | + } |
| 76 | + } else if (obj instanceof String || obj instanceof Label) { |
| 77 | + return new JsonPrimitive(serializeObjToString(obj)); |
| 78 | + } else if (obj instanceof Dict) { |
| 79 | + JsonObject jsonObject = new JsonObject(); |
| 80 | + for (Map.Entry<?, ?> entry : ((Dict<?, ?>) obj).entrySet()) { |
| 81 | + jsonObject.add(serializeObjToString(entry.getKey()), serializeObject(entry.getValue())); |
| 82 | + } |
| 83 | + return jsonObject; |
| 84 | + } else if (obj instanceof StarlarkList) { |
| 85 | + JsonArray jsonArray = new JsonArray(); |
| 86 | + for (Object item : (StarlarkList<?>) obj) { |
| 87 | + jsonArray.add(serializeObject(item)); |
| 88 | + } |
| 89 | + return jsonArray; |
| 90 | + } else { |
| 91 | + throw new IllegalArgumentException("Unsupported type: " + obj.getClass()); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private Object deserializeObject(JsonElement json) { |
| 96 | + if (json == null || json.isJsonNull()) { |
| 97 | + return Starlark.NONE; |
| 98 | + } else if (json.isJsonPrimitive()) { |
| 99 | + JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive(); |
| 100 | + if (jsonPrimitive.isBoolean()) { |
| 101 | + return jsonPrimitive.getAsBoolean(); |
| 102 | + } else if (jsonPrimitive.isNumber()) { |
| 103 | + return StarlarkInt.of(jsonPrimitive.getAsInt()); |
| 104 | + } else if (jsonPrimitive.isString()) { |
| 105 | + return deserializeStringToObject(jsonPrimitive.getAsString()); |
| 106 | + } else { |
| 107 | + throw new IllegalArgumentException("Unsupported JSON primitive: " + jsonPrimitive); |
| 108 | + } |
| 109 | + } else if (json.isJsonObject()) { |
| 110 | + JsonObject jsonObject = json.getAsJsonObject(); |
| 111 | + Dict.Builder<Object, Object> dict = Dict.builder(); |
| 112 | + for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { |
| 113 | + dict.put(deserializeStringToObject(entry.getKey()), deserializeObject(entry.getValue())); |
| 114 | + } |
| 115 | + return dict.buildImmutable(); |
| 116 | + } else if (json.isJsonArray()) { |
| 117 | + JsonArray jsonArray = json.getAsJsonArray(); |
| 118 | + List<Object> list = new ArrayList<>(); |
| 119 | + for (JsonElement item : jsonArray) { |
| 120 | + list.add(deserializeObject(item)); |
| 121 | + } |
| 122 | + return StarlarkList.copyOf(Mutability.IMMUTABLE, list); |
| 123 | + } else { |
| 124 | + throw new IllegalArgumentException("Unsupported JSON element: " + json); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Serializes an object (Label or String) to String A label is converted to a String as it is. A |
| 130 | + * String is being modified with a delimiter to be easily differentiated from the label when |
| 131 | + * deserializing. |
| 132 | + * |
| 133 | + * @param obj String or Label |
| 134 | + * @return serialized object |
| 135 | + */ |
| 136 | + private String serializeObjToString(Object obj) { |
| 137 | + if (obj instanceof Label) { |
| 138 | + return ((Label) obj).getUnambiguousCanonicalForm(); |
| 139 | + } |
| 140 | + return "--" + obj; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Deserializes a string to either a label or a String depending on the prefix. A string will have |
| 145 | + * a delimiter at the start, else it is converted to a label. |
| 146 | + * |
| 147 | + * @param value String to be deserialized |
| 148 | + * @return Object of type String of Label |
| 149 | + */ |
| 150 | + private Object deserializeStringToObject(String value) { |
| 151 | + if (value.startsWith("--")) { |
| 152 | + return value.substring(2); |
| 153 | + } |
| 154 | + return Label.parseCanonicalUnchecked(value); |
| 155 | + } |
| 156 | +} |
0 commit comments