-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Scala 2.13 and update circe to 0.13.0 (dropping Scala…
… 2.11)
- Loading branch information
Alex Zolotko
committed
Jul 29, 2020
1 parent
1a5e0bd
commit 35898c8
Showing
18 changed files
with
202 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package argus.macros | ||
|
||
import scala.reflect.api.Universe | ||
|
||
private[macros] object AnyDecoder { | ||
def anyDecoder[U <: Universe](u: U): u.Tree = { | ||
import u._ | ||
q""" | ||
def anyDecoder: Decoder[Any] = Decoder.instance((h: HCursor) => h.focus.get match { | ||
case n if n.isNull => null | ||
case n if n.isNumber => n.as[Double] | ||
case b if b.isBoolean => b.as[Boolean] | ||
case s if s.isString => s.as[String] | ||
case o if o.isObject => o.as[Map[String, Any]](Decoder.decodeMapLike(KeyDecoder.decodeKeyString, anyDecoder, Map.canBuildFrom)) | ||
case a if a.isArray => a.as[List[Any]](Decoder.decodeIterable(anyDecoder, List.canBuildFrom[Any])) | ||
}) | ||
""" | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
argus/src/main/scala-2.12/argus/macros/ParamsASTHelper.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,40 @@ | ||
package argus.macros | ||
|
||
import scala.language.experimental.macros | ||
import scala.reflect.api.Universe | ||
|
||
private[macros] object ParamsASTHelper { | ||
def paramsToMap[U <: Universe](u: U)( | ||
nameAndDefaults: List[(String, Any)], | ||
params: List[u.Tree]): Map[String, Any] = { | ||
import u._ | ||
|
||
def toValue(param: Tree): Any = param match { | ||
case Ident(TermName("None")) => None | ||
case Apply(Ident(TermName("Some")), List(Literal(Constant(value)))) => | ||
Some(value) | ||
case Literal(Constant(c)) => c | ||
case _ => param | ||
} | ||
|
||
// Handle positional vs. named argument separately | ||
val (positional, named) = params splitAt (params prefixLength { | ||
case AssignOrNamedArg(Ident(TermName(name)), _) => false; case _ => true | ||
}) | ||
|
||
require(positional.length <= nameAndDefaults.length, | ||
"More position arguments than specified in: " + nameAndDefaults) | ||
val posValues = nameAndDefaults zip positional map { | ||
case ((name, default), param) => | ||
(name, toValue(param)) | ||
} toMap | ||
|
||
val namedValues = named map { | ||
case AssignOrNamedArg(Ident(TermName(name)), t: Tree) => | ||
(name, toValue(t)) | ||
} toMap | ||
|
||
nameAndDefaults.toMap ++ posValues ++ namedValues | ||
} | ||
|
||
} |
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,19 @@ | ||
package argus.macros | ||
|
||
import scala.reflect.api.Universe | ||
|
||
private[macros] object AnyDecoder { | ||
def anyDecoder[U <: Universe](u: U): u.Tree = { | ||
import u._ | ||
q""" | ||
def anyDecoder: Decoder[Any] = Decoder.instance((h: HCursor) => h.focus.get match { | ||
case n if n.isNull => null | ||
case n if n.isNumber => n.as[Double] | ||
case b if b.isBoolean => b.as[Boolean] | ||
case s if s.isString => s.as[String] | ||
case o if o.isObject => o.as[Map[String, Any]](Decoder.decodeMapLike(KeyDecoder.decodeKeyString, anyDecoder, Map)) | ||
case a if a.isArray => a.as[List[Any]](Decoder.decodeIterable(anyDecoder, List)) | ||
}) | ||
""" | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
argus/src/main/scala-2.13/argus/macros/ParamsASTHelper.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,46 @@ | ||
package argus.macros | ||
|
||
import scala.language.experimental.macros | ||
import scala.reflect.api.Universe | ||
|
||
private[macros] object ParamsASTHelper { | ||
|
||
def paramsToMap[U <: Universe](u: U)( | ||
nameAndDefaults: List[(String, Any)], | ||
params: List[u.Tree]): Map[String, Any] = { | ||
import u._ | ||
|
||
def toValue(param: Tree): Any = param match { | ||
case Ident(TermName("None")) => None | ||
case Apply(Ident(TermName("Some")), List(Literal(Constant(value)))) => | ||
Some(value) | ||
case Literal(Constant(c)) => c | ||
case _ => param | ||
} | ||
|
||
// Handle positional vs. named argument separately | ||
val (positional, named) = params.splitAt(params.segmentLength { | ||
case NamedArg(Ident(TermName(_)), _) => false | ||
case Assign(Ident(TermName(_)), _) => false | ||
case _ => true | ||
}) | ||
|
||
require(positional.length <= nameAndDefaults.length, | ||
"More position arguments than specified in: " + nameAndDefaults) | ||
val posValues = nameAndDefaults | ||
.zip(positional) | ||
.map { | ||
case ((name, default), param) => | ||
(name, toValue(param)) | ||
} | ||
.toMap | ||
|
||
val namedValues = named.map { | ||
case NamedArg(Ident(TermName(name)), t: Tree) => (name, toValue(t)) | ||
case Assign(Ident(TermName(name)), t: Tree) => (name, toValue(t)) | ||
}.toMap | ||
|
||
nameAndDefaults.toMap ++ posValues ++ namedValues | ||
} | ||
|
||
} |
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
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
6 changes: 4 additions & 2 deletions
6
argus/src/test/scala/argus/macros/CirceCodecBuilderSpec.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
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
Oops, something went wrong.