io.circe.parser.parse Scala Examples

The following examples show how to use io.circe.parser.parse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: CirceSpec.scala    From featherbed   with Apache License 2.0 6 votes vote down vote up
package featherbed.circe

import cats.implicits._
import com.twitter.util.Future
import io.circe._
import io.circe.generic.auto._
import io.circe.parser.parse
import io.circe.syntax._
import org.scalatest.FlatSpec
import shapeless.{Coproduct, Witness}
import shapeless.union.Union

case class Foo(someText: String, someInt: Int)

class CirceSpec extends FlatSpec {

  "post request of a case class" should "derive JSON encoder" in {

    import com.twitter.util.{Future, Await}
    import com.twitter.finagle.{Service, Http}
    import com.twitter.finagle.http.{Request, Response}
    import java.net.InetSocketAddress

    val server = Http.serve(new InetSocketAddress(8766), new Service[Request, Response] {
      def apply(request: Request): Future[Response] = Future {
        val rep = Response()
        rep.contentString = s"${request.contentString}"
        rep.setContentTypeJson()
        rep
      }
    })

    import java.net.URL
    val client = new featherbed.Client(new URL("http://localhost:8766/api/"))

    import io.circe.generic.auto._

    val req = client.post("foo/bar")
      .withContent(Foo("Hello world!", 42), "application/json")
        .accept[Coproduct.`"application/json"`.T]

    val result = Await.result {
       req.send[Foo]()
    }

    Foo("test", 42).asJson.toString

    parse("""{"someText": "test", "someInt": 42}""").toValidated.map(_.as[Foo])

    Await.result(server.close())

  }

  "API example" should "compile" in {
    import shapeless.Coproduct
    import java.net.URL
    import com.twitter.util.Await
    case class Post(userId: Int, id: Int, title: String, body: String)

    case class Comment(postId: Int, id: Int, name: String, email: String, body: String)
    class JSONPlaceholderAPI(baseUrl: URL) {

      private val client = new featherbed.Client(baseUrl)
      type JSON = Coproduct.`"application/json"`.T

      object posts {

        private val listRequest = client.get("posts").accept[JSON]
        private val getRequest = (id: Int) => client.get(s"posts/$id").accept[JSON]

        def list(): Future[Seq[Post]] = listRequest.send[Seq[Post]]()
        def get(id: Int): Future[Post] = getRequest(id).send[Post]()
      }

      object comments {
        private val listRequest = client.get("comments").accept[JSON]
        private val getRequest = (id: Int) => client.get(s"comments/$id").accept[JSON]

        def list(): Future[Seq[Comment]] = listRequest.send[Seq[Comment]]()
        def get(id: Int): Future[Comment] = getRequest(id).send[Comment]()
      }
    }

    val apiClient = new JSONPlaceholderAPI(new URL("http://jsonplaceholder.typicode.com/"))

    Await.result(apiClient.posts.list())
  }

} 
Example 2
Source File: ScalajHttpClient.scala    From telegram   with Apache License 2.0 6 votes vote down vote up
package com.bot4s.telegram.clients

import java.net.Proxy
import java.nio.file.Files

import cats.instances.future._
import com.bot4s.telegram.api.RequestHandler
import com.bot4s.telegram.methods.{Request, JsonRequest, MultipartRequest, Response}
import com.bot4s.telegram.models.InputFile
import com.bot4s.telegram.marshalling
import io.circe.parser.parse
import io.circe.{Decoder, Encoder}
import scalaj.http.{Http, MultiPart}
import slogging.StrictLogging

import scala.concurrent.{ExecutionContext, Future, blocking}


class ScalajHttpClient(token: String, proxy: Proxy = Proxy.NO_PROXY, telegramHost: String = "api.telegram.org")
                      (implicit ec: ExecutionContext) extends RequestHandler[Future] with StrictLogging {

  val connectionTimeoutMs = 10000
  val readTimeoutMs = 50000

  private val apiBaseUrl = s"https://$telegramHost/bot$token/"

  def sendRequest[R, T <: Request[_]](request: T)(implicit encT: Encoder[T], decR: Decoder[R]): Future[R] = {
    val url = apiBaseUrl + request.methodName

    val scalajRequest = request match {
      case r: JsonRequest[_] =>
        Http(url)
          .postData(marshalling.toJson(request))
          .header("Content-Type", "application/json")

      case r: MultipartRequest[_] =>

        // InputFile.FileIds are encoded as query params.
        val (fileIds, files) = r.getFiles.partition {
          case (key, _: InputFile.FileId) => true
          case _ => false
        }

        val parts = files.map {
          case (camelKey, inputFile) =>
            val key = marshalling.snakenize(camelKey)
            inputFile match {
              case InputFile.FileId(id) =>
                throw new RuntimeException("InputFile.FileId cannot must be encoded as a query param")

              case InputFile.Contents(filename, contents) =>
                MultiPart(key, filename, "application/octet-stream", contents)

              case InputFile.Path(path) =>
                MultiPart(key, path.getFileName.toString(),
                  "application/octet-stream",
                  Files.newInputStream(path),
                  Files.size(path),
                  _ => ())

              case other =>
                throw new RuntimeException(s"InputFile $other not supported")
            }
        }

        val fields = parse(marshalling.toJson(request)).fold(throw _, _.asObject.map {
          _.toMap.mapValues {
            json =>
              json.asString.getOrElse(marshalling.printer.pretty(json))
          }
        })

        val fileIdsParams = fileIds.map {
          case (key, inputFile: InputFile.FileId) =>
            marshalling.snakenize(key) -> inputFile.fileId
        }

        val params = fields.getOrElse(Map())

        Http(url).params(params ++ fileIdsParams).postMulti(parts: _*)
    }

    import marshalling.responseDecoder

    Future {
      blocking {
        scalajRequest
          .timeout(connectionTimeoutMs, readTimeoutMs)
          .proxy(proxy)
          .asString
      }
    } map {
      x =>
        if (x.isSuccess)
          marshalling.fromJson[Response[R]](x.body)
        else
          throw new RuntimeException(s"Error ${x.code} on request")
    } map (processApiResponse[R])
  }

} 
Example 3
Source File: MultiNodeEtcdConstructrSpec.scala    From constructr   with Apache License 2.0 5 votes vote down vote up
package de.heikoseeberger.constructr

import akka.actor.{ Address, AddressFromURIString }
import io.circe.Json
import io.circe.parser.parse
import java.util.Base64

