Skip to content

Commit 0e04ecf

Browse files
authored
Import nested enums in the same package when defined in a different file (#53)
1 parent fa6df45 commit 0e04ecf

File tree

6 files changed

+84
-23
lines changed

6 files changed

+84
-23
lines changed

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

+12-21
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,32 @@ sealed class Import {
2424
abstract val qualifiedName: String
2525
abstract val simpleName: String
2626
abstract val pkg: PPackage
27+
abstract val nested: Boolean
2728

2829
data class Class(val pClass: PClass) : Import() {
29-
override val qualifiedName
30-
get() = pClass.qualifiedName
31-
32-
override val pkg: PPackage
33-
get() = pClass.ppackage
34-
35-
override val simpleName: String
36-
get() = pClass.simpleName
30+
override val qualifiedName = pClass.qualifiedName
31+
override val pkg = pClass.ppackage
32+
override val simpleName = pClass.simpleName
33+
override val nested = pClass.enclosing.isDefined()
3734
}
3835

3936
data class PackageMethod(
4037
override val pkg: PPackage,
4138
val name: String
4239
) : Import() {
43-
override val qualifiedName
44-
get() = pkg.qualify(name)
45-
46-
override val simpleName: String
47-
get() = name
40+
override val qualifiedName = pkg.qualify(name)
41+
override val simpleName = name
42+
override val nested = false
4843
}
4944

5045
data class ClassMethod(
5146
val enclosingClass: PClass,
5247
val name: String
5348
) : Import() {
54-
override val pkg: PPackage
55-
get() = enclosingClass.ppackage
56-
57-
override val qualifiedName: String
58-
get() = "${enclosingClass.qualifiedName}.$name"
59-
60-
override val simpleName: String
61-
get() = name
49+
override val pkg = enclosingClass.ppackage
50+
override val qualifiedName = "${enclosingClass.qualifiedName}.$name"
51+
override val simpleName = name
52+
override val nested = true
6253
}
6354
}
6455

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ class ImportResolver(
6666
fun resolveImports(astList: List<AST<TypeDesc>>) =
6767
astList.flatMapToSet { imports(it.data.type.rawType) }
6868
.asSequence()
69-
.filterNot { it.pkg == pkg }
7069
.filterNot { it.pkg == PPackage.KOTLIN }
7170
.filterNot { it is Import.Class && it.pClass.simpleName == "Any" }
71+
.filterClassesWithSamePackageName(pkg)
7272
.filterClassesWithSameNameAsMessageIn(astList)
7373
.filterClassesWithSameNameAsOneofFieldTypeIn(astList)
7474
.filterDuplicateSimpleNames(pkg) { getClassOrNone(it, ctx) }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
package com.toasttab.protokt.codegen.impl
17+
18+
import com.toasttab.protokt.codegen.model.PPackage
19+
20+
fun Sequence<Import>.filterClassesWithSamePackageName(pkg: PPackage) =
21+
filterNot { it.pkg == pkg && !it.nested }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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;
19+
20+
message DefinesNestedEnum {
21+
enum NestedEnum {
22+
ZEROTH = 0;
23+
FIRST = 1;
24+
}
25+
}

testing/runtime-tests/src/main/proto/com/toasttab/protokt/testing/rt/enums_across_packages.proto

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ package com.toasttab.protokt.testing.rt;
2020
import "com/toasttab/protokt/testing/rt/other/other_package.proto";
2121

2222
message UsesOtherPackageEnum {
23-
com.toasttab.protokt.testing.rt.other.OtherPackageMessage.OtherPackageEnum enum = 1;
23+
other.OtherPackageMessage.OtherPackageEnum enum = 1;
2424
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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;
19+
20+
import "com/toasttab/protokt/testing/rt/defines_nested_enum.proto";
21+
22+
message UsesNestedEnumInDifferentFileSamePackage {
23+
DefinesNestedEnum.NestedEnum value = 1;
24+
}

0 commit comments

Comments
 (0)