-
Notifications
You must be signed in to change notification settings - Fork 393
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
base: main
Are you sure you want to change the base?
PKL Config Scala #1076
Changes from all commits
c8a7a01
5916ca9
aecbe2b
446c417
86338ca
450fd94
c53d504
4fdb3fa
7a61d35
f23299b
acbd409
1d5634f
9c04e82
b8cac9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| 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("-release:${buildInfo.jvmTarget}") | ||
| } | ||
|
|
||
| tasks.test { | ||
| useJUnitPlatform { | ||
| includeEngines("scalatest") | ||
| testLogging { events("passed", "skipped", "failed") } | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessarily blocking this PR: if our goal is to support both Scala 2.13 and Scala 3, we should be testing the library from both targets.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we should produce scala 3.. so at this point we can lock at |
||
|
|
||
| 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 ", | ||
| ) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,33 @@ | ||||||
| /* | ||||||
| * 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) } | ||||||
|
|
||||||
| 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.") | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a nice point to discuss
what do you think? |
||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| 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)) } | ||
| ) | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.