class MultiNodeEtcdConstructrSpecMultiJvmNode1 extends MultiNodeEtcdConstructrSpec
class MultiNodeEtcdConstructrSpecMultiJvmNode2 extends MultiNodeEtcdConstructrSpec
class MultiNodeEtcdConstructrSpecMultiJvmNode3 extends MultiNodeEtcdConstructrSpec
class MultiNodeEtcdConstructrSpecMultiJvmNode4 extends MultiNodeEtcdConstructrSpec
class MultiNodeEtcdConstructrSpecMultiJvmNode5 extends MultiNodeEtcdConstructrSpec

object MultiNodeEtcdConstructrSpec {
  def toNodes(s: String): Set[Address] = {
    def jsonToNode(json: Json) = {
      val key =
        json.hcursor
          .get[String]("key")
          .fold(throw _, identity)
          .stripPrefix("/constructr/MultiNodeConstructrSpec/nodes/")
      AddressFromURIString(new String(Base64.getUrlDecoder.decode(key)))
    }
    import cats.syntax.either._ // for Scala 2.11
    parse(s)
      .fold(throw _, identity)
      .hcursor
      .downField("node")
      .get[Set[Json]]("nodes")
      .getOrElse(Set.empty)
      .map(jsonToNode)
  }
}

abstract class MultiNodeEtcdConstructrSpec
    extends MultiNodeConstructrSpec(
      2379,
      "/v2/keys/constructr?recursive=true",
      "/v2/keys/constructr/MultiNodeConstructrSpec/nodes",
      MultiNodeEtcdConstructrSpec.toNodes
    ) 
Example 4
Source File: JsonConvertersSpec.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.extractor.json

import com.daml.lf.data.{ImmArray, Ref}
import com.daml.lf.value.{Value => V}

import org.scalatest.{Matchers, WordSpec}
import io.circe.parser.parse
import io.circe.syntax._

class JsonConvertersSpec extends WordSpec with Matchers {
  import JsonConverters._
  import Ref.Name.{assertFromString => id}

  "records" should {
    val sampleRecord = V.ValueRecord(
      tycon = None,
      fields = ImmArray(
        (Some(id("foo")), V.ValueText("bar")),
        (Some(id("baz")), V.ValueInt64(1253049))
      ))
    val oneMissingLabel = sampleRecord.copy(fields = sampleRecord.fields map {
      case (Some("foo"), v) => (None, v)
      case o => o
    })

    "encode to JsonObject if all labels present" in {
      Right(sampleRecord.asJson) shouldBe parse("""{"foo": "bar", "baz": 1253049}""")
    }

    "encode to list of values if some label absent" in {
      Right(oneMissingLabel.asJson) shouldBe parse("""["bar", 1253049]""")
    }
  }
} 
Example 5
Source File: ConfigurablePatchJsonSpec.scala    From patchless   with Apache License 2.0 5 votes vote down vote up
package patchless.circe.extras

import cats.syntax.either._
import io.circe.generic.extras.Configuration
import io.circe.generic.extras.auto._
import io.circe.parser.parse
import org.scalatest.{FreeSpec, Matchers}
import patchless.Patch
import shapeless.record.Record

class ConfigurablePatchJsonSpec extends FreeSpec with Matchers {


  "Configurable decoder" - {
    "Normal" in {
      import io.circe.generic.extras.defaults._
      case class Foo(aString: String, bInt: Int, cBoolean: Boolean)

      def parsePatch(str: String) =
        parse(str).valueOr(throw _).as[Patch[Foo]].valueOr(throw _)

      val aPatched = parsePatch("""{"aString": "patched"}""")
      val bPatched = parsePatch("""{"bInt": 22}""")
      val cPatched = parsePatch("""{"cBoolean": false}""")

      aPatched.updates shouldEqual Record(aString = Some("patched"), bInt = None, cBoolean = None)
      bPatched.updates shouldEqual Record(aString = None, bInt = Some(22), cBoolean = None)
      cPatched.updates shouldEqual Record(aString = None, bInt = None, cBoolean = Some(false))
    }

    "Snake case" in {
      implicit val configuration = Configuration.default.withSnakeCaseMemberNames
      case class Foo(aString: String, bInt: Int, cBoolean: Boolean)

      def parsePatch(str: String) =
        parse(str).valueOr(throw _).as[Patch[Foo]].valueOr(throw _)

      val aPatched = parsePatch("""{"a_string": "patched"}""")
      val bPatched = parsePatch("""{"b_int": 22}""")
      val cPatched = parsePatch("""{"c_boolean": false}""")

      aPatched.updates shouldEqual Record(aString = Some("patched"), bInt = None, cBoolean = None)
      bPatched.updates shouldEqual Record(aString = None, bInt = Some(22), cBoolean = None)
      cPatched.updates shouldEqual Record(aString = None, bInt = None, cBoolean = Some(false))
    }

    "Options" in {
      import io.circe.generic.extras.defaults._
      case class Foo(aString: Option[String])
      def parsePatch(str: String) =
        parse(str).valueOr(throw _).as[Patch[Foo]].valueOr(throw _)

      val aPatchedSome = parsePatch("""{"aString": "patched"}""")
      val aPatchedNone = parsePatch("""{"aString": null}""")

      aPatchedSome.updates shouldEqual Record(aString = Some(Some("patched")))
      aPatchedNone.updates shouldEqual Record(aString = Some(None))
    }

  }

} 
Example 6
Source File: PatchJsonSpec.scala    From patchless   with Apache License 2.0 5 votes vote down vote up
package patchless.circe

import cats.syntax.either._
import org.scalatest.{FreeSpec, Matchers}
import io.circe.parser.parse
import patchless.Patch
import shapeless.record.Record
import io.circe.generic.auto._

class PatchJsonSpec extends FreeSpec with Matchers {

