-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolution of Scala 2 vs 3 reflection
- Loading branch information
1 parent
2e8c126
commit 627642d
Showing
5 changed files
with
81 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 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
69 changes: 69 additions & 0 deletions
69
openai-core/src/main/scala-3/io/cequence/openaiscala/service/ReflectionUtil.scala
This file contains 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,69 @@ | ||
package io.cequence.openaiscala.service | ||
|
||
import scala.quoted._ | ||
|
||
object ReflectionUtil { | ||
|
||
class InfixOp[T](using q: Quotes, val typ: Type[T]) { | ||
|
||
import q.reflect.* // Import the reflection API | ||
|
||
private val typeRepr: TypeRepr = TypeRepr.of[T] | ||
private val typeSymbol = typeRepr.typeSymbol | ||
|
||
private val optionInnerType: Option[TypeRepr] = | ||
if (typeRepr <:< TypeRepr.of[Option[_]]) | ||
Some(typeRepr.typeArgs.head) | ||
else | ||
None | ||
|
||
def matches(types: Type[_]*): Boolean = | ||
types.exists { candidateType => | ||
val candidateRepr = TypeRepr.of(using candidateType) | ||
typeRepr =:= candidateRepr || (optionInnerType.isDefined && optionInnerType.get =:= candidateRepr) | ||
} | ||
|
||
def subMatches(types: Type[_]*): Boolean = | ||
types.exists { candidateType => | ||
val candidateRepr = TypeRepr.of(using candidateType) | ||
typeRepr <:< candidateRepr || (optionInnerType.isDefined && optionInnerType.get <:< candidateRepr) | ||
} | ||
|
||
def isOption(): Boolean = | ||
typeRepr <:< TypeRepr.of[Option[_]] | ||
|
||
def isCaseClass(): Boolean = { | ||
typeSymbol.isClassDef && typeSymbol.flags.is(Flags.Case) | ||
} | ||
|
||
def getCaseClassFields(): List[(String, Type[_])] = { | ||
import q.reflect.* | ||
|
||
// Ensure it's a case class | ||
if (isCaseClass()) { | ||
// Collect case accessor fields | ||
typeSymbol.caseFields.map { field => | ||
val fieldName = field.name | ||
|
||
val fieldTypeRepr = field.tree match { | ||
case v: ValDef => v.tpt.tpe // Extract the type of the field | ||
} | ||
|
||
// Convert TypeRepr to Type[_] | ||
val fieldType = fieldTypeRepr.asType match { | ||
case '[t] => Type.of[t] // Convert TypeRepr to Type[_] | ||
} | ||
|
||
(fieldName, fieldType) | ||
} | ||
} else { | ||
List.empty // Not a case class, return empty list | ||
} | ||
} | ||
} | ||
|
||
def shortName(symbol: Symbol): String = { | ||
val paramFullName = symbol.name | ||
paramFullName.substring(paramFullName.lastIndexOf('.') + 1, paramFullName.length) | ||
} | ||
} |
File renamed without changes.