|
| 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.algebra.AST |
| 19 | +import com.toasttab.protokt.codegen.model.Import |
| 20 | +import com.toasttab.protokt.codegen.protoc.Enum |
| 21 | +import com.toasttab.protokt.codegen.protoc.Message |
| 22 | +import com.toasttab.protokt.codegen.protoc.Oneof |
| 23 | +import com.toasttab.protokt.codegen.protoc.StandardField |
| 24 | +import com.toasttab.protokt.codegen.protoc.TopLevelType |
| 25 | +import com.toasttab.protokt.codegen.protoc.TypeDesc |
| 26 | + |
| 27 | +/** |
| 28 | + * All classes defined in the scope of a message that are used as a field type |
| 29 | + * are added as possible import candidates. When those classes are only used |
| 30 | + * within the message that defines them, there is no need to import them to use |
| 31 | + * them without qualification by their enclosing message type. For example: |
| 32 | + * |
| 33 | + * class Message( |
| 34 | + * val enum: SomeEnum // does not need to be `Message.SomeEnum` |
| 35 | + * ) : KtMessage { |
| 36 | + * sealed class SomeEnum(...) : KtEnum() { |
| 37 | + * object OPTION_ONE : SomeEnum(...) |
| 38 | + * } |
| 39 | + * } |
| 40 | + * |
| 41 | + * These imports _are_ necessary in other files or equivalently when used in |
| 42 | + * messages other than that in which they are defined. |
| 43 | + */ |
| 44 | +fun Sequence<Import>.filterNestedClassesDefinedLocally(asts: List<AST<TypeDesc>>) = |
| 45 | + filterNot { |
| 46 | + it is Import.Class && it.nested && |
| 47 | + astsUsing(it, asts).containsOnly(astDefining(it, asts)) |
| 48 | + } |
| 49 | + |
| 50 | +private fun astsUsing(import: Import.Class, asts: List<AST<TypeDesc>>) = |
| 51 | + asts.filter { searchTypes(it.data.type.rawType, import) }.toSet() |
| 52 | + |
| 53 | +private fun searchTypes(t: TopLevelType, import: Import.Class): Boolean = |
| 54 | + t is Message && |
| 55 | + (searchFields(t, import) || |
| 56 | + t.nestedTypes.any { searchTypes(it, import) }) |
| 57 | + |
| 58 | +private fun searchFields(msg: Message, import: Import.Class) = |
| 59 | + msg.fields.any { |
| 60 | + when (it) { |
| 61 | + is StandardField -> it.typePClass == import.pClass |
| 62 | + is Oneof -> it.fields.any { f -> f.typePClass == import.pClass } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | +private fun astDefining(import: Import.Class, asts: List<AST<TypeDesc>>) = |
| 67 | + asts.filter { |
| 68 | + it.data.type.rawType.let { t -> |
| 69 | + import.pkg == kotlinPackage(it) && |
| 70 | + t is Message && searchMessage(t, t.name, import) |
| 71 | + } |
| 72 | + }.let { |
| 73 | + if (it.isNotEmpty()) { |
| 74 | + // singleOrNull returns null if more than one match rather than throwing |
| 75 | + it.single() |
| 76 | + } else { |
| 77 | + null |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | +private fun searchMessage(msg: Message, name: String, import: Import.Class): Boolean = |
| 82 | + name == import.pClass.nestedName || |
| 83 | + msg.nestedTypes.any { |
| 84 | + val nestedName = "$name.${it.name}" |
| 85 | + when (it) { |
| 86 | + is Message -> searchMessage(it, nestedName, import) |
| 87 | + is Enum -> nestedName == import.pClass.nestedName |
| 88 | + else -> false |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | +private fun <T> Set<T>.containsOnly(t: T) = |
| 93 | + size == 1 && contains(t) |
0 commit comments