Skip to content

Commit 9ffad63

Browse files
author
Andriy Onyshchuk
committed
fix style
1 parent 95782ad commit 9ffad63

File tree

6 files changed

+166
-72
lines changed

6 files changed

+166
-72
lines changed

pkl-config-scala/src/main/java/org/pkl/config/scala/annotation/EnumOwner.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.pkl.config.scala.annotation;
217

318
import java.lang.annotation.*;
@@ -7,5 +22,4 @@
722
public @interface EnumOwner {
823

924
Class<? extends scala.Enumeration> value();
10-
1125
}

pkl-config-scala/src/main/scala/org/pkl/config/scala/mapper/JavaReflectionSyntaxExtensions.scala

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -75,33 +75,36 @@ private[mapper] object JavaReflectionSyntaxExtensions {
7575
case _ => None
7676
}
7777

78-
/**
79-
* Attempts to recover the full list of enumeration values from a given runtime class.
80-
*
81-
* This method is designed to work with Scala 2 `Enumeration` values that were defined
82-
* using a custom subclass of `Enumeration.Val`, annotated with `@EnumOwner`, where the
83-
* annotation holds a reference to the singleton `Enumeration` object.
84-
*
85-
* The method checks whether the provided `Type` is a subclass of
86-
* `Enumeration#Value`, and if so, attempts to locate the `@EnumOwner` annotation on its class.
87-
* If present, it uses reflection to access the singleton `Enumeration` instance and returns
88-
* its list of values.
89-
*
90-
* @example
91-
* {{{
92-
* object SimpleEnum extends Enumeration {
93-
*
94-
* @EnumOwner(classOf[SimpleEnum.type])
95-
* case class V() extends Val(nextId)
96-
*
97-
* val Aaa = V()
98-
* val Bbb = V()
99-
* val Ccc = V()
100-
* }
101-
* }}}
102-
*
103-
* @return Some(list of `Enumeration#Value`) if the enumeration can be resolved, None otherwise
104-
*/
78+
/** Attempts to recover the full list of enumeration values from a given
79+
* runtime class.
80+
*
81+
* This method is designed to work with Scala 2 `Enumeration` values that
82+
* were defined using a custom subclass of `Enumeration.Val`, annotated
83+
* with `@EnumOwner`, where the annotation holds a reference to the
84+
* singleton `Enumeration` object.
85+
*
86+
* The method checks whether the provided `Type` is a subclass of
87+
* `Enumeration#Value`, and if so, attempts to locate the `@EnumOwner`
88+
* annotation on its class. If present, it uses reflection to access the
89+
* singleton `Enumeration` instance and returns its list of values.
90+
*
91+
* @example
92+
* {{{
93+
* object SimpleEnum extends Enumeration {
94+
*
95+
* @EnumOwner(classOf[SimpleEnum.type])
96+
* case class V() extends Val(nextId)
97+
*
98+
* val Aaa = V()
99+
* val Bbb = V()
100+
* val Ccc = V()
101+
* }
102+
* }}}
103+
*
104+
* @return
105+
* Some(list of `Enumeration#Value`) if the enumeration can be resolved,
106+
* None otherwise
107+
*/
105108
def asCustomEnum: Option[List[Enumeration#Value]] = {
106109

107110
def derive(enumClass: Class[_]): Option[List[Enumeration#Value]] = {
@@ -115,14 +118,14 @@ private[mapper] object JavaReflectionSyntaxExtensions {
115118
case _: Throwable => None
116119
}
117120
}
118-
121+
119122
x match {
120123
case x: Class[_] if classOf[Enumeration#Value].isAssignableFrom(x) =>
121124
for {
122125
anno <- Option(x.getAnnotation(classOf[EnumOwner]))
123126
enum <- derive(anno.value())
124127
} yield enum
125-
128+
126129
case _ =>
127130
None
128131
}
Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.pkl.config.scala.mapper
217

318
import org.pkl.config.java.mapper.{Converter, ConverterFactory, ValueMapper}
@@ -11,24 +26,34 @@ import scala.jdk.OptionConverters._
1126

1227
private[mapper] object PStringOrIntToEnumeration extends ConverterFactory {
1328

14-
override def create(sourceType: PClassInfo[_], targetType: Type): Optional[Converter[_, _]] = {
15-
targetType.asCustomEnum
16-
.map { members =>
17-
(new Converter[Any, Any] {
18-
override def convert(value: Any, valueMapper: ValueMapper): Any = {
19-
val res = value match {
20-
case i: Long => members.collectFirst {
29+
override def create(
30+
sourceType: PClassInfo[_],
31+
targetType: Type
32+
): Optional[Converter[_, _]] = {
33+
targetType.asCustomEnum.map { members =>
34+
(new Converter[Any, Any] {
35+
override def convert(value: Any, valueMapper: ValueMapper): Any = {
36+
val res = value match {
37+
case i: Long =>
38+
members.collectFirst {
2139
case value if value.id == i => value
2240
}
23-
case name: String => members.collectFirst {
24-
case value if { val n = value.toString; n == name || CodeGeneratorUtils.toEnumConstantName(n).equals(name) } => value
41+
case name: String =>
42+
members.collectFirst {
43+
case value if {
44+
val n = value.toString;
45+
n == name || CodeGeneratorUtils
46+
.toEnumConstantName(n)
47+
.equals(name)
48+
} =>
49+
value
2550
}
26-
case _ => None
27-
}
28-
29-
res.orNull
51+
case _ => None
3052
}
31-
}).asInstanceOf[Converter[_, _]]
32-
}.toJava
53+
54+
res.orNull
55+
}
56+
}).asInstanceOf[Converter[_, _]]
57+
}.toJava
3358
}
3459
}

pkl-config-scala/src/main/scala/org/pkl/config/scala/mapper/ScalaConverterFactories.scala

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
*/
1616
package org.pkl.config.scala.mapper
1717

18-
import org.pkl.config.java.mapper.{ConverterFactory, PObjectToDataObject, ValueMapper}
18+
import org.pkl.config.java.mapper.{
19+
ConverterFactory,
20+
PObjectToDataObject,
21+
ValueMapper
22+
}
1923
import org.pkl.config.scala.mapper.JavaReflectionSyntaxExtensions.ParametrizedTypeSyntaxExtension
2024
import org.pkl.core.util.CodeGeneratorUtils
2125
import org.pkl.core.{PClassInfo, PNull, PObject, Pair}
@@ -29,22 +33,22 @@ import scala.jdk.OptionConverters._
2933
import scala.language.implicitConversions
3034

3135
/** Defines a set of PKL to Scala converter factories.
32-
*/
36+
*/
3337
object ScalaConverterFactories {
3438

3539
private type Conv1[S, T] = Type => (S, CachedSourceTypeInfo, ValueMapper) => T
3640

3741
private type Conv2[S, T] = (Type, Type) => (
38-
S,
42+
S,
3943
(CachedSourceTypeInfo, CachedSourceTypeInfo),
4044
ValueMapper
41-
) => T
45+
) => T
4246

4347
val pObjectToCaseClass: ConverterFactory = new PObjectToDataObject {
4448

4549
override def selectConstructor(
46-
clazz: Class[_]
47-
): Optional[Constructor[_]] = {
50+
clazz: Class[_]
51+
): Optional[Constructor[_]] = {
4852
clazz.getDeclaredConstructors.headOption
4953
.filter(_ =>
5054
// case classes all implement Product
@@ -62,9 +66,9 @@ object ScalaConverterFactories {
6266
(value, s1, vm) => {
6367
value match {
6468
case _: PNull | null => None
65-
case v: Option[_] => v.map(s1.updateAndGet(_, t1, vm))
66-
case v: Optional[_] => v.toScala.map(s1.updateAndGet(_, t1, vm))
67-
case v => Option(s1.updateAndGet(v, t1, vm))
69+
case v: Option[_] => v.map(s1.updateAndGet(_, t1, vm))
70+
case v: Optional[_] => v.toScala.map(s1.updateAndGet(_, t1, vm))
71+
case v => Option(s1.updateAndGet(v, t1, vm))
6872
}
6973
}
7074
)
@@ -91,8 +95,8 @@ object ScalaConverterFactories {
9195
}
9296

9397
def pCollectionToMutableCollectionConv[T[_]](
94-
toSpecific: IterableOnce[_] => T[_]
95-
): Conv1[java.util.Collection[_], T[_]] =
98+
toSpecific: IterableOnce[_] => T[_]
99+
): Conv1[java.util.Collection[_], T[_]] =
96100
t1 =>
97101
(value, cache, vm) =>
98102
toSpecific(value.asScala.map(x => cache.updateAndGet(x, t1, vm)))
@@ -204,7 +208,7 @@ object ScalaConverterFactories {
204208
)
205209

206210
val pCollectionToImmutableLazyList
207-
: ConverterFactory = CachedConverterFactories
211+
: ConverterFactory = CachedConverterFactories
208212
.forParametrizedType1[java.util.Collection[_], immutable.LazyList[_]](
209213
x =>
210214
x == PClassInfo.Collection | x == PClassInfo.Set | x == PClassInfo.List,
@@ -237,6 +241,6 @@ object ScalaConverterFactories {
237241
)
238242

239243
private implicit def pClassInfoToPredicate(
240-
x: PClassInfo[_]
241-
): PClassInfo[_] => Boolean = _ == x
244+
x: PClassInfo[_]
245+
): PClassInfo[_] => Boolean = _ == x
242246
}

pkl-config-scala/src/test/scala/org/pkl/config/scala/ScalaObjectMapperSpec.scala

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.pkl.config.scala
217

318
import org.pkl.config.java.ConfigEvaluator
@@ -20,7 +35,7 @@ class ScalaObjectMapperSpec extends AnyFunSuite {
2035
import ScalaObjectMapperSpec._
2136

2237
test("evaluate scala types") {
23-
38+
2439
val code =
2540
"""
2641
|module ObjectMappingTestContainer
@@ -186,12 +201,12 @@ object ScalaObjectMapperSpec {
186201
object SimpleEnum extends Enumeration {
187202
@EnumOwner(classOf[SimpleEnum.type])
188203
case class V() extends Val(nextId)
189-
204+
190205
val Aaa = V()
191206
val Bbb = V()
192207
val Ccc = V()
193208
}
194-
209+
195210
case class ObjectMappingTestContainer(
196211
// Options
197212
optionalVal1: Option[String],
@@ -247,14 +262,18 @@ object ScalaObjectMapperSpec {
247262
// Map & Mapping with structured keys
248263
intListingStringMapping: Map[List[Int], String],
249264
intSetListStringMapping: Map[List[Set[Int]], String],
250-
thisOneGoesToEleven: Map[List[Set[Int]], Map[List[Int], Map[Int, String]]],
265+
thisOneGoesToEleven: Map[
266+
List[Set[Int]],
267+
Map[List[Int], Map[Int, String]]
268+
],
251269
// enums
252270
simpleEnumViaString: SimpleEnum.V,
253271
simpleEnumViaInt: SimpleEnum.V
254272
)
255273

256274
object ObjectMappingTestContainer {
257275
implicit def anyDiffx[T]: Diff[T] = Diff.useEquals[T]
258-
implicit val diffx: Diff[ObjectMappingTestContainer] = Diff.derived[ObjectMappingTestContainer]
276+
implicit val diffx: Diff[ObjectMappingTestContainer] =
277+
Diff.derived[ObjectMappingTestContainer]
259278
}
260279
}

0 commit comments

Comments
 (0)