Skip to content

Commit 039544e

Browse files
Use numbers instead of type names as map keys in FileDescriptorResolver (#144)
1 parent fb79386 commit 039544e

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed

protokt-codegen/src/main/kotlin/com/toasttab/protokt/codegen/descriptor/FileDescriptorResolver.kt

+4-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import com.squareup.kotlinpoet.TypeSpec
2525
import com.toasttab.protokt.codegen.impl.bindMargin
2626
import com.toasttab.protokt.codegen.impl.embed
2727
import com.toasttab.protokt.codegen.impl.namedCodeBlock
28-
import com.toasttab.protokt.codegen.impl.toParamName
2928
import com.toasttab.protokt.codegen.protoc.Enum
3029
import com.toasttab.protokt.codegen.protoc.Message
3130
import com.toasttab.protokt.codegen.protoc.Protocol
@@ -58,21 +57,21 @@ private constructor(
5857
"""
5958
|lazy {
6059
| val descriptorData = arrayOf(
61-
|%descriptorData:L
60+
| %descriptorData:L
6261
| )
6362
|
6463
| %fileDescriptor:T.buildFrom(
6564
| descriptorData,
6665
| listOf(
67-
|${dependencyLines(dependencies)}
66+
| ${dependencyLines(dependencies)}
6867
| )
6968
| )
7069
|}
7170
""".bindMargin(),
7271
mapOf(
7372
"descriptorData" to descriptorLines(),
7473
"fileDescriptor" to ClassName("com.toasttab.protokt", "FileDescriptor")
75-
) + dependencies.associateBy { it.toParamName() }
74+
) + dependencies.withIndex().associateBy { "param${it.index}" }.mapValues { it.value.value }
7675
)
7776
).build()
7877
)
@@ -98,7 +97,7 @@ private constructor(
9897
)
9998

10099
private fun dependencyLines(dependencies: List<TypeName>) =
101-
dependencies.joinToString(",\n") { "%${it.toParamName()}:T.descriptor" }
100+
dependencies.withIndex().joinToString(",\n") { "%param${it.index}:T.descriptor" }
102101

103102
private fun clearJsonInfo(fileDescriptorProto: DescriptorProtos.FileDescriptorProto) =
104103
fileDescriptorProto.toBuilder()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2021 Toast Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
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+
syntax = "proto3";
17+
18+
// This is in a different packages since the test was created because of a bug
19+
// that triggered with reserved keywords (specifically `data`) in the package name.
20+
package toasttab.protokt.testing.rt.data;
21+
22+
23+
message Foo {
24+
bool data = 1;
25+
bool null = 2;
26+
bool abstract = 3;
27+
bool continue = 4;
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2021 Toast Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
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+
syntax = "proto3";
17+
18+
package toasttab.protokt.testing.rt.data;
19+
20+
import "toasttab/protokt/testing/rt/data/evil_names.proto";
21+
22+
message Bar {
23+
Foo foo = 1;
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2021 Toast Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
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.toasttab.protokt.testing.rt
17+
18+
import com.google.common.truth.Truth.assertThat
19+
import org.junit.jupiter.api.Test
20+
import toasttab.protokt.testing.rt.data.EvilNames
21+
import toasttab.protokt.testing.rt.data.ImportEvilNames
22+
23+
class EvilNamesTest {
24+
@Test
25+
fun `ensure package has reserved word in package name`() {
26+
assertThat(EvilNames::class.qualifiedName).contains(".data.")
27+
assertThat(ImportEvilNames::class.qualifiedName).contains(".data.")
28+
}
29+
}

0 commit comments

Comments
 (0)