  "Decoder" - {

    "Auto" in {
      case class Foo(aString: String, bInt: Int, cBoolean: Boolean)

      def parsePatch(str: String) =
        parse(str).valueOr(throw _).as[Patch[Foo]].valueOr(throw _)

      val aPatched = parsePatch("""{"aString": "patched"}""")
      val bPatched = parsePatch("""{"bInt": 22}""")
      val cPatched = parsePatch("""{"cBoolean": false}""")

      aPatched.updates shouldEqual Record(aString = Some("patched"), bInt = None, cBoolean = None)
      bPatched.updates shouldEqual Record(aString = None, bInt = Some(22), cBoolean = None)
      cPatched.updates shouldEqual Record(aString = None, bInt = None, cBoolean = Some(false))
    }

    "Options" in {
      case class Foo(aString: Option[String])
      def parsePatch(str: String) =
        parse(str).valueOr(throw _).as[Patch[Foo]].valueOr(throw _)

      val aPatchedSome = parsePatch("""{"aString": "patched"}""")
      val aPatchedNone = parsePatch("""{"aString": null}""")

      aPatchedSome.updates shouldEqual Record(aString = Some(Some("patched")))
      aPatchedNone.updates shouldEqual Record(aString = Some(None))
    }

  }

} 
Example 7
Source File: MigrateV12ToV13.scala    From nexus-kg   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg

import akka.actor.ActorSystem
import akka.persistence.cassandra.query.scaladsl.CassandraReadJournal
import akka.persistence.query.{EventEnvelope, NoOffset, PersistenceQuery}
import cats.implicits._
import ch.epfl.bluebrain.nexus.admin.client.AdminClient
import ch.epfl.bluebrain.nexus.admin.client.types.Project
import ch.epfl.bluebrain.nexus.commons.test.Resources
import ch.epfl.bluebrain.nexus.iam.client.types._
import ch.epfl.bluebrain.nexus.kg.config.AppConfig
import ch.epfl.bluebrain.nexus.kg.config.Vocabulary.nxv
import ch.epfl.bluebrain.nexus.kg.resources.Event.{Created, Updated}
import ch.epfl.bluebrain.nexus.kg.resources.{OrganizationRef, ResId, Views}
import com.typesafe.scalalogging.Logger
import io.circe.Json
import io.circe.parser.parse
import monix.eval.Task
import monix.execution.Scheduler
import monix.execution.schedulers.CanBlock

import scala.concurrent.Future

object MigrateV12ToV13 extends Resources {
  private val log                                     = Logger[MigrateV12ToV13.type]
  private val newMapping                              = jsonContentOf("/elasticsearch/mapping.json")
  private val defaultEsId                             = nxv.defaultElasticSearchIndex.value
  private implicit val mockedAcls: AccessControlLists = AccessControlLists.empty

  def migrate(
      views: Views[Task],
      adminClient: AdminClient[Task]
  )(implicit config: AppConfig, as: ActorSystem, sc: Scheduler, pm: CanBlock): Unit = {

    implicit val token: Option[AuthToken] = config.iam.serviceAccountToken

    def checkAndUpdateMapping(id: ResId, rev: Long, source: Json)(
        implicit project: Project,
        caller: Caller
    ): Task[Unit] = {

      source.hcursor.get[String]("mapping").flatMap(parse) match {
        case Left(err) =>
          log.error(s"Error while fetching mapping for view id ${id.show}. Reason: '$err'")
          Task.unit
        case Right(mapping) if mapping == newMapping =>
          Task.unit
        case _ =>
          views.update(id, rev, source deepMerge Json.obj("mapping" -> newMapping)).value.flatMap {
            case Left(err) =>
              log.error(s"Error updating the view with id '${id.show}' and rev '$rev'. Reason: '$err'")
              Task.unit
            case _ =>
              log.info(s"View with id '${id.show}' and rev '$rev' was successfully updated.")
              Task.unit
          }
      }
    }

    def fetchProject(orgRef: OrganizationRef, id: ResId)(f: Project => Task[Unit]): Task[Unit] = {
      adminClient.fetchProject(orgRef.id, id.parent.id).flatMap {
        case Some(project) => f(project)
        case None =>
          log.error(s"Project with id '${id.parent.id}' was not found for view with id '${id.show}'")
          Task.unit

      }
    }

    log.info("Migrating views mappings.")
    val pq = PersistenceQuery(as).readJournalFor[CassandraReadJournal](CassandraReadJournal.Identifier)
    Task
      .fromFuture {
        pq.currentEventsByTag(s"type=${nxv.ElasticSearchView.value.asString}", NoOffset)
          .mapAsync(1) {
            case EventEnvelope(_, _, _, Created(id, orgRef, _, _, source, _, subject)) if id.value == defaultEsId =>
              fetchProject(orgRef, id) { project =>
                checkAndUpdateMapping(id, 1L, source)(project, Caller(subject, Set(subject)))
              }.runToFuture
            case EventEnvelope(_, _, _, Updated(id, orgRef, rev, _, source, _, subject)) if id.value == defaultEsId =>
              fetchProject(orgRef, id) { project =>
                checkAndUpdateMapping(id, rev, source)(project, Caller(subject, Set(subject)))
              }.runToFuture
            case _ =>
              Future.unit

          }
          .runFold(0) {
            case (acc, _) =>
              if (acc % 10 == 0) log.info(s"Processed '$acc' persistence ids.")
              acc + 1
          }
          .map(_ => ())
      }
      .runSyncUnsafe()
    log.info("Finished migrating views mappings.")
  }

} 
Example 8
Source File: TestObjects.scala    From elastic-indexer4s   with MIT License 5 votes vote down vote up
package com.yannick_cw.elastic_indexer4s.elasticsearch

import com.sksamuel.elastic4s.http.{ElasticClient, ElasticNodeEndpoint}
import com.sksamuel.elastic4s.mappings.{FieldDefinition, KeywordField, ObjectField, TextField}
import com.yannick_cw.elastic_indexer4s.elasticsearch.elasic_config.{
  ElasticWriteConfig,
  MappingSetting,
  TypedMappingSetting
}
import io.circe.Json
import io.circe.parser.parse
import org.scalacheck.Gen

import scala.concurrent.duration._

object TestObjects {
  case class Address(street: String, zip: Int)
  case class User(name: String, age: Int, address: Address)
  case class NotMatchingUser(name: Int)

  private val addressGen: Gen[Address] = for {
    street <- Gen.alphaStr
    zip    <- Gen.posNum[Int]
  } yield Address(street, zip)

  private val userGen = for {
    name    <- Gen.alphaStr
    age     <- Gen.posNum[Int]
    address <- addressGen
  } yield User(name, age, address)

  def randomUser: User = userGen.sample.get

  val userMappingDef: List[FieldDefinition] = List(
    TextField("name"),
    KeywordField("age"),
    ObjectField("address").fields(
      TextField("street"),
      KeywordField("zip")
    )
  )

