-
Notifications
You must be signed in to change notification settings - Fork 375
PKL Config Scala #1076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andyglow
wants to merge
1
commit into
apple:main
Choose a base branch
from
andyglow:add-pkl-config-scala
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
PKL Config Scala #1076
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| version = "3.10.0" | ||
| runner.dialect = scala3 | ||
|
|
||
| maxColumn = 100 | ||
|
|
||
| docstrings.style = Asterisk | ||
| docstrings.blankFirstLine = yes | ||
| docstrings.wrap = yes | ||
|
|
||
| rewrite.scala3.removeOptionalBraces = false | ||
| rewrite.insertBraces.minLines = 1 // Or 2 | ||
| rewrite.insertBraces.allBlocks = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. | ||
| * | ||
| * 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
| @file:Suppress("HttpUrlsUsage", "unused") | ||
|
|
||
| import org.gradle.accessors.dm.LibrariesForLibs | ||
| import org.gradle.kotlin.dsl.withType | ||
|
|
||
| plugins { | ||
| id("pklJavaLibrary") | ||
| scala | ||
| } | ||
|
|
||
| // Build configuration. | ||
| val buildInfo = project.extensions.getByType<BuildInfo>() | ||
|
|
||
| // Version Catalog library symbols. | ||
| val libs = the<LibrariesForLibs>() | ||
|
|
||
| dependencies { | ||
| testImplementation(libs.scalaTestPlusJunit) | ||
| testImplementation(libs.scalaTest) | ||
| testImplementation(libs.diffx) | ||
| } | ||
|
|
||
| scala { scalaVersion = libs.versions.scala } | ||
|
|
||
| tasks.withType<ScalaCompile>().configureEach { | ||
| scalaCompileOptions.additionalParameters = | ||
| listOf("-Xsource:3", "-release:${buildInfo.jvmTarget}", "-target:${buildInfo.jvmTarget}") | ||
| } | ||
|
|
||
| tasks.test { | ||
| useJUnitPlatform { | ||
| includeEngines("scalatest") | ||
| testLogging { events("passed", "skipped", "failed") } | ||
| } | ||
| } | ||
|
|
||
| spotless { | ||
| scala { | ||
| scalafmt(libs.versions.scalafmt.get()).configFile(rootProject.file(".scalafmt.conf")) | ||
| target("src/*/scala/**/*.scala") | ||
| licenseHeaderFile( | ||
| rootProject.file("build-logic/src/main/resources/license-header.star-block.txt"), | ||
| "package ", | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. | ||
| * | ||
| * 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
| plugins { | ||
| id("pklAllProjects") | ||
| id("pklScalaLibrary") | ||
| id("pklPublishLibrary") | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(projects.pklConfigJava) | ||
| api(libs.scalaReflect) | ||
| } | ||
|
|
||
| publishing { | ||
| publications { | ||
| named<MavenPublication>("library") { | ||
| pom { | ||
| url.set("https://github.com/apple/pkl/tree/main/pkl-config-scala") | ||
| description.set("Scala config library based on the Pkl config language.") | ||
| } | ||
| } | ||
| } | ||
| } |
161 changes: 161 additions & 0 deletions
161
pkl-config-scala/src/main/scala/org/pkl/config/scala/mapper/CachedConverterFactories.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| * Copyright © 2025-2026 Apple Inc. and the Pkl project authors. All rights reserved. | ||
| * | ||
| * 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 | ||
| * | ||
| * https://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 org.pkl.config.scala.mapper | ||
|
|
||
| import org.pkl.config.java.mapper.{Converter, ConverterFactory, Reflection, ValueMapper} | ||
| import org.pkl.config.scala.mapper.JavaReflectionSyntaxExtensions.* | ||
| import org.pkl.core.PClassInfo | ||
|
|
||
| import java.lang.reflect.Type | ||
| import java.util.Optional | ||
| import scala.jdk.OptionConverters.RichOption | ||
| import scala.reflect.ClassTag | ||
|
|
||
| /** | ||
| * Provides infrastructure that helps define custom converter factories in a somewhat concise way at | ||
| * the same time utilizing caching. | ||
| */ | ||
| private[mapper] object CachedConverterFactories { | ||
|
|
||
| /** | ||
| * Function used in converters that essentially does a conversion logic. | ||
| * | ||
| * @tparam S | ||
| * source type | ||
| * @tparam C | ||
| * cache. represented by `CachedSourceTypeInfo` for single-param generic types and | ||
| * `(CachedSourceTypeInfo, CachedSourceTypeInfo)` for two-param types. | ||
| * @tparam T | ||
| * target type | ||
| */ | ||
| private type ConversionFunction[S, C, T] = (S, C, ValueMapper) => T | ||
|
|
||
| /** | ||
| * A converter for single-parameter types, caching conversion functions. | ||
| * | ||
| * @param conv | ||
| * A function that defines the conversion logic using the cached `CachedSourceTypeInfo`. | ||
| */ | ||
| private final class Converter1[S, T]( | ||
| conv: ConversionFunction[S, CachedSourceTypeInfo, T] | ||
| ) extends Converter[S, T] { | ||
| private val s1 = new CachedSourceTypeInfo() | ||
| override def convert(value: S, valueMapper: ValueMapper): T = { | ||
| conv.apply(value, s1, valueMapper) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A converter for two-parameter types (e.g., Tuple2 or Map), caching conversion functions. | ||
| * | ||
| * @param conv | ||
| * A function that defines the conversion logic using two instances of `CachedSourceTypeInfo`. | ||
| */ | ||
| private final class Converter2[S, T]( | ||
| conv: ConversionFunction[ | ||
| S, | ||
| (CachedSourceTypeInfo, CachedSourceTypeInfo), | ||
| T | ||
| ] | ||
| ) extends Converter[S, T] { | ||
| private val s1 = new CachedSourceTypeInfo() | ||
| private val s2 = new CachedSourceTypeInfo() | ||
| override def convert(value: S, valueMapper: ValueMapper): T = { | ||
| conv.apply(value, (s1, s2), valueMapper) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A factory for creating converters based on parameterized types, supporting generic conversion. | ||
| * | ||
| * @param acceptSourceType | ||
| * Predicate to determine if the source type is acceptable. | ||
| * @param extractTypeParams | ||
| * Function to extract type parameters from the `ParameterizedType`. | ||
| * @param newConverter | ||
| * Function to create a new converter based on extracted type parameters. | ||
| */ | ||
| private final class ParametrizinglyTypedConverterFactory[T: ClassTag, TT]( | ||
| acceptSourceType: PClassInfo[?] => Boolean, | ||
| extractTypeParams: Type => Option[TT], | ||
| newConverter: TT => Converter[?, ?] | ||
| ) extends ConverterFactory { | ||
| private val targetClassTag: ClassTag[T] = implicitly | ||
|
|
||
| override def create( | ||
| sourceType: PClassInfo[?], | ||
| targetType: Type | ||
| ): Optional[Converter[?, ?]] = { | ||
| if (acceptSourceType(sourceType)) { | ||
| val targetClass = Reflection.toRawType(targetType) | ||
| if (targetClassTag.runtimeClass.isAssignableFrom(targetClass)) { | ||
| val typeParams = extractTypeParams( | ||
| Reflection.getExactSupertype(targetType, targetClass) | ||
| ) | ||
| typeParams.map(newConverter).toJava | ||
| } else { | ||
| Optional.empty() | ||
| } | ||
| } else { | ||
| Optional.empty() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Factory method for single-parameter types such as `List` or `Option`, using cached conversion. | ||
| * | ||
| * @param acceptSourceType | ||
| * Predicate to determine if the source type is acceptable. | ||
| * @param conv | ||
| * Conversion function applied to the value and cache. | ||
| */ | ||
| def forParametrizedType1[S, T: ClassTag]( | ||
| acceptSourceType: PClassInfo[?] => Boolean, | ||
| conv: Type => ConversionFunction[ | ||
| S, | ||
| CachedSourceTypeInfo, | ||
| T | ||
| ] | ||
| ): ConverterFactory = new ParametrizinglyTypedConverterFactory[T, Type]( | ||
| acceptSourceType, | ||
| _.params1, | ||
| t1 => new Converter1(conv(t1)) | ||
| ) | ||
|
|
||
| /** | ||
| * Factory method for two-parameter types such as `Map` or `Tuple2`, using cached conversion. | ||
| * | ||
| * @param acceptSourceType | ||
| * Predicate to determine if the source type is acceptable. | ||
| * @param conv | ||
| * Conversion function applied to the value and cache. | ||
| */ | ||
| def forParametrizedType2[S, T: ClassTag]( | ||
| acceptSourceType: PClassInfo[?] => Boolean, | ||
| conv: (Type, Type) => ConversionFunction[ | ||
| S, | ||
| (CachedSourceTypeInfo, CachedSourceTypeInfo), | ||
| T | ||
| ] | ||
| ): ConverterFactory = { | ||
| new ParametrizinglyTypedConverterFactory[T, (Type, Type)]( | ||
| acceptSourceType, | ||
| _.params2, | ||
| { case (t1, t2) => new Converter2(conv(t1, t2)) } | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.