Skip to content

Commit

Permalink
Inline lambda builder codegen function
Browse files Browse the repository at this point in the history
  • Loading branch information
joffrey-bion committed Apr 11, 2024
1 parent 49dabea commit 73cc8de
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ internal fun ChromeDPCommand.toDslFunctionSpec(): FunSpec {
if (optionalParams.any()) {
addAnnotation(Annotations.jvmOverloads)
addModifiers(KModifier.INLINE)
val initLambdaParam = initBuilderLambdaParam(names.inputTypeBuilderName, "optionalArgs") {
defaultValue("{}")
}
val initLambdaParam = optionalArgsBuilderLambdaParam(names.inputTypeBuilderName)
addParameter(initLambdaParam)
val builderConstructorCall = constructorCallTemplate(names.inputTypeBuilderName, mandatoryParams.map { it.name })
addNamedCode("val builder = ${builderConstructorCall.template}\n", builderConstructorCall.namedArgsMapping)
Expand All @@ -143,8 +141,12 @@ private fun FunSpec.Builder.commonCommandFunction(command: ChromeDPCommand) {
returns(command.names.outputTypeName)
}

private fun initBuilderLambdaParam(
builderTypeName: TypeName,
name: String,
configure: ParameterSpec.Builder.() -> Unit = {},
) = ParameterSpec.builder(name = name, type = lambdaTypeWithBuilderReceiver(builderTypeName)).apply(configure).build()
private fun optionalArgsBuilderLambdaParam(builderTypeName: TypeName) =
ParameterSpec.builder(name = "optionalArgs", type = lambdaTypeWithBuilderReceiver(builderTypeName)).apply {
defaultValue("{}")
}.build()

private fun lambdaTypeWithBuilderReceiver(builderTypeName: TypeName) = LambdaTypeName.get(
receiver = builderTypeName,
returnType = Unit::class.asTypeName(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ internal fun TypeSpec.Builder.addPrimaryConstructorProps(props: List<ChromeDPPar
addProperties(propertySpecs)
}

internal fun lambdaTypeWithBuilderReceiver(builderTypeName: TypeName) = LambdaTypeName.get(
receiver = builderTypeName,
parameters = emptyList(),
returnType = Unit::class.asTypeName(),
)

internal fun ChromeDPParameter.toParameterSpec(includeDocAndAnnotations: Boolean = true): ParameterSpec =
ParameterSpec.builder(name, type).apply {
if (includeDocAndAnnotations) {
Expand Down

0 comments on commit 73cc8de

Please sign in to comment.