  def jsonSettingMapping(docType: String, shards: Int, replicas: Int): Json =
    parse(
      s"""
      |{
      |  "settings": {
      |    "number_of_shards": $shards,
      |    "number_of_replicas": $replicas
      |  },
      |  "mappings": {
      |    "$docType": {
      |      "properties" : {
      |        "address" : {
      |          "properties" : {
      |            "street" : {
      |              "type" : "text"
      |            },
      |            "zip" : {
      |              "type" : "integer"
      |            }
      |          }
      |        },
      |        "age" : {
      |          "type" : "integer"
      |        },
      |        "name" : {
      |          "type" : "text"
      |        }
      |      }
      |    }
      |  }
      |}
      |
      |
    """.stripMargin
    ).fold(throw _, identity)

  def testConf(
      mappingSetting: MappingSetting = TypedMappingSetting(),
      waitForEs: FiniteDuration = 1 second
  )(implicit c: ElasticClient): ElasticWriteConfig =
    new ElasticWriteConfig(
      elasticNodeEndpoints = List(ElasticNodeEndpoint("http", "host", 0, None)),
      indexPrefix = "test_index",
      docType = "docType",
      mappingSetting = mappingSetting,
      waitForElasticTimeout = waitForEs
    ) { override lazy val client: ElasticClient = c }
} 
Example 9
Source File: MappingSetting.scala    From elastic-indexer4s   with MIT License 5 votes vote down vote up
package com.yannick_cw.elastic_indexer4s.elasticsearch.elasic_config

import com.sksamuel.elastic4s.analyzers.AnalyzerDefinition
import com.sksamuel.elastic4s.mappings.MappingDefinition
import io.circe.{Json, ParsingFailure}
import io.circe.parser.parse

sealed trait MappingSetting {
  def fold[A](typed: TypedMappingSetting => A, unsafe: StringMappingSetting => A): A
}

case class TypedMappingSetting(
    analyzer: List[AnalyzerDefinition] = List.empty,
    mappings: List[MappingDefinition] = List.empty,
    shards: Option[Int] = None,
    replicas: Option[Int] = None
) extends MappingSetting {
  def fold[A](typed: (TypedMappingSetting) => A, unsafe: (StringMappingSetting) => A): A = typed(this)
}
sealed abstract case class StringMappingSetting(source: Json) extends MappingSetting {
  def fold[A](typed: (TypedMappingSetting) => A, unsafe: (StringMappingSetting) => A): A = unsafe(this)
}
object StringMappingSetting {
  def unsafeString(source: String): Either[ParsingFailure, MappingSetting] =
    parse(source).map(json => new StringMappingSetting(json) {})
} 
Example 10
Source File: IPythonFormatSpec.scala    From polynote   with Apache License 2.0 5 votes vote down vote up
package polynote.server.repository.format.ipynb

import io.circe.parser.parse
import org.scalatest.{FreeSpec, Matchers}
import polynote.messages.{NotebookCell, CellID}
import polynote.server.repository.NotebookContent
import polynote.testing.ZIOSpec

class IPythonFormatSpec extends FreeSpec with Matchers with ZIOSpec {

  private val subject = new IPythonFormat

  "IPythonFormat" - {

    "applies notebook language when no cell language is present" in {

      val nb = subject.decodeNotebook("dummy",
        """{
          | "metadata": {
          |   "language_info": {
          |     "name": "python"
          |   }
          | },
          | "nbformat": 4,
          | "nbformat_minor": 0,
          | "cells": [
          |   {
          |     "cell_type": "code",
          |     "execution_count": 0,
          |     "source": [
          |       "some python code"
          |     ],
          |     "outputs": []
          |   },
          |   {
          |     "cell_type": "code",
          |     "execution_count": 1,
          |     "metadata": {
          |       "language": "scala"
          |     },
          |     "source": [
          |       "some scala code"
          |     ],
          |     "outputs": []
          |   }
          | ]
          |}""".stripMargin).runIO()

      nb.cells.head.language shouldEqual "python"
      nb.cells(1).language shouldEqual "scala"

    }

    "Saves language_info with most-used language from notebook" in {
      val encoded = subject.encodeNotebook(NotebookContent(
        List(
          NotebookCell(CellID(0), "aaa", "aaa"),
          NotebookCell(CellID(1), "ccc", "ccc"),
          NotebookCell(CellID(2), "bbb", "bbb"),
          NotebookCell(CellID(3), "aaa", "aaa")),
        None
      )).runIO()

      val langInfoName = parse(encoded).right.get.hcursor
        .downField("metadata")
        .downField("language_info")
        .downField("name")
        .as[String]

      langInfoName shouldEqual Right("aaa")
    }

  }

} 
Example 11
Source File: IPythonFormat.scala    From polynote   with Apache License 2.0 5 votes vote down vote up
package polynote.server.repository.format.ipynb

import cats.syntax.either._
import io.circe.Printer
import io.circe.parser.parse
import io.circe.syntax._
import polynote.kernel.{BaseEnv, GlobalEnv}
import polynote.messages.Notebook
import polynote.server.repository.NotebookContent
import polynote.server.repository.format.NotebookFormat
import zio.{RIO, ZIO}

class IPythonFormat extends NotebookFormat {

  override val extension: String = "ipynb"

  override val mime: String = "application/x-ipynb+json"

  override def decodeNotebook(noExtPath: String, rawContent: String): RIO[Any, Notebook] = for {
    parsed  <- ZIO.fromEither(parse(rawContent))
    staged  <- ZIO.fromEither(parsed.as[JupyterNotebookStaged])
    decoded <- ZIO.fromEither(if (staged.nbformat == 3) parsed.as[JupyterNotebookV3].map(JupyterNotebookV3.toV4) else parsed.as[JupyterNotebook])
  } yield JupyterNotebook.toNotebook(decoded).toNotebook(s"$noExtPath.$extension")

  override def encodeNotebook(nb: NotebookContent): RIO[Any, String] = for {
    ipynb <- ZIO(JupyterNotebook.fromNotebook(nb))
  } yield Printer.spaces2.copy(dropNullValues = true).pretty(ipynb.asJson)
} 
Example 12
Source File: JsonCodecs.scala    From skunk   with MIT License 5 votes vote down vote up
// Copyright (c) 2018-2020 by Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT

package skunk
package circe.codec

import cats.implicits._
import io.circe.{ Json, Encoder => CEncoder, Decoder => CDecoder }
import io.circe.parser.parse
import skunk.data.Type

trait JsonCodecs {

