Skip to content

Commit 8d7e680

Browse files
authored
Merge pull request #3247 from square/bquenaudon.2025-01-06.weakimports
Added support for weak imports in ProtoParser and ProtoFileElement
2 parents b861ecd + fc7b1ab commit 8d7e680

File tree

5 files changed

+45
-22
lines changed

5 files changed

+45
-22
lines changed

wire-schema/api/wire-schema.api

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -572,18 +572,19 @@ public abstract interface class com/squareup/wire/schema/ProfileLoader {
572572

573573
public final class com/squareup/wire/schema/ProtoFile {
574574
public static final field Companion Lcom/squareup/wire/schema/ProtoFile$Companion;
575-
public fun <init> (Lcom/squareup/wire/schema/Location;Ljava/util/List;Ljava/util/List;Ljava/lang/String;Ljava/util/List;Ljava/util/List;Ljava/util/List;Lcom/squareup/wire/schema/Options;Lcom/squareup/wire/Syntax;)V
575+
public fun <init> (Lcom/squareup/wire/schema/Location;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/lang/String;Ljava/util/List;Ljava/util/List;Ljava/util/List;Lcom/squareup/wire/schema/Options;Lcom/squareup/wire/Syntax;)V
576576
public final fun component1 ()Lcom/squareup/wire/schema/Location;
577+
public final fun component10 ()Lcom/squareup/wire/Syntax;
577578
public final fun component2 ()Ljava/util/List;
578579
public final fun component3 ()Ljava/util/List;
579-
public final fun component4 ()Ljava/lang/String;
580-
public final fun component5 ()Ljava/util/List;
580+
public final fun component4 ()Ljava/util/List;
581+
public final fun component5 ()Ljava/lang/String;
581582
public final fun component6 ()Ljava/util/List;
582583
public final fun component7 ()Ljava/util/List;
583-
public final fun component8 ()Lcom/squareup/wire/schema/Options;
584-
public final fun component9 ()Lcom/squareup/wire/Syntax;
585-
public final fun copy (Lcom/squareup/wire/schema/Location;Ljava/util/List;Ljava/util/List;Ljava/lang/String;Ljava/util/List;Ljava/util/List;Ljava/util/List;Lcom/squareup/wire/schema/Options;Lcom/squareup/wire/Syntax;)Lcom/squareup/wire/schema/ProtoFile;
586-
public static synthetic fun copy$default (Lcom/squareup/wire/schema/ProtoFile;Lcom/squareup/wire/schema/Location;Ljava/util/List;Ljava/util/List;Ljava/lang/String;Ljava/util/List;Ljava/util/List;Ljava/util/List;Lcom/squareup/wire/schema/Options;Lcom/squareup/wire/Syntax;ILjava/lang/Object;)Lcom/squareup/wire/schema/ProtoFile;
584+
public final fun component8 ()Ljava/util/List;
585+
public final fun component9 ()Lcom/squareup/wire/schema/Options;
586+
public final fun copy (Lcom/squareup/wire/schema/Location;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/lang/String;Ljava/util/List;Ljava/util/List;Ljava/util/List;Lcom/squareup/wire/schema/Options;Lcom/squareup/wire/Syntax;)Lcom/squareup/wire/schema/ProtoFile;
587+
public static synthetic fun copy$default (Lcom/squareup/wire/schema/ProtoFile;Lcom/squareup/wire/schema/Location;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/lang/String;Ljava/util/List;Ljava/util/List;Ljava/util/List;Lcom/squareup/wire/schema/Options;Lcom/squareup/wire/Syntax;ILjava/lang/Object;)Lcom/squareup/wire/schema/ProtoFile;
587588
public fun equals (Ljava/lang/Object;)Z
588589
public final fun getExtendList ()Ljava/util/List;
589590
public final fun getImports ()Ljava/util/List;
@@ -594,6 +595,7 @@ public final class com/squareup/wire/schema/ProtoFile {
594595
public final fun getServices ()Ljava/util/List;
595596
public final fun getSyntax ()Lcom/squareup/wire/Syntax;
596597
public final fun getTypes ()Ljava/util/List;
598+
public final fun getWeakImports ()Ljava/util/List;
597599
public fun hashCode ()I
598600
public final fun javaPackage ()Ljava/lang/String;
599601
public final fun linkOptions (Lcom/squareup/wire/schema/Linker;Z)V
@@ -1375,9 +1377,10 @@ public final class com/squareup/wire/schema/internal/parser/OptionReader {
13751377

13761378
public final class com/squareup/wire/schema/internal/parser/ProtoFileElement {
13771379
public static final field Companion Lcom/squareup/wire/schema/internal/parser/ProtoFileElement$Companion;
1378-
public fun <init> (Lcom/squareup/wire/schema/Location;Ljava/lang/String;Lcom/squareup/wire/Syntax;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;)V
1379-
public synthetic fun <init> (Lcom/squareup/wire/schema/Location;Ljava/lang/String;Lcom/squareup/wire/Syntax;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
1380+
public fun <init> (Lcom/squareup/wire/schema/Location;Ljava/lang/String;Lcom/squareup/wire/Syntax;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;)V
1381+
public synthetic fun <init> (Lcom/squareup/wire/schema/Location;Ljava/lang/String;Lcom/squareup/wire/Syntax;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
13801382
public final fun component1 ()Lcom/squareup/wire/schema/Location;
1383+
public final fun component10 ()Ljava/util/List;
13811384
public final fun component2 ()Ljava/lang/String;
13821385
public final fun component3 ()Lcom/squareup/wire/Syntax;
13831386
public final fun component4 ()Ljava/util/List;
@@ -1386,8 +1389,8 @@ public final class com/squareup/wire/schema/internal/parser/ProtoFileElement {
13861389
public final fun component7 ()Ljava/util/List;
13871390
public final fun component8 ()Ljava/util/List;
13881391
public final fun component9 ()Ljava/util/List;
1389-
public final fun copy (Lcom/squareup/wire/schema/Location;Ljava/lang/String;Lcom/squareup/wire/Syntax;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;)Lcom/squareup/wire/schema/internal/parser/ProtoFileElement;
1390-
public static synthetic fun copy$default (Lcom/squareup/wire/schema/internal/parser/ProtoFileElement;Lcom/squareup/wire/schema/Location;Ljava/lang/String;Lcom/squareup/wire/Syntax;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;ILjava/lang/Object;)Lcom/squareup/wire/schema/internal/parser/ProtoFileElement;
1392+
public final fun copy (Lcom/squareup/wire/schema/Location;Ljava/lang/String;Lcom/squareup/wire/Syntax;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;)Lcom/squareup/wire/schema/internal/parser/ProtoFileElement;
1393+
public static synthetic fun copy$default (Lcom/squareup/wire/schema/internal/parser/ProtoFileElement;Lcom/squareup/wire/schema/Location;Ljava/lang/String;Lcom/squareup/wire/Syntax;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;ILjava/lang/Object;)Lcom/squareup/wire/schema/internal/parser/ProtoFileElement;
13911394
public static final fun empty (Ljava/lang/String;)Lcom/squareup/wire/schema/internal/parser/ProtoFileElement;
13921395
public fun equals (Ljava/lang/Object;)Z
13931396
public final fun getExtendDeclarations ()Ljava/util/List;
@@ -1399,6 +1402,7 @@ public final class com/squareup/wire/schema/internal/parser/ProtoFileElement {
13991402
public final fun getServices ()Ljava/util/List;
14001403
public final fun getSyntax ()Lcom/squareup/wire/Syntax;
14011404
public final fun getTypes ()Ljava/util/List;
1405+
public final fun getWeakImports ()Ljava/util/List;
14021406
public fun hashCode ()I
14031407
public final fun toSchema ()Ljava/lang/String;
14041408
public fun toString ()Ljava/lang/String;

wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/ProtoFile.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ data class ProtoFile(
2525
val location: Location,
2626
val imports: List<String>,
2727
val publicImports: List<String>,
28+
val weakImports: List<String>,
2829
val packageName: String?,
2930
val types: List<Type>,
3031
val services: List<Service>,
@@ -42,6 +43,7 @@ data class ProtoFile(
4243
syntax,
4344
imports,
4445
publicImports,
46+
weakImports,
4547
Type.toElements(types),
4648
Service.toElements(services),
4749
Extend.toElements(extendList),
@@ -102,7 +104,7 @@ data class ProtoFile(
102104
val retainedOptions = options.retainAll(schema, markSet)
103105

104106
val result = ProtoFile(
105-
location, imports, publicImports, packageName, retainedTypes,
107+
location, imports, publicImports, weakImports, packageName, retainedTypes,
106108
retainedServices, retainedExtends, retainedOptions, syntax,
107109
)
108110
result.javaPackage = javaPackage
@@ -121,7 +123,7 @@ data class ProtoFile(
121123
val retainedOptions = options.retainLinked()
122124

123125
val result = ProtoFile(
124-
location, imports, publicImports, packageName, retainedTypes,
126+
location, imports, publicImports, weakImports, packageName, retainedTypes,
125127
retainedServices, retainedExtends, retainedOptions, syntax,
126128
)
127129
result.javaPackage = javaPackage
@@ -202,10 +204,14 @@ data class ProtoFile(
202204

203205
val retainedPublicImports = publicImports.filter { nonEmptyProtoFilesInSchema.contains(it) }
204206

205-
return if (imports.size != retainedImports.size || publicImports.size != retainedPublicImports.size) {
207+
val retainedWeakImports = weakImports.filter { referencedImports.contains(it) }
208+
209+
return if (imports.size != retainedImports.size || publicImports.size != retainedPublicImports.size ||
210+
weakImports.size != retainedWeakImports.size
211+
) {
206212
val result = ProtoFile(
207-
location, retainedImports, retainedPublicImports, packageName, types, services,
208-
extendList, options, syntax,
213+
location, retainedImports, retainedPublicImports, retainedWeakImports, packageName,
214+
types, services, extendList, options, syntax,
209215
)
210216
result.javaPackage = javaPackage
211217
result.wirePackage = wirePackage
@@ -251,8 +257,8 @@ data class ProtoFile(
251257

252258
return ProtoFile(
253259
protoFileElement.location, protoFileElement.imports,
254-
protoFileElement.publicImports, packageName, types, services, wireExtends, options,
255-
protoFileElement.syntax,
260+
protoFileElement.publicImports, protoFileElement.weakImports, packageName,
261+
types, services, wireExtends, options, protoFileElement.syntax,
256262
)
257263
}
258264
}

wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/internal/parser/ProtoFileElement.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ data class ProtoFileElement(
2626
val syntax: Syntax? = null,
2727
val imports: List<String> = emptyList(),
2828
val publicImports: List<String> = emptyList(),
29+
val weakImports: List<String> = emptyList(),
2930
val types: List<TypeElement> = emptyList(),
3031
val services: List<ServiceElement> = emptyList(),
3132
val extendDeclarations: List<ExtendElement> = emptyList(),
@@ -43,14 +44,17 @@ data class ProtoFileElement(
4344
append('\n')
4445
append("package $packageName;\n")
4546
}
46-
if (imports.isNotEmpty() || publicImports.isNotEmpty()) {
47+
if (imports.isNotEmpty() || publicImports.isNotEmpty() || weakImports.isNotEmpty()) {
4748
append('\n')
4849
for (file in imports) {
4950
append("import \"$file\";\n")
5051
}
5152
for (file in publicImports) {
5253
append("import public \"$file\";\n")
5354
}
55+
for (file in weakImports) {
56+
append("import weak \"$file\";\n")
57+
}
5458
}
5559
if (options.isNotEmpty()) {
5660
append('\n')

wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/internal/parser/ProtoParser.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ class ProtoParser internal constructor(
3030
data: CharArray,
3131
) {
3232
private val reader: SyntaxReader = SyntaxReader(data, location)
33-
private val publicImports = mutableListOf<String>()
3433
private val imports = mutableListOf<String>()
34+
private val publicImports = mutableListOf<String>()
35+
private val weakImports = mutableListOf<String>()
3536
private val nestedTypes = mutableListOf<TypeElement>()
3637
private val services = mutableListOf<ServiceElement>()
3738
private val extendsList = mutableListOf<ExtendElement>()
@@ -59,6 +60,7 @@ class ProtoParser internal constructor(
5960
syntax = syntax,
6061
imports = imports.map { it.toPath().withUnixSlashes().toString() },
6162
publicImports = publicImports.map { it.toPath().withUnixSlashes().toString() },
63+
weakImports = weakImports.map { it.toPath().withUnixSlashes().toString() },
6264
types = nestedTypes,
6365
services = services,
6466
extendDeclarations = extendsList,
@@ -114,6 +116,7 @@ class ProtoParser internal constructor(
114116

115117
label == "import" && context.permitsImport() -> {
116118
when (val importString = reader.readString()) {
119+
"weak" -> weakImports.add(reader.readString())
117120
"public" -> publicImports.add(reader.readString())
118121
else -> imports.add(importString)
119122
}

wire-schema/src/commonTest/kotlin/com/squareup/wire/schema/internal/parser/ProtoFileElementTest.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,16 @@ class ProtoFileElementTest {
152152
}
153153

154154
@Test
155-
fun simpleWithBothImportsToSchema() {
155+
fun simpleWithMultipleImportTypesToSchema() {
156156
val element = MessageElement(
157-
location = location,
157+
location = location.at(line = 8, column = 1),
158158
name = "Message",
159159
)
160160
val file = ProtoFileElement(
161161
location = location,
162162
imports = listOf("example.thing"),
163163
publicImports = listOf("example.other"),
164+
weakImports = listOf("yet.other"),
164165
types = listOf(element),
165166
)
166167
val expected = """
@@ -169,11 +170,16 @@ class ProtoFileElementTest {
169170
|
170171
|import "example.thing";
171172
|import public "example.other";
173+
|import weak "yet.other";
172174
|
173175
|message Message {}
174176
|
175177
""".trimMargin()
176178
assertThat(file.toSchema()).isEqualTo(expected)
179+
180+
// Re-parse the expected string into a ProtoFile and ensure they're equal.
181+
val parsed = ProtoParser.parse(location, expected)
182+
assertThat(parsed).isEqualTo(file)
177183
}
178184

179185
@Test

0 commit comments

Comments
 (0)