Skip to content

Commit

Permalink
Add support for closure based properties and lazy modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
dnkoutso committed Feb 15, 2024
1 parent c965f3b commit fb41275
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/io/outfoxx/swiftpoet/Modifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ enum class Modifier(
CLASS("class", Target.FUNCTION, Target.PROPERTY),
STATIC("static", Target.FUNCTION, Target.PROPERTY),

LAZY("lazy", Target.PROPERTY),

MUTATING("mutating", Target.FUNCTION, Target.PROPERTY),
NONMUTATING("nonmutating", Target.FUNCTION, Target.PROPERTY),

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/outfoxx/swiftpoet/PropertySpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class PropertySpec private constructor(
) : AttributedSpec(builder.attributes.toImmutableList(), builder.tags) {

val mutable = builder.mutable
val closure = builder.closure
val mutableVisibility = builder.mutableVisibility
val simpleSpec = builder.simpleSpec
val subscriptSpec = builder.subscriptSpec
Expand Down Expand Up @@ -118,6 +119,9 @@ class PropertySpec private constructor(
}

codeWriter.emit("}")
if (closure) {
codeWriter.emit("()")
}
}
}

Expand All @@ -136,6 +140,7 @@ class PropertySpec private constructor(
val builder = Builder()
builder.simpleSpec = simpleSpec
builder.mutable = mutable
builder.closure = closure
builder.doc.add(doc)
builder.modifiers += modifiers
builder.initializer = initializer
Expand All @@ -148,6 +153,7 @@ class PropertySpec private constructor(
internal var simpleSpec: Pair<String, TypeName>? = null
internal var subscriptSpec: FunctionSignatureSpec? = null
internal var mutable = false
internal var closure = false
internal var mutableVisibility: Modifier? = null
internal val doc = CodeBlock.builder()
internal val modifiers = mutableListOf<Modifier>()
Expand All @@ -165,6 +171,10 @@ class PropertySpec private constructor(
this.subscriptSpec = subscriptSpec
}

fun closure(closure: Boolean) = apply {
this.closure = closure
}

fun mutable(mutable: Boolean) = apply {
check(subscriptSpec == null) { "subscripts cannot be mutable" }
this.mutable = mutable
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/io/outfoxx/swiftpoet/test/PropertySpecTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import io.outfoxx.swiftpoet.DeclaredTypeName.Companion.typeName
import io.outfoxx.swiftpoet.FunctionSignatureSpec
import io.outfoxx.swiftpoet.FunctionSpec
import io.outfoxx.swiftpoet.INT
import io.outfoxx.swiftpoet.Modifier.LAZY
import io.outfoxx.swiftpoet.Modifier.PRIVATE
import io.outfoxx.swiftpoet.Modifier.PUBLIC
import io.outfoxx.swiftpoet.Modifier.UNOWNED
Expand Down Expand Up @@ -87,6 +88,26 @@ class PropertySpecTests {
)
}

@Test
@DisplayName("Adds lazy modifier")
fun lazyProperty() {
val testProperty =
PropertySpec.varBuilder("test", STRING, LAZY)
.build()

val out = StringWriter()
testProperty.emit(CodeWriter(out), setOf())

assertThat(
out.toString(),
equalTo(
"""
lazy var test: Swift.String
""".trimIndent()
)
)
}

@Test
@DisplayName("Adds unowned modifier")
fun unownedReferences() {
Expand Down Expand Up @@ -128,6 +149,45 @@ class PropertySpecTests {
)
}

@Test
@DisplayName("Adds closure")
fun addsClosure() {
val testProperty =
PropertySpec.varBuilder("test", STRING, PUBLIC)
.closure(true)
.getter(
FunctionSpec
.getterBuilder()
.addStatement("%S", 1)
.build()
)
.setter(
FunctionSpec
.setterBuilder()
.addCode("")
.build()
)
.build()

val out = StringWriter()
testProperty.emit(CodeWriter(out), setOf())

assertThat(
out.toString(),
equalTo(
"""
public var test: Swift.String {
get {
"1"
}
set {
}
}()
""".trimIndent()
)
)
}

@Test
@DisplayName("Standalone mutable visibility modifier")
fun standaloneMutableVisibility() {
Expand Down

0 comments on commit fb41275

Please sign in to comment.