  private def genCodec[A](tpe: Type)(
    implicit encode: CEncoder[A],
             decode: CDecoder[A]
  ): Codec[A] =
    Codec.simple(
      a => encode(a).noSpaces,
      s => parse(s).flatMap(decode.decodeJson(_)).leftMap(_.getMessage),
      tpe
    )

  
  val jsonb: Codec[Json] = jsonb[Json]

}

object json extends JsonCodecs

object all extends JsonCodecs 
Example 13
Source File: JsonCodecTest.scala    From skunk   with MIT License 5 votes vote down vote up
// Copyright (c) 2018-2020 by Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT

package tests
package codec

import cats.implicits._
import io.circe.Json
import io.circe.parser.parse
import skunk._
import skunk.circe.codec.all._
import skunk.implicits._

case object JsonCodecTest extends CodecTest {

  val j: Json = parse("""{"foo": [true, "bar"], "tags": {"a": 1, "b": null}}""").toOption.get

  codecTest(json)(j)
  codecTest(json[Int ~ String])(42 ~ "foo")
  codecTest(jsonb)(j)
  codecTest(jsonb[Int ~ String])(42 ~ "foo")

} 
Example 14
Source File: ValidationReport.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.rdf.shacl

import cats.implicits._
import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri
import ch.epfl.bluebrain.nexus.rdf.Node.{BNode, IriNode}
import ch.epfl.bluebrain.nexus.rdf.jena.syntax.all._
import ch.epfl.bluebrain.nexus.rdf.jsonld.syntax._
import ch.epfl.bluebrain.nexus.rdf.shacl.Vocabulary._
import ch.epfl.bluebrain.nexus.rdf.syntax.all._
import io.circe.parser.parse
import io.circe.{Encoder, Json}
import org.apache.jena.rdf.model.Resource

import scala.io.Source


  def isValid(ignoreTargetedNodes: Boolean = false): Boolean =
    (ignoreTargetedNodes && conforms) || (!ignoreTargetedNodes && targetedNodes > 0 && conforms)
}

object ValidationReport {

  final def apply(report: Resource): Either[String, ValidationReport] =
    // format: off
    for {
      tmp     <- report.getModel.asRdfGraph(BNode())
      subject <- tmp.triples.find { case (_, p ,_ ) => p == IriNode(sh.conforms) }.map(_._1).toRight("Unable to find predicate sh:conforms in the validation report graph")
      graph    = tmp.withRoot(subject)
      cursor   = graph.cursor
      conforms <- cursor.down(sh.conforms).as[Boolean].leftMap(_.message)
      targeted <- cursor.down(nxsh.targetedNodes).as[Int].leftMap(_.message)
      json     <- graph.toJson(shaclCtx)
    } yield ValidationReport(conforms, targeted, json.removeKeys("@context", "@id").addContext(shaclCtxUri))
  // format: on

  private val shaclCtxUri: AbsoluteIri = url"https://bluebrain.github.io/nexus/contexts/shacl-20170720.json"
  private val shaclCtx: Json           = jsonContentOf("/shacl-context-resp.json")

  implicit val reportEncoder: Encoder[ValidationReport] = Encoder.instance(_.json)

  private def jsonContentOf(resourcePath: String): Json =
    parse(Source.fromInputStream(getClass.getResourceAsStream(resourcePath)).mkString)
      .getOrElse(throw new IllegalArgumentException)
} 
Example 15
Source File: StaticResourceIamAdminRoutesSpec.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.commons.http.routes

import java.util.UUID

import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.testkit.ScalatestRouteTest
import ch.epfl.bluebrain.nexus.commons.http.RdfMediaTypes
import org.scalatest.Inspectors
import java.util.regex.Pattern.quote

import ch.epfl.bluebrain.nexus.util.Resources
import com.typesafe.config.{Config, ConfigFactory}
import io.circe.parser.parse
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

class StaticResourceIamAdminRoutesSpec
    extends AnyWordSpecLike
    with Matchers
    with Inspectors
    with ScalatestRouteTest
    with Resources {

  val baseUri = "http://nexus.example.com/v1"

  override def testConfig: Config = ConfigFactory.empty()

  val staticRoutes = new StaticResourceRoutes(
    Map(
      "/contexts/context1" -> "/commons/static-routes-test/contexts/context1.json",
      "/contexts/context2" -> "/commons/static-routes-test/contexts/context2.json",
      "/schemas/schema1"   -> "/commons/static-routes-test/schemas/schema1.json",
      "/schemas/schema2"   -> "/commons/static-routes-test/schemas/schema2.json"
    ),
    "test",
    baseUri
  ).routes

  val baseReplacement = Map(
    quote("{{base}}") -> baseUri
  )
  val files           = Map(
    "/v1/test/contexts/context1" -> jsonContentOf(
      "/commons/static-routes-test/contexts/context1.json",
      baseReplacement
    ),
    "/v1/test/contexts/context2" -> jsonContentOf(
      "/commons/static-routes-test/contexts/context2.json",
      baseReplacement
    ),
    "/v1/test/schemas/schema1"   -> jsonContentOf("/commons/static-routes-test/schemas/schema1.json", baseReplacement),
    "/v1/test/schemas/schema2"   -> jsonContentOf("/commons/static-routes-test/schemas/schema2.json", baseReplacement)
  )

  "A StaticResourceRoutes" should {

    "return static resources" in {
      forAll(files.toList) {
        case (path, json) =>
          Get(path) ~> staticRoutes ~> check {
            status shouldEqual StatusCodes.OK
            contentType shouldEqual RdfMediaTypes.`application/ld+json`.toContentType
            parse(responseAs[String]).toOption.get shouldEqual json
          }
      }

    }

    "return 404 when resource doesn't exist" in {
      Get(s"/v1/test/schemas/${UUID.randomUUID().toString}") ~> staticRoutes ~> check {
        rejections shouldEqual Seq()
      }
    }
  }

} 
Example 16
Source File: StaticResourceRoutes.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.commons.http.routes

import java.util.regex.Pattern.quote

import akka.http.scaladsl.model.Uri
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import ch.epfl.bluebrain.nexus.commons.http.JsonLdCirceSupport._
import ch.epfl.bluebrain.nexus.commons.http.directives.PrefixDirectives
import io.circe.Json
import io.circe.parser.parse

import scala.io.Source


class StaticResourceRoutes(resourcePaths: Map[String, String], prefix: String, baseUri: Uri) extends PrefixDirectives {

