I need to add a mapper to my extension, I tried `...
# community-support
j
I need to add a mapper to my extension, I tried
Copy code
public fun mapVersion(transform: (GradleVersion) -> String) {
        mapper.set(
            object : VersionValueSource.Mapper {
                override fun map(version: GradleVersion, gitData: GitData): String {
                    return transform(version)
                }
            },
        )
    }
But it is not working.
Action<GradleVersion>
is not enough as I need to return a
String
and it only allows
Unit
. Is there something similar to that
Action
but allowing changing the return type?
The stacktrace:
Copy code
Caused by: org.gradle.internal.snapshot.ValueSnapshottingException: Couldn't populate class com.javiersc.semver.project.gradle.plugin.SemverExtension$mapVersion$mapper$1
a
There's
org.gradle.api.Transformer
, but I don't think it will help with value snapshotting
is VersionValueSource an implementation of
org.gradle.api.provider.ValueSource
?
j
Yes
I am creating multiple fun interfaces
Copy code
internal val versionMapper: Property<VersionMapper> =
    objects
        .property<VersionMapper>()
        .convention(VersionMapper { version -> version.toString() })
internal val versionAndGitMapper: Property<VersionAndGitMapper> =
    objects
        .property<VersionAndGitMapper>()
        .convention(VersionAndGitMapper { version, _ -> version.toString() })
public fun mapVersion(transform: VersionMapper) {
    versionMapper.set(transform)
}
public fun mapVersion(transform: VersionAndGitMapper) {
    versionAndGitMapper.set(transform)
}
Really ugly, but it works
Probably I can use
Transformer
instead of fun interfaces to ensure compatibility in Groovy
Well, with
Transformer
I cannot have a lambda with multiple params, right?
a
I thought that ValueSource instances had to be created with
ProviderFactory.of()
, do they really work if you create them as a functional interface?
j
the mappers are the functional interface and I am passing them to the parameters
Copy code
project.providers.of(VersionValueSource::class) { valueSourceSpec ->
                val gitDir = project.provider { project.semverExtension.gitDir.get().asFile }
                valueSourceSpec.parameters.versionMapper.set(project.semverExtension.versionMapper)
                valueSourceSpec.parameters.versionAndGitMapper.set(
a
oh sorry, I misread, thanks for clarifying
🙂 1
j
A
Transformer
is not serializable so it cannot be passed to the source params