Skip to content

Commit

Permalink
Get annotations of subclass fieldMembers too and flatten everything
Browse files Browse the repository at this point in the history
should do that for the class annotations too
  • Loading branch information
Grifs committed Nov 14, 2024
1 parent 7eb9a8f commit 2cd2130
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
49 changes: 30 additions & 19 deletions src/main/scala/io/viash/helpers/Mirroring.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ inline def internalFunctionalityFieldsOf[T]: List[String] = ${ internalFunctiona
inline def deprecatedFieldsOf[T]: Vector[(String, String, String, String)] = ${ deprecatedFieldsOfImpl[T] }
inline def removedFieldsOf[T]: Vector[(String, String, String, String)] = ${ removedFieldsOfImpl[T] }
inline def annotationsOf[T]: List[(String, List[String])] = ${ annotationsOfImpl[T] }
inline def membersOf[T]: List[String] = ${ membersOfImpl[T] }
// inline def membersOf[T]: List[String] = ${ membersOfImpl[T] }
inline def memberTypeAnnotationsOf[T]: List[(String, String, List[(String, List[String])])] = ${ memberTypeAnnotationsOfImpl[T] }
inline def historyOf[T]: List[String] = ${ historyOfImpl[T] }

Expand Down Expand Up @@ -181,23 +181,22 @@ def annotationsOfImpl[T: Type](using Quotes): Expr[List[(String, List[String])]]
values
}

// TODO get annotations from base classes and flatten
val annots = tpe.annotations
.filter(_.tpe.typeSymbol.fullName.startsWith("io.viash"))
.map(ann => (ann.tpe.typeSymbol.name, annotationToStrings(ann)))

Expr(annots)

def membersOfImpl[T: Type](using Quotes): Expr[List[String]] = {
import quotes.reflect.*
val tpe = TypeRepr.of[T].typeSymbol
val memberSymbols = tpe.fieldMembers.map(_.name)
Expr(memberSymbols)
}
// def membersOfImpl[T: Type](using Quotes): Expr[List[String]] = {
// import quotes.reflect.*
// val tpe = TypeRepr.of[T].typeSymbol
// val memberSymbols = tpe.caseFields.map(_.name)
// Expr(memberSymbols)
// }

def memberTypeAnnotationsOfImpl[T: Type](using Quotes): Expr[List[(String, String, List[(String, List[String])])]] = {
import quotes.reflect.*
val tpe = TypeRepr.of[T].typeSymbol
val memberSymbols = tpe.fieldMembers

def unfinishedStringStripMargin(s: String, marginChar: Char = '|'): String = {
s.replaceAll("\\\\n", "\n").stripMargin(marginChar)
Expand Down Expand Up @@ -243,18 +242,30 @@ def memberTypeAnnotationsOfImpl[T: Type](using Quotes): Expr[List[(String, Strin
values
}

val annots =
memberSymbols
.map{ case m =>
val name = m.name
val mTpe = "foo"
val annotations = m.annotations
.filter(_.tpe.typeSymbol.fullName.startsWith("io.viash"))
.map(ann => (ann.tpe.typeSymbol.name, annotationToStrings(ann)))
(name, mTpe, annotations)
val tpe = TypeRepr.of[T].typeSymbol
val baseClasses = TypeRepr.of[T].baseClasses.filter(_.fullName.startsWith("io.viash"))

// base classes don't have case fields, so we need to get the member fields from the base classes and filter them
val caseFieldNames = tpe.caseFields.map(_.name)

val annots =
baseClasses
.map{ case bc =>
bc.fieldMembers
.filter(m => caseFieldNames.contains(m.name))
.map(m =>
val name = m.name
val mTpe = "foo"
val annotations = m.annotations
.filter(_.tpe.typeSymbol.fullName.startsWith("io.viash"))
.map(ann => (ann.tpe.typeSymbol.name, annotationToStrings(ann)))
(name, mTpe, annotations)
)
}
// flatten the list of lists by name
val annotsFlattened = annots.flatten.groupBy(_._1).map{ case (k, v) => (k, v.head._2, v.flatMap(_._3)) }.toList

Expr(annots)
Expr(annotsFlattened)
}

def historyOfImpl[T: Type](using Quotes): Expr[List[String]] = {
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/io/viash/schema/SchemaTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SchemaTest extends AnyFunSuite with BeforeAndAfterAll with PrivateMethodTe
case _ => ()
}

assert(nonAnnotated.size == 9) // CollectedSchemas has 9 members, all of them unannotated
assert(nonAnnotated.size == 10) // CollectedSchemas has 9 members, all of them unannotated + 1 __this__ member
}

test("Check formatting of deprecation annotations") {
Expand Down

0 comments on commit 2cd2130

Please sign in to comment.