|
41 | 41 | import java.lang.reflect.ParameterizedType; |
42 | 42 | import java.lang.reflect.Type; |
43 | 43 | import java.util.Base64; |
44 | | -import java.util.List; |
45 | 44 | import java.util.Optional; |
46 | 45 | import javax.annotation.Nullable; |
47 | 46 | import net.starlark.java.syntax.Location; |
@@ -82,52 +81,81 @@ public void write(JsonWriter jsonWriter, ModuleKey moduleKey) throws IOException |
82 | 81 | @Override |
83 | 82 | public ModuleKey read(JsonReader jsonReader) throws IOException { |
84 | 83 | String jsonString = jsonReader.nextString(); |
85 | | - if (jsonString.equals("<root>")) { |
86 | | - return ModuleKey.ROOT; |
87 | | - } |
88 | | - List<String> parts = Splitter.on('@').splitToList(jsonString); |
89 | | - if (parts.get(1).equals("_")) { |
90 | | - return ModuleKey.create(parts.get(0), Version.EMPTY); |
91 | | - } |
92 | | - |
93 | | - Version version; |
94 | 84 | try { |
95 | | - version = Version.parse(parts.get(1)); |
| 85 | + return ModuleKey.fromString(jsonString); |
96 | 86 | } catch (ParseException e) { |
97 | 87 | throw new JsonParseException( |
98 | 88 | String.format("Unable to parse ModuleKey %s version from the lockfile", jsonString), |
99 | 89 | e); |
100 | 90 | } |
101 | | - return ModuleKey.create(parts.get(0), version); |
102 | 91 | } |
103 | 92 | }; |
104 | 93 |
|
105 | | - // TODO(salmasamy) need to handle "isolated" in module extensions when it is stable |
106 | 94 | public static final TypeAdapter<ModuleExtensionId> MODULE_EXTENSION_ID_TYPE_ADAPTER = |
107 | 95 | new TypeAdapter<>() { |
108 | 96 | @Override |
109 | 97 | public void write(JsonWriter jsonWriter, ModuleExtensionId moduleExtId) throws IOException { |
110 | | - jsonWriter.value(moduleExtId.getBzlFileLabel() + "%" + moduleExtId.getExtensionName()); |
| 98 | + String isolationKeyPart = moduleExtId.getIsolationKey().map(key -> "%" + key).orElse(""); |
| 99 | + jsonWriter.value( |
| 100 | + moduleExtId.getBzlFileLabel() |
| 101 | + + "%" |
| 102 | + + moduleExtId.getExtensionName() |
| 103 | + + isolationKeyPart); |
111 | 104 | } |
112 | 105 |
|
113 | 106 | @Override |
114 | 107 | public ModuleExtensionId read(JsonReader jsonReader) throws IOException { |
115 | 108 | String jsonString = jsonReader.nextString(); |
116 | | - // [0] is labelString, [1] is extensionName |
117 | | - List<String> extIdParts = Splitter.on("%").splitToList(jsonString); |
| 109 | + var extIdParts = Splitter.on('%').splitToList(jsonString); |
| 110 | + Optional<ModuleExtensionId.IsolationKey> isolationKey; |
| 111 | + if (extIdParts.size() > 2) { |
| 112 | + try { |
| 113 | + isolationKey = |
| 114 | + Optional.of(ModuleExtensionId.IsolationKey.fromString(extIdParts.get(2))); |
| 115 | + } catch (ParseException e) { |
| 116 | + throw new JsonParseException( |
| 117 | + String.format( |
| 118 | + "Unable to parse ModuleExtensionID isolation key: '%s' from the lockfile", |
| 119 | + extIdParts.get(2)), |
| 120 | + e); |
| 121 | + } |
| 122 | + } else { |
| 123 | + isolationKey = Optional.empty(); |
| 124 | + } |
118 | 125 | try { |
119 | 126 | return ModuleExtensionId.create( |
120 | | - Label.parseCanonical(extIdParts.get(0)), extIdParts.get(1), Optional.empty()); |
| 127 | + Label.parseCanonical(extIdParts.get(0)), extIdParts.get(1), isolationKey); |
121 | 128 | } catch (LabelSyntaxException e) { |
122 | 129 | throw new JsonParseException( |
123 | 130 | String.format( |
124 | | - "Unable to parse ModuleExtensionID bzl file label: '%s' from the lockfile", |
| 131 | + "Unable to parse ModuleExtensionID bzl file label: '%s' from the lockfile", |
125 | 132 | extIdParts.get(0)), |
126 | 133 | e); |
127 | 134 | } |
128 | 135 | } |
129 | 136 | }; |
130 | 137 |
|
| 138 | + public static final TypeAdapter<ModuleExtensionId.IsolationKey> ISOLATION_KEY_TYPE_ADAPTER = |
| 139 | + new TypeAdapter<>() { |
| 140 | + @Override |
| 141 | + public void write(JsonWriter jsonWriter, ModuleExtensionId.IsolationKey isolationKey) |
| 142 | + throws IOException { |
| 143 | + jsonWriter.value(isolationKey.toString()); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public ModuleExtensionId.IsolationKey read(JsonReader jsonReader) throws IOException { |
| 148 | + String jsonString = jsonReader.nextString(); |
| 149 | + try { |
| 150 | + return ModuleExtensionId.IsolationKey.fromString(jsonString); |
| 151 | + } catch (ParseException e) { |
| 152 | + throw new JsonParseException( |
| 153 | + String.format("Unable to parse isolation key: '%s' from the lockfile", jsonString), |
| 154 | + e); |
| 155 | + } |
| 156 | + } |
| 157 | + }; |
| 158 | + |
131 | 159 | public static final TypeAdapter<byte[]> BYTE_ARRAY_TYPE_ADAPTER = |
132 | 160 | new TypeAdapter<>() { |
133 | 161 | @Override |
@@ -283,6 +311,7 @@ public static Gson createLockFileGson(Path moduleFilePath) { |
283 | 311 | .registerTypeAdapter(Version.class, VERSION_TYPE_ADAPTER) |
284 | 312 | .registerTypeAdapter(ModuleKey.class, MODULE_KEY_TYPE_ADAPTER) |
285 | 313 | .registerTypeAdapter(ModuleExtensionId.class, MODULE_EXTENSION_ID_TYPE_ADAPTER) |
| 314 | + .registerTypeAdapter(ModuleExtensionId.IsolationKey.class, ISOLATION_KEY_TYPE_ADAPTER) |
286 | 315 | .registerTypeAdapter(AttributeValues.class, new AttributeValuesAdapter()) |
287 | 316 | .registerTypeAdapter(byte[].class, BYTE_ARRAY_TYPE_ADAPTER) |
288 | 317 | .create(); |
|
0 commit comments