Created
February 26, 2020 03:54
-
-
Save ugdark/ecd8a84c70f747493f3e9668237258cf to your computer and use it in GitHub Desktop.
jsonのfieldが23個以上どうするんだ?エラーは1つではなく複数返したい。
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
import cats.data.Validated.{Invalid, Valid} | |
import cats.syntax.apply._ | |
import io.circe.generic.semiauto.deriveEncoder | |
import io.circe.parser.decodeAccumulating | |
import io.circe.{Encoder, _} | |
import io.tabmo.circe.extra.rules.IntRules | |
import io.tabmo.json.rules._ | |
/** | |
* 下記の方法はfield 22個まででの対応方法がわかんない。複数のErrorが帰る | |
* forで書き換えるとfail-fastで1つのエラーが発見されると終了になってしまう。 | |
*/ | |
object Test { | |
case class Large( | |
a: Int, | |
b: Int, | |
c: Int, | |
d: Int, | |
e: Int, | |
f: Int, | |
g: Int, | |
h: Int, | |
i: Int, | |
j: Int, | |
k: Int, | |
l: Int, | |
m: Int, | |
n: Int, | |
o: Int, | |
p: Int, | |
q: Int, | |
r: Int, | |
s: Int, | |
t: Int, | |
u: Int, | |
v: Int, | |
// w: Int, | |
// x: Int, | |
// y: Int, | |
// z: Int | |
) | |
implicit val encoder: Encoder[Large] = deriveEncoder[Large] | |
implicit val decoder: Decoder[Large] = ( | |
Decoder.instance(_.downField("a").read(IntRules.positive())), | |
Decoder.instance(_.downField("b").read(IntRules.positive())), | |
Decoder.instance(_.downField("c").read(IntRules.positive())), | |
Decoder.instance(_.downField("d").read(IntRules.positive())), | |
Decoder.instance(_.downField("e").read(IntRules.positive())), | |
Decoder.instance(_.downField("f").read(IntRules.positive())), | |
Decoder.instance(_.downField("g").read(IntRules.positive())), | |
Decoder.instance(_.downField("h").read(IntRules.positive())), | |
Decoder.instance(_.downField("i").read(IntRules.positive())), | |
Decoder.instance(_.downField("j").read(IntRules.positive())), | |
Decoder.instance(_.downField("k").read(IntRules.positive())), | |
Decoder.instance(_.downField("l").read(IntRules.positive())), | |
Decoder.instance(_.downField("m").read(IntRules.positive())), | |
Decoder.instance(_.downField("n").read(IntRules.positive())), | |
Decoder.instance(_.downField("o").read(IntRules.positive())), | |
Decoder.instance(_.downField("p").read(IntRules.positive())), | |
Decoder.instance(_.downField("q").read(IntRules.positive())), | |
Decoder.instance(_.downField("r").read(IntRules.positive())), | |
Decoder.instance(_.downField("s").read(IntRules.positive())), | |
Decoder.instance(_.downField("t").read(IntRules.positive())), | |
Decoder.instance(_.downField("u").read(IntRules.positive())), | |
Decoder.instance(_.downField("v").read(IntRules.positive())) | |
).mapN(Large.apply) | |
def main(args: Array[String]): Unit = { | |
val text = """{"a":"a", "b":"b", "c":3, "d":4, "e":5, "f":6, "g":7, "h":8, "i":9, "j":10, "k":11, "l":12, "m":13, "n":14, "o":15, "p":16, "q":17, "r":18, "s":19, "t":20, "u":21, "v":22}""" | |
val result = decodeAccumulating(text)(decoder) match { | |
case Valid(value) => Right(value) | |
case Invalid(errors) => | |
Left(errors) | |
} | |
// "a":"a", "b":"b"で文字2つのエラー | |
println(result.left.get.size == 2) | |
println(result) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment