Skip to content

Commit

Permalink
Make the tuple logic more targeted.
Browse files Browse the repository at this point in the history
  • Loading branch information
yilinwei committed Dec 20, 2023
1 parent 27002fa commit cf63cd3
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,24 @@ private[focus] trait SelectParserBase extends ParserBase {
case None => FocusError.NotAConcreteClass(tpe.show).asResult
}

import scala.util.matching.Regex

private val tupleFieldPattern = "^_[0-9]+$".r

def getFieldType(fromType: TypeRepr, fieldName: String, pos: Position): FocusResult[TypeRepr] = {
// We need to do this to support tuples, because even though they conform as case classes in other respects,
// for some reason their field names (_1, _2, etc) have a space at the end, ie `_1 `.
def getTrimmedFieldSymbol(fromTypeSymbol: Symbol): Symbol =
fromTypeSymbol.fieldMembers.find(_.name.trim == fieldName).getOrElse(Symbol.noSymbol)
def getFieldSymbol(fromTypeSymbol: Symbol): Symbol = {
// We need to do this to support tuples, because even though they conform as case classes in other respects,
// for some reason their field names (_1, _2, etc) have a space at the end, ie `_1 `.
val f: String => String =
if (fromType <:< TypeRepr.of[Tuple])
s => if (tupleFieldPattern.matches(fieldName)) s.trim else s
else
identity
fromTypeSymbol.fieldMembers.find(s => f(s.name) == fieldName).getOrElse(Symbol.noSymbol)
}

getClassSymbol(fromType).flatMap { fromTypeSymbol =>
getTrimmedFieldSymbol(fromTypeSymbol) match {
getFieldSymbol(fromTypeSymbol) match {
case FieldType(possiblyTypeArg) => Right(swapWithSuppliedType(fromType, possiblyTypeArg))
case _ => FocusError.CouldntFindFieldType(fromType.show, fieldName, pos).asResult
}
Expand Down

0 comments on commit cf63cd3

Please sign in to comment.