Skip to content

Commit ec3a536

Browse files
committed
protoc-plugin: Add descriptors to declarations and remove unnecessary fields
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 8c6ae56 commit ec3a536

File tree

3 files changed

+38
-62
lines changed

3 files changed

+38
-62
lines changed

protoc-gen/src/main/kotlin/kotlinx/rpc/protobuf/ModelToKotlinCommonGenerator.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class ModelToKotlinCommonGenerator(
213213
code("$assignment decoder.read$encFuncName()")
214214
}
215215

216-
is FieldType.List -> if (field.packed) {
216+
is FieldType.List -> if (field.dec.isPacked) {
217217
whenCase("tag.fieldNr == ${field.number} && tag.wireType == WireType.LENGTH_DELIMITED") {
218218
code("$assignment decoder.readPacked${fieldType.value.decodeEncodeFuncName()}()")
219219
}
@@ -241,7 +241,7 @@ class ModelToKotlinCommonGenerator(
241241
scope("$fieldName?.also") {
242242
code(field.writeValue("it"))
243243
}
244-
} else if (!field.hasPresence) {
244+
} else if (!field.dec.hasPresence()) {
245245
ifBranch(condition = field.defaultCheck(), ifBlock = {
246246
code(field.writeValue(field.name))
247247
})
@@ -255,10 +255,10 @@ class ModelToKotlinCommonGenerator(
255255
return when (val fieldType = type) {
256256
is FieldType.IntegralType -> "encoder.write${type.decodeEncodeFuncName()}($number, $variable)"
257257
is FieldType.List -> when {
258-
packed && packedFixedSize ->
258+
dec.isPacked && packedFixedSize ->
259259
"encoder.writePacked${fieldType.value.decodeEncodeFuncName()}($number, $variable)"
260260

261-
packed && !packedFixedSize ->
261+
dec.isPacked && !packedFixedSize ->
262262
"encoder.writePacked${fieldType.value.decodeEncodeFuncName()}($number, $variable, ${
263263
wireSizeCall(
264264
variable
@@ -285,7 +285,7 @@ class ModelToKotlinCommonGenerator(
285285
}
286286

287287
is FieldType.List -> when {
288-
isPackable && !packedFixedSize -> sizeFunc
288+
dec.isPacked && !packedFixedSize -> sizeFunc
289289
else -> error("Unexpected use of size call for field: $name, type: $fieldType")
290290
}
291291

@@ -431,13 +431,13 @@ class ModelToKotlinCommonGenerator(
431431
code("@kotlinx.rpc.grpc.annotations.Grpc")
432432
clazz(service.name.simpleName, declarationType = DeclarationType.Interface) {
433433
service.methods.forEach { method ->
434-
val inputType by method.inputType
435-
val outputType by method.outputType
434+
val inputType = method.inputType
435+
val outputType = method.outputType
436436
function(
437437
name = method.name,
438-
modifiers = if (method.serverStreaming) "" else "suspend",
439-
args = "message: ${inputType.name.safeFullName().wrapInFlowIf(method.clientStreaming)}",
440-
returnType = outputType.name.safeFullName().wrapInFlowIf(method.serverStreaming),
438+
modifiers = if (method.dec.isServerStreaming) "" else "suspend",
439+
args = "message: ${inputType.name.safeFullName().wrapInFlowIf(method.dec.isClientStreaming)}",
440+
returnType = outputType.name.safeFullName().wrapInFlowIf(method.dec.isServerStreaming),
441441
)
442442
}
443443
}

protoc-gen/src/main/kotlin/kotlinx/rpc/protobuf/codeRequestToModel.kt

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,50 +57,42 @@ private fun Descriptors.FileDescriptor.toCommonModel(): FileDeclaration = cached
5757
messageDeclarations = messageTypes.map { it.toCommonModel() },
5858
enumDeclarations = enumTypes.map { it.toCommonModel() },
5959
serviceDeclarations = services.map { it.toCommonModel() },
60-
deprecated = options.deprecated,
6160
doc = null,
61+
dec = this,
6262
)
6363
}
6464

6565
private fun Descriptors.Descriptor.toCommonModel(): MessageDeclaration = cached {
6666
val regularFields = fields.filter { field -> field.realContainingOneof == null }.map { it.toCommonModel() }
6767

6868
return MessageDeclaration(
69-
outerClassName = fqName(),
7069
name = fqName(),
7170
actualFields = regularFields,
7271
// get all oneof declarations that are not created from an optional in proto3 https://github.com/googleapis/api-linter/issues/1323
7372
oneOfDeclarations = oneofs.filter { it.fields[0].realContainingOneof != null }.map { it.toCommonModel() },
7473
enumDeclarations = enumTypes.map { it.toCommonModel() },
7574
nestedDeclarations = nestedTypes.map { it.toCommonModel() },
76-
deprecated = options.deprecated,
7775
doc = null,
76+
dec = this,
7877
)
7978
}
8079

8180
private fun Descriptors.FieldDescriptor.toCommonModel(): FieldDeclaration = cached {
8281
toProto().hasProto3Optional()
8382
return FieldDeclaration(
8483
name = fqName().simpleName,
85-
number = number,
8684
type = modelType(),
87-
nullable = isNullable(),
88-
deprecated = options.deprecated,
8985
doc = null,
90-
proto = toProto(),
86+
dec = this,
9187
)
9288
}
9389

94-
private fun Descriptors.FieldDescriptor.isNullable(): Boolean {
95-
// aligns with edition settings and backward compatibility with proto2 and proto3
96-
return hasPresence() && !isRequired && !hasDefaultValue()
97-
}
9890

9991
private fun Descriptors.OneofDescriptor.toCommonModel(): OneOfDeclaration = cached {
10092
return OneOfDeclaration(
10193
name = fqName(),
10294
variants = fields.map { it.toCommonModel() },
103-
descriptor = this
95+
dec = this,
10496
)
10597
}
10698

@@ -122,20 +114,19 @@ private fun Descriptors.EnumDescriptor.toCommonModel(): EnumDeclaration = cached
122114
}
123115

124116
return EnumDeclaration(
125-
outerClassName = fqName(),
126117
name = fqName(),
127118
originalEntries = entriesMap.values.toList(),
128119
aliases = aliases,
129-
deprecated = options.deprecated,
130-
doc = null
120+
doc = null,
121+
dec = this,
131122
)
132123
}
133124

134125
private fun Descriptors.EnumValueDescriptor.toCommonModel(): EnumDeclaration.Entry = cached {
135126
return EnumDeclaration.Entry(
136127
name = fqName(),
137-
deprecated = options.deprecated,
138128
doc = null,
129+
dec = this,
139130
)
140131
}
141132

@@ -144,25 +135,25 @@ private fun Descriptors.EnumValueDescriptor.toAliasModel(original: EnumDeclarati
144135
return EnumDeclaration.Alias(
145136
name = fqName(),
146137
original = original,
147-
deprecated = options.deprecated,
148138
doc = null,
139+
dec = this,
149140
)
150141
}
151142

152143
private fun Descriptors.ServiceDescriptor.toCommonModel(): ServiceDeclaration = cached {
153144
return ServiceDeclaration(
154145
name = fqName(),
155-
methods = methods.map { it.toCommonModel() }
146+
methods = methods.map { it.toCommonModel() },
147+
dec = this,
156148
)
157149
}
158150

159151
private fun Descriptors.MethodDescriptor.toCommonModel(): MethodDeclaration = cached {
160152
return MethodDeclaration(
161153
name = name,
162-
clientStreaming = isClientStreaming,
163-
serverStreaming = isServerStreaming,
164-
inputType = lazy { inputType.toCommonModel() },
165-
outputType = lazy { outputType.toCommonModel() }
154+
inputType = inputType.toCommonModel(),
155+
outputType = outputType.toCommonModel(),
156+
dec = this,
166157
)
167158
}
168159

protoc-gen/src/main/kotlin/kotlinx/rpc/protobuf/model/model.kt

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
package kotlinx.rpc.protobuf.model
66

7-
import com.google.protobuf.DescriptorProtos
87
import com.google.protobuf.Descriptors
98

109
data class Model(
@@ -18,84 +17,70 @@ data class FileDeclaration(
1817
val messageDeclarations: List<MessageDeclaration>,
1918
val enumDeclarations: List<EnumDeclaration>,
2019
val serviceDeclarations: List<ServiceDeclaration>,
21-
val deprecated: Boolean,
2220
val doc: String?,
21+
val dec: Descriptors.FileDescriptor,
2322
)
2423

2524
data class MessageDeclaration(
26-
val outerClassName: FqName,
2725
val name: FqName,
2826
val actualFields: List<FieldDeclaration>, // excludes oneOf fields, but includes oneOf itself
2927
val oneOfDeclarations: List<OneOfDeclaration>,
3028
val enumDeclarations: List<EnumDeclaration>,
3129
val nestedDeclarations: List<MessageDeclaration>,
32-
val deprecated: Boolean,
3330
val doc: String?,
31+
val dec: Descriptors.Descriptor,
3432
)
3533

3634
data class EnumDeclaration(
37-
val outerClassName: FqName,
3835
val name: FqName,
3936
val originalEntries: List<Entry>,
4037
val aliases: List<Alias>,
41-
val deprecated: Boolean,
4238
val doc: String?,
39+
val dec: Descriptors.EnumDescriptor,
4340
) {
4441
data class Entry(
4542
val name: FqName,
46-
val deprecated: Boolean,
4743
val doc: String?,
44+
val dec: Descriptors.EnumValueDescriptor,
4845
)
4946

5047
data class Alias(
5148
val name: FqName,
5249
val original: Entry,
53-
val deprecated: Boolean,
5450
val doc: String?,
51+
val dec: Descriptors.EnumValueDescriptor,
5552
)
5653
}
5754

5855
data class OneOfDeclaration(
5956
val name: FqName,
6057
val variants: List<FieldDeclaration>,
61-
val descriptor: Descriptors.OneofDescriptor?
58+
val dec: Descriptors.OneofDescriptor
6259
)
6360

6461
data class FieldDeclaration(
6562
val name: String,
66-
val number: Int,
6763
val type: FieldType,
68-
val nullable: Boolean,
69-
val deprecated: Boolean,
7064
val doc: String?,
71-
val proto: DescriptorProtos.FieldDescriptorProto
65+
val dec: Descriptors.FieldDescriptor
7266
) {
73-
val isRepeated = proto.label == DescriptorProtos.FieldDescriptorProto.Label.LABEL_REPEATED
74-
val isExtension = proto.hasExtendee()
75-
val containsOneOf = proto.hasOneofIndex()
76-
77-
val hasPresence = if (isRepeated) false else
78-
proto.proto3Optional || proto.type == DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE
79-
|| proto.type == DescriptorProtos.FieldDescriptorProto.Type.TYPE_GROUP
80-
|| isExtension || containsOneOf
81-
82-
val isPackable = isRepeated && type.isPackable
83-
84-
val packed = isPackable // TODO: must checked if this is also declared as [packed = true] (or proto3 auto packed)
85-
8667
val packedFixedSize = type.wireType == WireType.FIXED64 || type.wireType == WireType.FIXED32
68+
69+
// aligns with edition settings and backward compatibility with proto2 and proto3
70+
val nullable: Boolean = dec.hasPresence() && !dec.isRequired && !dec.hasDefaultValue()
71+
val number: Int = dec.number
8772
}
8873

8974
data class ServiceDeclaration(
9075
val name: FqName,
9176
val methods: List<MethodDeclaration>,
77+
val dec: Descriptors.ServiceDescriptor,
9278
)
9379

9480
data class MethodDeclaration(
9581
val name: String,
96-
val clientStreaming: Boolean,
97-
val serverStreaming: Boolean,
98-
val inputType: Lazy<MessageDeclaration>,
99-
val outputType: Lazy<MessageDeclaration>,
82+
val inputType: MessageDeclaration,
83+
val outputType: MessageDeclaration,
84+
val dec: Descriptors.MethodDescriptor,
10085
)
10186

0 commit comments

Comments
 (0)