Skip to content

Commit

Permalink
Support generic qualifiers for TypeName
Browse files Browse the repository at this point in the history
Allows qualifying a `TypeName` with a generic qaulifier (`some` or `any`).
  • Loading branch information
kdubb committed Jan 23, 2024
1 parent 0e455a2 commit dacc5fe
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/main/java/io/outfoxx/swiftpoet/GenericQualifiedTypeName.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.outfoxx.swiftpoet

/**
* Qualify a [TypeName] with a [GenericQualifier] (any or some).
*
* `DataProtocol` qualified with `any` is
* ```
* any DataProtocol
* ```
*/
class GenericQualifiedTypeName internal constructor(
val type: TypeName,
val qualifier: GenericQualifier,
) : TypeName() {

override fun emit(out: CodeWriter): CodeWriter {
out.emitCode("${qualifier.name.lowercase()} ")
if (type is ComposedTypeName || type is GenericQualifiedTypeName) {
out.emitCode("(%T)", type)
} else {
out.emitCode("%T", type)
}
return out
}

companion object {

fun of(type: TypeName, qualifier: GenericQualifier): GenericQualifiedTypeName {
return GenericQualifiedTypeName(type, qualifier)
}

fun any(type: TypeName): GenericQualifiedTypeName {
return of(type, GenericQualifier.ANY)
}

fun some(type: TypeName): GenericQualifiedTypeName {
return of(type, GenericQualifier.SOME)
}
}

}

enum class GenericQualifier {
ANY,
SOME
}
3 changes: 3 additions & 0 deletions src/main/java/io/outfoxx/swiftpoet/TypeName.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ abstract class TypeName internal constructor() {

open fun makeNonImplicit(): TypeName = this

open fun qualify(qualifier: GenericQualifier): GenericQualifiedTypeName =
GenericQualifiedTypeName.of(this, qualifier)

open val name: String
get() {
val out = StringWriter()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2018 Outfox, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.outfoxx.swiftpoet.test

import io.outfoxx.swiftpoet.ComposedTypeName
import io.outfoxx.swiftpoet.DeclaredTypeName
import io.outfoxx.swiftpoet.GenericQualifiedTypeName
import io.outfoxx.swiftpoet.GenericQualifier
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test

class GenericQualifiedTypeNameTests {

@Test
@DisplayName("Generates generic qualified using explicit builders")
fun genericQualifiedTypeNameExplicit() {
val type = DeclaredTypeName.typeName(".GenericType")

assertThat(GenericQualifiedTypeName.any(type).name, equalTo("any GenericType"))
assertThat(GenericQualifiedTypeName.some(type).name, equalTo("some GenericType"))
}

@Test
@DisplayName("Generates generic qualified declared type names")
fun genericQualifiedDeclaredTypeName() {
val type = DeclaredTypeName.typeName(".GenericType")

assertThat(type.qualify(GenericQualifier.ANY).name, equalTo("any GenericType"))
assertThat(type.qualify(GenericQualifier.SOME).name, equalTo("some GenericType"))
}

@Test
@DisplayName("Generates generic qualified composite type names")
fun genericQualifiedCompositeTypeName() {
val typeA = DeclaredTypeName.typeName(".A")
val typeB = DeclaredTypeName.typeName(".B")
val typeC = DeclaredTypeName.typeName(".C")
val composed = ComposedTypeName.composed(typeA, typeB, typeC)

assertThat(composed.qualify(GenericQualifier.ANY).name, equalTo("any (A & B & C)"))
assertThat(composed.qualify(GenericQualifier.SOME).name, equalTo("some (A & B & C)"))
}
}

0 comments on commit dacc5fe

Please sign in to comment.