|
| 1 | +/* |
| 2 | + * Copyright © 2025-2026 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 | + */ |
| 16 | +package org.pkl.config.scala.mapper |
| 17 | + |
| 18 | +import org.pkl.config.java.mapper.{Converter, ConverterFactory, Reflection, ValueMapper} |
| 19 | +import org.pkl.config.scala.mapper.JavaReflectionSyntaxExtensions.* |
| 20 | +import org.pkl.core.PClassInfo |
| 21 | + |
| 22 | +import java.lang.reflect.Type |
| 23 | +import java.util.Optional |
| 24 | +import scala.jdk.OptionConverters.RichOption |
| 25 | +import scala.reflect.ClassTag |
| 26 | + |
| 27 | +/** |
| 28 | + * Provides infrastructure that helps define custom converter factories in a somewhat concise way at |
| 29 | + * the same time utilizing caching. |
| 30 | + */ |
| 31 | +private[mapper] object CachedConverterFactories { |
| 32 | + |
| 33 | + /** |
| 34 | + * Function used in converters that essentially does a conversion logic. |
| 35 | + * |
| 36 | + * @tparam S |
| 37 | + * source type |
| 38 | + * @tparam C |
| 39 | + * cache. represented by `CachedSourceTypeInfo` for single-param generic types and |
| 40 | + * `(CachedSourceTypeInfo, CachedSourceTypeInfo)` for two-param types. |
| 41 | + * @tparam T |
| 42 | + * target type |
| 43 | + */ |
| 44 | + private type ConversionFunction[S, C, T] = (S, C, ValueMapper) => T |
| 45 | + |
| 46 | + /** |
| 47 | + * A converter for single-parameter types, caching conversion functions. |
| 48 | + * |
| 49 | + * @param conv |
| 50 | + * A function that defines the conversion logic using the cached `CachedSourceTypeInfo`. |
| 51 | + */ |
| 52 | + private final class Converter1[S, T]( |
| 53 | + conv: ConversionFunction[S, CachedSourceTypeInfo, T] |
| 54 | + ) extends Converter[S, T] { |
| 55 | + private val s1 = new CachedSourceTypeInfo() |
| 56 | + override def convert(value: S, valueMapper: ValueMapper): T = { |
| 57 | + conv.apply(value, s1, valueMapper) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * A converter for two-parameter types (e.g., Tuple2 or Map), caching conversion functions. |
| 63 | + * |
| 64 | + * @param conv |
| 65 | + * A function that defines the conversion logic using two instances of `CachedSourceTypeInfo`. |
| 66 | + */ |
| 67 | + private final class Converter2[S, T]( |
| 68 | + conv: ConversionFunction[ |
| 69 | + S, |
| 70 | + (CachedSourceTypeInfo, CachedSourceTypeInfo), |
| 71 | + T |
| 72 | + ] |
| 73 | + ) extends Converter[S, T] { |
| 74 | + private val s1 = new CachedSourceTypeInfo() |
| 75 | + private val s2 = new CachedSourceTypeInfo() |
| 76 | + override def convert(value: S, valueMapper: ValueMapper): T = { |
| 77 | + conv.apply(value, (s1, s2), valueMapper) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * A factory for creating converters based on parameterized types, supporting generic conversion. |
| 83 | + * |
| 84 | + * @param acceptSourceType |
| 85 | + * Predicate to determine if the source type is acceptable. |
| 86 | + * @param extractTypeParams |
| 87 | + * Function to extract type parameters from the `ParameterizedType`. |
| 88 | + * @param newConverter |
| 89 | + * Function to create a new converter based on extracted type parameters. |
| 90 | + */ |
| 91 | + private final class ParametrizinglyTypedConverterFactory[T: ClassTag, TT]( |
| 92 | + acceptSourceType: PClassInfo[?] => Boolean, |
| 93 | + extractTypeParams: Type => Option[TT], |
| 94 | + newConverter: TT => Converter[?, ?] |
| 95 | + ) extends ConverterFactory { |
| 96 | + private val targetClassTag: ClassTag[T] = implicitly |
| 97 | + |
| 98 | + override def create( |
| 99 | + sourceType: PClassInfo[?], |
| 100 | + targetType: Type |
| 101 | + ): Optional[Converter[?, ?]] = { |
| 102 | + if (acceptSourceType(sourceType)) { |
| 103 | + val targetClass = Reflection.toRawType(targetType) |
| 104 | + if (targetClassTag.runtimeClass.isAssignableFrom(targetClass)) { |
| 105 | + val typeParams = extractTypeParams( |
| 106 | + Reflection.getExactSupertype(targetType, targetClass) |
| 107 | + ) |
| 108 | + typeParams.map(newConverter).toJava |
| 109 | + } else { |
| 110 | + Optional.empty() |
| 111 | + } |
| 112 | + } else { |
| 113 | + Optional.empty() |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Factory method for single-parameter types such as `List` or `Option`, using cached conversion. |
| 120 | + * |
| 121 | + * @param acceptSourceType |
| 122 | + * Predicate to determine if the source type is acceptable. |
| 123 | + * @param conv |
| 124 | + * Conversion function applied to the value and cache. |
| 125 | + */ |
| 126 | + def forParametrizedType1[S, T: ClassTag]( |
| 127 | + acceptSourceType: PClassInfo[?] => Boolean, |
| 128 | + conv: Type => ConversionFunction[ |
| 129 | + S, |
| 130 | + CachedSourceTypeInfo, |
| 131 | + T |
| 132 | + ] |
| 133 | + ): ConverterFactory = new ParametrizinglyTypedConverterFactory[T, Type]( |
| 134 | + acceptSourceType, |
| 135 | + _.params1, |
| 136 | + t1 => new Converter1(conv(t1)) |
| 137 | + ) |
| 138 | + |
| 139 | + /** |
| 140 | + * Factory method for two-parameter types such as `Map` or `Tuple2`, using cached conversion. |
| 141 | + * |
| 142 | + * @param acceptSourceType |
| 143 | + * Predicate to determine if the source type is acceptable. |
| 144 | + * @param conv |
| 145 | + * Conversion function applied to the value and cache. |
| 146 | + */ |
| 147 | + def forParametrizedType2[S, T: ClassTag]( |
| 148 | + acceptSourceType: PClassInfo[?] => Boolean, |
| 149 | + conv: (Type, Type) => ConversionFunction[ |
| 150 | + S, |
| 151 | + (CachedSourceTypeInfo, CachedSourceTypeInfo), |
| 152 | + T |
| 153 | + ] |
| 154 | + ): ConverterFactory = { |
| 155 | + new ParametrizinglyTypedConverterFactory[T, (Type, Type)]( |
| 156 | + acceptSourceType, |
| 157 | + _.params2, |
| 158 | + { case (t1, t2) => new Converter2(conv(t1, t2)) } |
| 159 | + ) |
| 160 | + } |
| 161 | +} |
0 commit comments