  private def contentOf(file: String): String = {
    val source   = Source.fromInputStream(getClass.getResourceAsStream(file))
    val contents = source.mkString
    source.close()
    contents
  }

  private def contentOf(file: String, replacements: Map[String, String]): String =
    replacements.foldLeft(contentOf(file)) {
      case (value, (regex, replacement)) => value.replaceAll(regex, replacement)
    }

  private val baseReplacement: Map[String, String] = Map(quote("{{base}}") -> baseUri.toString)

  private lazy val resources: Map[String, Json] =
    resourcePaths.view
      .mapValues { resource => parse(contentOf(resource, baseReplacement)).toOption }
      .flatMap {
        case (key, value) =>
          value match {
            case Some(v) => Some((key, v))
            case None    => None
          }
      }
      .toMap

  def routes: Route =
    uriPrefix(baseUri) {
      (get & pathPrefix(prefix)) {
        extractUnmatchedPath { resourcePath =>
          resources.get(resourcePath.toString) match {
            case Some(json) => complete(json)
            case None       => reject
          }
        }
      }
    }

} 
Example 17
Source File: package.scala    From telegram   with Apache License 2.0 5 votes vote down vote up
package com.bot4s.telegram.marshalling

import cats.free.Trampoline
import cats.instances.function._
import cats.instances.list._
import cats.syntax.traverse._
import io.circe.parser.parse
import io.circe.syntax._
import io.circe.{Json, JsonObject, _}

object `package` extends CirceEncoders with CirceDecoders with CaseConversions {

  private def transformKeys(json: Json, f: String => String): Trampoline[Json] = {
    def transformObjectKeys(obj: JsonObject, f: String => String): JsonObject =
      JsonObject.fromIterable(
        obj.toList.map {
          case (k, v) => f(k) -> v
        }
      )

    json.arrayOrObject(
      Trampoline.done(json),
      _.toList.traverse(j => Trampoline.defer(transformKeys(j, f))).map(Json.fromValues(_)),
      transformObjectKeys(_, f).traverse(obj => Trampoline.defer(transformKeys(obj, f))).map(Json.fromJsonObject)
    )
  }

  private def camelKeys(json: io.circe.Json): Json = transformKeys(json, camelize).run

  private def snakeKeys(json: io.circe.Json): Json = transformKeys(json, snakenize).run

  val printer = Printer.noSpaces.copy(dropNullValues = true)

  def toJson[T: Encoder](t: T): String = printer.pretty(t.asJson)

  def fromJson[T: Decoder](s: String): T = {
    parse(s).fold(throw _,
      json =>
        camelKeys(json).as[T].fold(throw _, identity))
  }
} 
Example 18
Source File: SttpClient.scala    From telegram   with Apache License 2.0 5 votes vote down vote up
package com.bot4s.telegram.clients

import cats.MonadError
import cats.syntax.functor._
import com.bot4s.telegram.api.RequestHandler
import com.bot4s.telegram.marshalling.CaseConversions
import com.bot4s.telegram.methods.{JsonRequest, MultipartRequest, Response}
import com.softwaremill.sttp.{Request => _, Response => _, _}
import com.bot4s.telegram.marshalling
import com.bot4s.telegram.methods._
import com.bot4s.telegram.models.InputFile
import io.circe.parser.parse
import io.circe.{Decoder, Encoder}
import slogging.StrictLogging

import scala.concurrent.duration._


class SttpClient[F[_]](token: String, telegramHost: String = "api.telegram.org")
                      (implicit backend: SttpBackend[F, Nothing], monadError: MonadError[F, Throwable])
  extends RequestHandler[F]()(monadError) with StrictLogging {

  val readTimeout: Duration = 50.seconds

  private implicit def circeBodySerializer[B : Encoder]: BodySerializer[B] =
    b => StringBody(marshalling.toJson[B](b), "utf-8", Some(MediaTypes.Json))

  private def asJson[B : Decoder]: ResponseAs[B, Nothing] =
    asString("utf-8").map(s => marshalling.fromJson[B](s))

  private val apiBaseUrl = s"https://$telegramHost/bot$token/"

  override def sendRequest[R, T <: Request[_]](request: T)(implicit encT: Encoder[T], decR: Decoder[R]): F[R] = {
    val url = apiBaseUrl + request.methodName

    val sttpRequest: RequestT[Id, String, Nothing] = request match {
      case r: JsonRequest[_] =>
        sttp.post(uri"$url").body(request)

      case r: MultipartRequest[_] =>
        val files = r.getFiles

        val parts = files.map {
          case (camelKey, inputFile) =>
            val key = CaseConversions.snakenize(camelKey)
            inputFile match {
              case InputFile.FileId(id) => multipart(key, id)
              case InputFile.Contents(filename, contents) => multipart(key, contents).fileName(filename)
              //case InputFile.Path(path) => multipartFile(key, path)
              case other =>
                throw new RuntimeException(s"InputFile $other not supported")
            }
        }

        val fields = parse(marshalling.toJson(request)).fold(throw _, _.asObject.map {
          _.toMap.mapValues {
            json =>
              json.asString.getOrElse(marshalling.printer.pretty(json))
          }
        })

        val params = fields.getOrElse(Map())

        sttp.post(uri"$url?$params").multipartBody(parts)
    }

    import com.bot4s.telegram.marshalling.responseDecoder

    val response = sttpRequest
      .readTimeout(readTimeout)
      .parseResponseIf(_ => true) // Always parse response
      .response(asJson[Response[R]])
      .send[F]()

    response
      .map(_.unsafeBody)
      .map(processApiResponse[R])
  }
} 
Example 19
Source File: HardcodedDerivationSpec.scala    From circe-magnolia   with Apache License 2.0 5 votes vote down vote up
package io.circe.magnolia.configured

import io.circe.Encoder
import io.circe.magnolia.MagnoliaEncoder
import io.circe.magnolia.configured.HardcodedDerivationSpec.{User, UserType}
import io.circe.parser.parse
import io.circe.tests.CirceSuite
import magnolia.{CaseClass, Magnolia, SealedTrait}

// An example of hardcoding a configuration. This means at when deriving Encoder/Decoder you no longer need to provide
// a Configuration object
class HardcodedDerivationSpec extends CirceSuite {
  "Hardcoded Encoder deriver" should "match the hardcoded configured behavior" in {
    assert(UserType.encoder(User("John", "Doe")).asRight[Throwable] == parse(
      """
        {
          "type": "user",
          "first_name": "John",
          "last_name": "Doe"
        }
      """))
  }
}

