• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TouK / nussknacker / 5976637142

25 Aug 2023 01:43PM UTC coverage: 81.47% (+0.03%) from 81.438%
5976637142

push

github

Filemon279
Fix migration in 1.11

25 of 25 new or added lines in 2 files covered. (100.0%)

14865 of 18246 relevant lines covered (81.47%)

5.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/common-api/src/main/scala/pl/touk/nussknacker/engine/api/CirceUtil.scala
1
package pl.touk.nussknacker.engine.api
2

3
import io.circe
4
import io.circe.generic.extras.Configuration
5
import io.circe._
6

7
import java.net.{URI, URL}
8
import java.nio.charset.StandardCharsets
9
import scala.annotation.tailrec
10
import scala.jdk.CollectionConverters._
11

12
object CirceUtil {
13

14
  implicit val configuration: Configuration = Configuration
15
    .default
16
    .withDefaults
17
    .withDiscriminator("type")
58✔
18

19

20
  def decodeJson[T: Decoder](json: String): Either[circe.Error, T] =
21
    io.circe.parser.parse(json).flatMap(Decoder[T].decodeJson)
38✔
22

23
  def decodeJson[T: Decoder](json: Array[Byte]): Either[circe.Error, T] =
24
    decodeJson(new String(json, StandardCharsets.UTF_8))
18✔
25

26
  def decodeJsonUnsafe[T: Decoder](json: String): T = unsafe(decodeJson(json))
2✔
27

28
  def decodeJsonUnsafe[T: Decoder](json: String, message: String): T = unsafe(decodeJson(json), message)
22✔
29

30
  def decodeJsonUnsafe[T: Decoder](json: Array[Byte]): T = unsafe(decodeJson(json))
18✔
31

32
  def decodeJsonUnsafe[T: Decoder](json: Array[Byte], message: String): T = unsafe(decodeJson(json), message)
×
33

34
  def decodeJsonUnsafe[T: Decoder](json: Json): T = unsafe(Decoder[T].decodeJson(json))
12✔
35

36
  def decodeJsonUnsafe[T: Decoder](json: Json, message: String): T = unsafe(Decoder[T].decodeJson(json), message)
4✔
37

38
  private def unsafe[T](result: Either[circe.Error, T], message: String = "") = result match {
39
    case Left(error) => throw DecodingError(s"Failed to decode${if (message.isBlank) "" else s" - $message"}, error: ${error.getMessage}", error)
2✔
40
    case Right(data) => data
36✔
41
  }
42

43
  case class DecodingError(message: String, ex: Throwable) extends IllegalArgumentException(message, ex)
44

45
  object codecs {
46

47
    implicit def jMapEncoder[K: KeyEncoder, V: Encoder]: Encoder[java.util.Map[K, V]] = Encoder[Map[K, V]].contramap(_.asScala.toMap)
6✔
48

49
    implicit lazy val uriDecoder: Decoder[URI] = Decoder.decodeString.map(URI.create)
50
    implicit lazy val uriEncoder: Encoder[URI] = Encoder.encodeString.contramap(_.toString)
51

52
    implicit val urlEncoder: Encoder[URL] = Encoder.encodeString.contramap(_.toExternalForm)
4✔
53
    implicit val urlDecoder: Decoder[URL] = Decoder.decodeString.map(new URL(_))
×
54
  }
55

56
  implicit class RichACursor(cursor: ACursor) {
57

58
    def downAt(p: Json => Boolean): ACursor = {
59
      @tailrec
60
      def go(c: ACursor): ACursor = c match {
61
        case success: HCursor => if (p(success.value)) success else go(success.right)
2✔
62
        case other => other
×
63
      }
64

65
      go(cursor.downArray)
2✔
66
    }
67

68
  }
69

70
  // Be default circe print all empty values as a nulls which can be good for programs because it is more explicit
71
  // that some value is null, but for people it generates a lot of unnecessary noice
72
  val humanReadablePrinter: Printer = Printer.spaces2.copy(dropNullValues = true)
58✔
73

74
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc