Skip to content

Commit 8a30fc5

Browse files
authored
Don't qualify oneof type name when it doesn't share a name with its enclosing type (#82)
1 parent 7457803 commit 8a30fc5

File tree

3 files changed

+54
-25
lines changed

3 files changed

+54
-25
lines changed

protokt-codegen/src/main/kotlin/com/toasttab/protokt/codegen/impl/OneofAnnotator.kt

+7-6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ private constructor(
9191
PClass.fromName(fieldType).let {
9292
// If a wrapper type is specified and it shares a name with the
9393
// oneof, it must be fully qualified.
94+
// See testing/options/src/main/proto/com/toasttab/protokt/testing/options/oneof_exercises.proto
9495
if (unqualifiedType.simpleName == it.simpleName) {
9596
it.possiblyQualify(ctx.pkg).qualifiedName
9697
} else {
@@ -109,21 +110,21 @@ private constructor(
109110
val pClass = f.typePClass
110111

111112
// Cannot strip qualifiers for field type in a different package
113+
// See testing/runtime-tests/src/main/proto/com/toasttab/protokt/testing/rt/oneof/oneof_packages.proto
112114
val requiresQualifiedTypeName = pClass.ppackage != ctx.pkg
113115

114116
return if (requiresQualifiedTypeName) {
115117
pClass.renderName(ctx.pkg)
116118
} else {
117-
if (oneofFieldTypeName == pClass.nestedName) {
118-
// Oneof field name shares name of its type
119-
if (oneofFieldTypeName == pClass.simpleName) {
120-
// Oneof field name shares name of its enclosing type
119+
// See testing/runtime-tests/src/main/proto/com/toasttab/protokt/testing/rt/oneof/oneof_exercises.proto
120+
if (oneofFieldTypeName == pClass.simpleName) {
121+
if (oneofFieldTypeName == pClass.nestedName) {
121122
pClass.qualifiedName
122123
} else {
123-
pClass.simpleName
124+
pClass.nestedName
124125
}
125126
} else {
126-
pClass.nestedName
127+
pClass.simpleName
127128
}
128129
}
129130
}

testing/options/src/main/proto/com/toasttab/protokt/testing/options/oneof_exercises.proto

+1-19
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,9 @@ import "protokt/protokt.proto";
2323
// generated sealed class type names. If the code generator does
2424
// not handle them properly then compilation will fail.
2525

26-
message OneofExerciseModel {
27-
oneof oneof {
28-
// Needs partial qualification: OneofExerciseModel.Model
29-
Model model = 1;
30-
}
31-
32-
message Model {
33-
bytes id = 1;
34-
}
35-
}
36-
37-
message OneofExerciseModel2 {
38-
oneof oneof {
39-
// Needs full qualification: com.toasttab.model.OneofExerciseModel
40-
OneofExerciseModel oneof_exercise_model = 1;
41-
}
42-
}
43-
4426
message OneofExerciseModelWithWrapper {
4527
oneof oneof {
46-
// Needs full qualification: com.toasttab.testing.options.CachingId
28+
// Needs full qualification: com.toasttab.protokt.testing.options.CachingId
4729
bytes caching_id = 1 [
4830
(.protokt.property).wrap = "CachingId"
4931
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2020 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 com.toasttab.protokt.testing.rt.oneof;
19+
20+
// This file exercises oneofs whose names are the same as their
21+
// generated sealed class type names. If the code generator does
22+
// not handle them properly then compilation will fail.
23+
24+
message OneofExerciseModel {
25+
oneof oneof {
26+
// Needs partial qualification: OneofExerciseModel.Model
27+
Model model = 1;
28+
}
29+
30+
oneof oneof2 {
31+
// Does not need partial qualification: Model
32+
// This will still compile if qualified but may show a warning in IDEs
33+
Model not_named_model = 2;
34+
}
35+
36+
message Model {
37+
bytes id = 1;
38+
}
39+
}
40+
41+
message OneofExerciseModel2 {
42+
oneof oneof {
43+
// Needs full qualification: com.toasttab.protokt.testing.rt.oneof.OneofExerciseModel
44+
OneofExerciseModel oneof_exercise_model = 1;
45+
}
46+
}

0 commit comments

Comments
 (0)