object HardcodedDerivationSpec {

  sealed trait UserType
  final case class User(firstName: String, lastName: String) extends UserType

  final case class SuperUser(name: String) extends UserType

  object UserType {
    implicit val encoder: Encoder[UserType] = hardcodedConfiguration.deriveEncoder[UserType]
  }

  object hardcodedConfiguration {
    val config: Configuration =
      Configuration
        .default
        .withDiscriminator("type")
        .withSnakeCaseConstructorNames
        .withSnakeCaseMemberNames

    type Typeclass[T] = Encoder[T]

    def combine[T](caseClass: CaseClass[Typeclass, T]): Typeclass[T] =
      MagnoliaEncoder.combine(caseClass)(config)

    def dispatch[T](sealedTrait: SealedTrait[Typeclass, T]): Typeclass[T] =
      MagnoliaEncoder.dispatch(sealedTrait)(config)

    def deriveEncoder[T]: Typeclass[T] = macro Magnolia.gen[T]
  }

} 
Example 20
Source File: HttpHelper.scala    From codacy-analysis-cli   with GNU Affero General Public License v3.0 5 votes vote down vote up
package com.codacy.analysis.core.utils

import io.circe.parser.parse
import io.circe.{Json, ParsingFailure}
import scalaj.http.{Http, HttpRequest, HttpResponse}

class HttpHelper(apiUrl: Option[String], extraHeaders: Map[String, String]) {

  private lazy val connectionTimeoutMs = 2000
  private lazy val readTimeoutMs = 5000

  private val remoteUrl = apiUrl.getOrElse("https://api.codacy.com") + "/2.0"

  def get(endpoint: String): Either[ParsingFailure, Json] = {
    val headers: Map[String, String] = Map("Content-Type" -> "application/json") ++ extraHeaders

    val response: HttpResponse[String] =
      Http(s"$remoteUrl$endpoint").headers(headers).timeout(connectionTimeoutMs, readTimeoutMs).asString

    parse(response.body)
  }

  def post(endpoint: String, dataOpt: Option[Json] = None): Either[ParsingFailure, Json] = {
    val headers: Map[String, String] = dataOpt.fold(Map.empty[String, String]) { _ =>
      Map("Content-Type" -> "application/json")
    } ++ extraHeaders

    val request: HttpRequest = dataOpt.map { data =>
      Http(s"$remoteUrl$endpoint").postData(data.toString)
    }.getOrElse(Http(s"$remoteUrl$endpoint"))
      .method("POST")
      .headers(headers)
      .timeout(connectionTimeoutMs, readTimeoutMs)

    parse(request.asString.body)
  }

} 
Example 21
Source File: AnyFormatSpec.scala    From scalapb-circe   with MIT License 5 votes vote down vote up
package scalapb_circe

import com.google.protobuf.any.{Any => PBAny}
import jsontest.anytests.{AnyTest, ManyAnyTest}
import io.circe.parser.parse
import scalapb_json._
import EitherOps._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.must.Matchers

class AnyFormatSpec extends AnyFlatSpec with Matchers with JavaAssertions {
  val RawExample = AnyTest("test")

  val RawJson = parse(s"""{"field":"test"}""").getOrError

  val AnyExample = PBAny.pack(RawExample)

  val AnyJson = parse(s"""{"@type":"type.googleapis.com/jsontest.AnyTest","field":"test"}""").getOrError

  val CustomPrefixAny = PBAny.pack(RawExample, "example.com/")

  val CustomPrefixJson = parse(s"""{"@type":"example.com/jsontest.AnyTest","field":"test"}""").getOrError

  val ManyExample = ManyAnyTest(
    Seq(
      PBAny.pack(AnyTest("1")),
      PBAny.pack(AnyTest("2"))
    )
  )

  val ManyPackedJson = parse("""
      |{
      |  "@type": "type.googleapis.com/jsontest.ManyAnyTest",
      |  "fields": [
      |    {"@type": "type.googleapis.com/jsontest.AnyTest", "field": "1"},
      |    {"@type": "type.googleapis.com/jsontest.AnyTest", "field": "2"}
      |  ]
      |}
    """.stripMargin).getOrError

  override def registeredCompanions = Seq(AnyTest, ManyAnyTest)

  // For clarity
  def UnregisteredPrinter = JsonFormat.printer

  def UnregisteredParser = JsonFormat.parser

  "Any" should "fail to serialize if its respective companion is not registered" in {
    an[IllegalStateException] must be thrownBy UnregisteredPrinter.toJson(AnyExample)
  }

  "Any" should "fail to deserialize if its respective companion is not registered" in {
    a[JsonFormatException] must be thrownBy UnregisteredParser.fromJson[PBAny](AnyJson)
  }

  "Any" should "serialize correctly if its respective companion is registered" in {
    ScalaJsonPrinter.toJson(AnyExample) must be(AnyJson)
  }

  "Any" should "fail to serialize with a custom URL prefix if specified" in {
    an[IllegalStateException] must be thrownBy ScalaJsonPrinter.toJson(CustomPrefixAny)
  }

  "Any" should "fail to deserialize for a non-Google-prefixed type URL" in {
    a[JsonFormatException] must be thrownBy ScalaJsonParser.fromJson[PBAny](CustomPrefixJson)
  }

  "Any" should "deserialize correctly if its respective companion is registered" in {
    ScalaJsonParser.fromJson[PBAny](AnyJson) must be(AnyExample)
  }

  "Any" should "resolve printers recursively" in {
    val packed = PBAny.pack(ManyExample)
    ScalaJsonPrinter.toJson(packed) must be(ManyPackedJson)
  }

  "Any" should "resolve parsers recursively" in {
    ScalaJsonParser.fromJson[PBAny](ManyPackedJson).unpack[ManyAnyTest] must be(ManyExample)
  }
} 
Example 22
Source File: WellKnownTypesSpec.scala    From scalapb-circe   with MIT License 5 votes vote down vote up
package scalapb_circe

import com.google.protobuf.duration.Duration
import com.google.protobuf.timestamp.Timestamp
import jsontest.test.WellKnownTest
import io.circe.parser.parse
import EitherOps._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.must.Matchers

class WellKnownTypesSpec extends AnyFlatSpec with Matchers {

  val durationProto = WellKnownTest(duration = Some(Duration(146, 3455)))

