From 6896bae4313e7919152f5758419136df38f6b2d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Fromentin?= Date: Wed, 11 Sep 2024 17:55:30 +0200 Subject: [PATCH] dev: Add BigInt support --- .../io/github/iltotore/iron/macros/ReflectUtil.scala | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/main/src/io/github/iltotore/iron/macros/ReflectUtil.scala b/main/src/io/github/iltotore/iron/macros/ReflectUtil.scala index 188ff0d..9061bed 100644 --- a/main/src/io/github/iltotore/iron/macros/ReflectUtil.scala +++ b/main/src/io/github/iltotore/iron/macros/ReflectUtil.scala @@ -182,7 +182,8 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q): private val enhancedDecoders: Map[TypeRepr, (Term, Map[String, ?]) => Either[DecodingFailure, ?]] = Map( TypeRepr.of[Boolean] -> decodeBoolean, - TypeRepr.of[String] -> decodeString + TypeRepr.of[BigInt] -> decodeBigInt, + TypeRepr.of[String] -> decodeString ) /** @@ -325,3 +326,11 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q): case (leftResult, rightResult) => Left(DecodingFailure.StringPartsNotInlined(List(leftResult, rightResult))) case _ => Left(DecodingFailure.Unknown) + + def decodeBigInt(term: Term, definitions: Map[String, ?]): Either[DecodingFailure, BigInt] = + term match + case Apply(Select(Ident("BigInt"), "apply"), List(value)) => + if value.tpe <:< TypeRepr.of[Int] then decodeTerm[Int](value, definitions).map(BigInt.apply) + else if value.tpe <:< TypeRepr.of[Long] then decodeTerm[Long](value, definitions).map(BigInt.apply) + else Left(DecodingFailure.Unknown) + case _ => Left(DecodingFailure.Unknown)