Skip to content

Commit da27547

Browse files
IRusEgorand
andauthored
Allow to use context parameters as Names (#2114) (#2117)
* Allow to use context parameters as Names (#2114) * Allow to use context parameters as Names (#2114) Co-authored-by: Egor Andreevich <[email protected]> * Allow to use context parameters as Names (#2114) Co-authored-by: Egor Andreevich <[email protected]> * Allow to use context parameters as Names (#2114) * Allow to use context parameters as Names (#2114) --------- Co-authored-by: Egor Andreevich <[email protected]>
1 parent 7e17f09 commit da27547

File tree

9 files changed

+63
-50
lines changed

9 files changed

+63
-50
lines changed

docs/context-parameters.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ public fun greet() {
2222
You can add multiple context parameters:
2323

2424
```kotlin
25+
val loggerType = ClassName("java.util.logging", "Logger")
26+
val configType = ClassName("com.example", "Config")
27+
28+
val logger = ContextParameter("logger", loggerType)
29+
val config = ContextParameter("config", configType)
30+
2531
val processData = FunSpec.builder("processData")
26-
.contextParameter("logger", ClassName("com.example", "Logger"))
27-
.contextParameter("config", ClassName("com.example", "Config"))
28-
.addStatement("logger.info(\"Processing with config: ${'$'}config\")")
32+
.contextParameter(logger)
33+
.contextParameter(config)
34+
.addStatement("%N.info(\"Processing with config: ${'$'}%N\")", logger, config)
2935
.build()
3036
```
3137

kotlinpoet/api/kotlinpoet.api

-5
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,6 @@ public final class com/squareup/kotlinpoet/FunSpec$Builder : com/squareup/kotlin
392392
public final fun callThisConstructor ([Ljava/lang/String;)Lcom/squareup/kotlinpoet/FunSpec$Builder;
393393
public static synthetic fun callThisConstructor$default (Lcom/squareup/kotlinpoet/FunSpec$Builder;[Lcom/squareup/kotlinpoet/CodeBlock;ILjava/lang/Object;)Lcom/squareup/kotlinpoet/FunSpec$Builder;
394394
public final fun clearBody ()Lcom/squareup/kotlinpoet/FunSpec$Builder;
395-
public synthetic fun contextParameter (Lcom/squareup/kotlinpoet/TypeName;)Lcom/squareup/kotlinpoet/ContextParameterizable$Builder;
396-
public synthetic fun contextParameter (Ljava/lang/String;Lcom/squareup/kotlinpoet/TypeName;)Lcom/squareup/kotlinpoet/ContextParameterizable$Builder;
397395
public synthetic fun contextParameters (Ljava/lang/Iterable;)Lcom/squareup/kotlinpoet/ContextParameterizable$Builder;
398396
public synthetic fun contextReceivers (Ljava/lang/Iterable;)Lcom/squareup/kotlinpoet/ContextReceivable$Builder;
399397
public final fun endControlFlow ()Lcom/squareup/kotlinpoet/FunSpec$Builder;
@@ -795,9 +793,6 @@ public final class com/squareup/kotlinpoet/PropertySpec$Builder : com/squareup/k
795793
public final fun addTypeVariable (Lcom/squareup/kotlinpoet/TypeVariableName;)Lcom/squareup/kotlinpoet/PropertySpec$Builder;
796794
public final fun addTypeVariables (Ljava/lang/Iterable;)Lcom/squareup/kotlinpoet/PropertySpec$Builder;
797795
public final fun build ()Lcom/squareup/kotlinpoet/PropertySpec;
798-
public synthetic fun contextParameter (Lcom/squareup/kotlinpoet/TypeName;)Lcom/squareup/kotlinpoet/ContextParameterizable$Builder;
799-
public synthetic fun contextParameter (Ljava/lang/String;Lcom/squareup/kotlinpoet/TypeName;)Lcom/squareup/kotlinpoet/ContextParameterizable$Builder;
800-
public synthetic fun contextParameters (Ljava/lang/Iterable;)Lcom/squareup/kotlinpoet/ContextParameterizable$Builder;
801796
public final fun delegate (Lcom/squareup/kotlinpoet/CodeBlock;)Lcom/squareup/kotlinpoet/PropertySpec$Builder;
802797
public final fun delegate (Ljava/lang/String;[Ljava/lang/Object;)Lcom/squareup/kotlinpoet/PropertySpec$Builder;
803798
public fun getAnnotations ()Ljava/util/List;

kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/CodeBlock.kt

+1
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ public class CodeBlock private constructor(
362362
is FunSpec -> o.name
363363
is TypeSpec -> o.name!!
364364
is MemberName -> o.simpleName
365+
is ContextParameter -> if (o.name == "_") throw IllegalStateException("Named context parameter required") else o.name
365366
else -> throw IllegalArgumentException("expected name but was $o")
366367
}
367368

kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/ContextParameter.kt

+7
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ public interface ContextParameterizable {
9191
public fun contextParameter(type: TypeName): T =
9292
contextParameters(listOf(ContextParameter(type)))
9393

94+
/**
95+
* Adds the given [ContextParameter] to this type's list of context parameters.
96+
*/
97+
@ExperimentalKotlinPoetApi
98+
public fun contextParameter(contextParameter: ContextParameter): T =
99+
contextParameters(listOf(contextParameter))
100+
94101
/**
95102
* Adds a context parameter with the given [name] and [type] to this type's list of context parameters.
96103
*/

kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/FunSpec.kt

+1-18
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ public class FunSpec private constructor(
304304
builder.tags += tagMap.tags
305305
builder.originatingElements += originatingElements
306306
builder.contextReceiverTypes += contextReceiverTypes
307+
builder.contextParameters += contextParameters
307308
return builder
308309
}
309310

@@ -379,24 +380,6 @@ public class FunSpec private constructor(
379380
contextReceiverTypes += receiverTypes
380381
}
381382

382-
/**
383-
* Adds a context parameter with the given [name] and [type] to this function.
384-
*/
385-
@ExperimentalKotlinPoetApi
386-
override fun contextParameter(name: String, type: TypeName): Builder = apply {
387-
checkContextParametersAllowed()
388-
contextParameters += ContextParameter(name, type)
389-
}
390-
391-
/**
392-
* Adds a context parameter with the name "_" and [type] to this type's list of context parameters.
393-
*/
394-
@ExperimentalKotlinPoetApi
395-
override fun contextParameter(type: TypeName): Builder = apply {
396-
checkContextParametersAllowed()
397-
contextParameters += ContextParameter(type)
398-
}
399-
400383
/**
401384
* Adds context parameters to this function.
402385
*/

kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/PropertySpec.kt

+1-24
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public class PropertySpec private constructor(
172172
builder.tags += tagMap.tags
173173
builder.originatingElements += originatingElements
174174
builder.contextReceiverTypes += contextReceiverTypes
175+
builder.contextParameters += contextParameters
175176
return builder
176177
}
177178

@@ -205,30 +206,6 @@ public class PropertySpec private constructor(
205206
@ExperimentalKotlinPoetApi
206207
override val contextParameters: MutableList<ContextParameter> = mutableListOf()
207208

208-
/**
209-
* Adds a context parameter with the given [name] and [type] to this property.
210-
*/
211-
@ExperimentalKotlinPoetApi
212-
override fun contextParameter(name: String, type: TypeName): Builder = apply {
213-
contextParameters += ContextParameter(name, type)
214-
}
215-
216-
/**
217-
* Adds a context parameter with the name "_" and [type] to this type's list of context parameters.
218-
*/
219-
@ExperimentalKotlinPoetApi
220-
override fun contextParameter(type: TypeName): Builder = apply {
221-
contextParameters += ContextParameter(type)
222-
}
223-
224-
/**
225-
* Adds context parameters to this property.
226-
*/
227-
@ExperimentalKotlinPoetApi
228-
override fun contextParameters(parameters: Iterable<ContextParameter>): Builder = apply {
229-
contextParameters += parameters
230-
}
231-
232209
/** True to create a `var` instead of a `val`. */
233210
public fun mutable(mutable: Boolean = true): Builder = apply {
234211
this.mutable = mutable

kotlinpoet/src/jvmTest/kotlin/com/squareup/kotlinpoet/CodeBlockTest.kt

+19
Original file line numberDiff line numberDiff line change
@@ -681,4 +681,23 @@ class CodeBlockTest {
681681

682682
Locale.setDefault(defaultLocale)
683683
}
684+
685+
@Test fun nameFromContextParameter() {
686+
val logger = ContextParameter("logger", ClassName("java.util.logging", "Logger"))
687+
assertThat(CodeBlock.of("%N", logger).toString()).isEqualTo("logger")
688+
}
689+
690+
@Test fun nameFromMultipleContextParameters() {
691+
val logger = ContextParameter("logger", ClassName("java.util.logging", "Logger"))
692+
val config = ContextParameter("config", ClassName("com.example", "Config"))
693+
assertThat(CodeBlock.of("%N.info(\"Processing with config: ${'$'}%N\")", logger, config).toString())
694+
.isEqualTo("logger.info(\"Processing with config: ${'$'}config\")")
695+
}
696+
697+
@Test fun nameFromUnnamedContextParameter() {
698+
val unnamed = ContextParameter(ClassName("java.util.logging", "Logger"))
699+
assertThrows<IllegalStateException> {
700+
CodeBlock.of("%N", unnamed)
701+
}.hasMessageThat().isEqualTo("Named context parameter required")
702+
}
684703
}

kotlinpoet/src/jvmTest/kotlin/com/squareup/kotlinpoet/FunSpecTest.kt

+24
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,30 @@ class FunSpecTest {
702702
}.hasMessageThat().isEqualTo("Using both context receivers and context parameters is not allowed")
703703
}
704704

705+
@Test fun contextParameterInAddStatement() {
706+
val loggerType = ClassName("java.util.logging", "Logger")
707+
val configType = ClassName("com.example", "Config")
708+
709+
val logger = ContextParameter("logger", loggerType)
710+
val config = ContextParameter("config", configType)
711+
712+
val processData = FunSpec.builder("processData")
713+
.contextParameter(logger)
714+
.contextParameter(config)
715+
.addStatement("%N.info(\"Processing with config: ${'$'}%N\")", logger, config)
716+
.build()
717+
718+
assertThat(processData.toString()).isEqualTo(
719+
"""
720+
|context(logger: java.util.logging.Logger, config: com.example.Config)
721+
|public fun processData() {
722+
| logger.info("Processing with config: ${'$'}config")
723+
|}
724+
|
725+
""".trimMargin(),
726+
)
727+
}
728+
705729
@Test fun constructorWithContextParameter() {
706730
assertThrows<IllegalStateException> {
707731
FunSpec.constructorBuilder()

kotlinpoet/src/jvmTest/kotlin/com/squareup/kotlinpoet/PropertySpecTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ class PropertySpecTest {
288288
.addModifiers(KModifier.PUBLIC)
289289
.addTypeVariable(TypeVariableName("T"))
290290
.delegate("Delegates.notNull()")
291+
.contextParameter("user", STRING)
291292
.receiver(Int::class)
292293
.getter(
293294
FunSpec.getterBuilder()

0 commit comments

Comments
 (0)