  "duration" should "serialize and parse correctly" in {
    val durationJson = """{
                         |  "duration": "146.000003455s"
                         |}""".stripMargin
    JsonFormat.printer.toJson(durationProto) must be(parse(durationJson).getOrError)
    JsonFormat.parser.fromJsonString[WellKnownTest](durationJson) must be(durationProto)
  }

  "timestamp" should "serialize and parse correctly" in {
    val timestampJson = """{
                          |  "timestamp": "2016-09-16T12:35:24.375123456Z"
                          |}""".stripMargin
    val timestampProto =
      WellKnownTest(timestamp = Some(Timestamp(seconds = 1474029324, nanos = 375123456)))
    JsonFormat.parser.fromJsonString[WellKnownTest](timestampJson) must be(timestampProto)
    JsonFormat.printer.toJson(timestampProto) must be(parse(timestampJson).getOrError)
  }
} 
Example 23
Source File: OneOfSpec.scala    From scalapb-circe   with MIT License 5 votes vote down vote up
package scalapb_circe

import com.google.protobuf.util.JsonFormat.{printer => ProtobufJavaPrinter}
import jsontest.oneof.OneOf._
import jsontest.oneof.{OneOf, OneOfMessage}
import io.circe.parser.parse
import org.scalatest.prop._
import EitherOps._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.must.Matchers

class OneOfSpec extends AnyFlatSpec with Matchers with TableDrivenPropertyChecks {

  val examples = Table(
    ("message", "json"),
    (OneOf.defaultInstance, "{}"),
    (OneOf(Field.Empty), "{}"),
    (OneOf(Field.Primitive("")), """{"primitive":""}"""),
    (OneOf(Field.Primitive("test")), """{"primitive":"test"}"""),
    (OneOf(Field.Wrapper("")), """{"wrapper":""}"""),
    (OneOf(Field.Wrapper("test")), """{"wrapper":"test"}"""),
    (OneOf(Field.Message(OneOfMessage())), """{"message":{}}"""),
    (OneOf(Field.Message(OneOfMessage(Some("test")))), """{"message":{"field":"test"}}""")
  )

  forEvery(examples) { (message: OneOf, json: String) =>
    new Printer(includingDefaultValueFields = false).toJson(message) must be(parse(json).getOrError)
    new Printer(includingDefaultValueFields = false).toJson(message) must be(
      parse(
        ProtobufJavaPrinter().print(toJavaProto(message))
      ).getOrError
    )

    new Printer(includingDefaultValueFields = true).toJson(message) must be(parse(json).getOrError)
    new Printer(includingDefaultValueFields = true).toJson(message) must be(
      parse(
        ProtobufJavaPrinter().includingDefaultValueFields().print(toJavaProto(message))
      ).getOrError
    )
  }

} 
Example 24
Source File: JavaAssertionsPlatform.scala    From scalapb-circe   with MIT License 5 votes vote down vote up
package scalapb_circe

import com.google.protobuf.util.JsonFormat.{TypeRegistry => JavaTypeRegistry}
import scalapb.{GeneratedMessage, GeneratedMessageCompanion, JavaProtoSupport}
import org.scalatest.matchers.must.Matchers

trait JavaAssertionsPlatform {
  self: Matchers with JavaAssertions =>

  def registeredCompanions: Seq[GeneratedMessageCompanion[_]]

  val JavaJsonTypeRegistry =
    registeredCompanions.foldLeft(JavaTypeRegistry.newBuilder())(_ add _.javaDescriptor).build()
  val JavaJsonPrinter =
    com.google.protobuf.util.JsonFormat.printer().usingTypeRegistry(JavaJsonTypeRegistry)
  val JavaJsonParser = com.google.protobuf.util.JsonFormat.parser()

  def assertJsonIsSameAsJava[T <: GeneratedMessage](v: T, checkRoundtrip: Boolean = true)(
    implicit cmp: GeneratedMessageCompanion[T]
  ) = {
    val scalaJson = ScalaJsonPrinter.print(v)
    val javaJson = JavaJsonPrinter.print(
      cmp.asInstanceOf[JavaProtoSupport[T, com.google.protobuf.GeneratedMessageV3]].toJavaProto(v)
    )

    import io.circe.parser.parse
    parse(scalaJson).isRight must be(true)
    parse(scalaJson) must be(parse(javaJson))
    if (checkRoundtrip) {
      ScalaJsonParser.fromJsonString[T](scalaJson) must be(v)
    }
  }

  def javaParse[T <: com.google.protobuf.GeneratedMessageV3.Builder[T]](
    json: String,
    b: com.google.protobuf.GeneratedMessageV3.Builder[T]
  ) = {
    JavaJsonParser.merge(json, b)
    b.build()
  }
} 
Example 25
Source File: MultiNodeConsulConstructrSpec.scala    From constructr-consul   with Apache License 2.0 5 votes vote down vote up
package com.tecsisa.constructr.akka.consul

import akka.actor.{ Address, AddressFromURIString }
import io.circe.Json
import io.circe.parser.parse
import java.util.Base64._

class MultiNodeConsulConstructrSpecMultiJvmNode1 extends MultiNodeConsulConstructrSpec
class MultiNodeConsulConstructrSpecMultiJvmNode2 extends MultiNodeConsulConstructrSpec
class MultiNodeConsulConstructrSpecMultiJvmNode3 extends MultiNodeConsulConstructrSpec
class MultiNodeConsulConstructrSpecMultiJvmNode4 extends MultiNodeConsulConstructrSpec
class MultiNodeConsulConstructrSpecMultiJvmNode5 extends MultiNodeConsulConstructrSpec

object MultiNodeConsulConstructrSpec {
  def toNodes(s: String): Set[Address] = {
    def jsonToNode(json: Json) = {
      val a =
        json.hcursor
          .get[String]("Key")
          .fold(throw _, identity)
          .stripPrefix("constructr/MultiNodeConstructrSpec/nodes/")
      AddressFromURIString(new String(getUrlDecoder.decode(a), "UTF-8"))
    }
    import cats.syntax.either._ // for Scala 2.11
    parse(s)
      .fold(throw _, identity)
      .as[Set[Json]]
      .getOrElse(Set.empty)
      .map(jsonToNode)
  }
}

abstract class MultiNodeConsulConstructrSpec
    extends MultiNodeConstructrSpec(
      8501,
      "/v1/kv/constructr/MultiNodeConstructrSpec?recurse",
      "/v1/kv/constructr/MultiNodeConstructrSpec/nodes?recurse",
      MultiNodeConsulConstructrSpec.toNodes
    )