scala.collection.immutable.ListMap Scala Examples

The following examples show how to use scala.collection.immutable.ListMap. 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: TopicConfigurationParser.scala    From kafka-configurator   with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
package com.sky.kafka.configurator

import java.io.{Reader => JReader}

import cats.instances.either._
import cats.instances.list._
import cats.syntax.either._
import cats.syntax.traverse._
import io.circe
import io.circe.generic.AutoDerivation
import io.circe.yaml.parser._
import io.circe.{Decoder, DecodingFailure, Json}

import scala.collection.immutable.ListMap

object TopicConfigurationParser extends AutoDerivation {

  def apply(topicConfigReader: JReader): Either[circe.Error, List[Topic]] =
    for {
      ymlAsJson <- parse(topicConfigReader)
      topicConfigs <- if (ymlAsJson.isBoolean) List.empty[Topic].asRight else ymlAsJson.as[List[Topic]]
    } yield topicConfigs

  case class TopicConfig(partitions: Int, replication: Int, config: Map[String, String])

  implicit val topicsDecoder: Decoder[List[Topic]] = Decoder.instance { cursor =>
    for {
      configMap <- cursor.as[ListMap[String, TopicConfig]]
      topics = configMap.map { case (name, conf) => Topic(name, conf.partitions, conf.replication, conf.config) }
    } yield topics.toList
  }

  implicit val stringMapDecoder: Decoder[Map[String, String]] = Decoder.instance { cursor =>
    def stringify(json: Json): Json =
      json.asNumber.fold(json)(num =>
        Json.fromString(num.toLong.fold(num.toDouble.toString)(_.toString)))

    def failWithMsg(msg: String) = DecodingFailure(msg, List.empty)

    for {
      jsonObj <- cursor.value.asObject.toRight(failWithMsg(s"${cursor.value} is not an object"))
      valuesAsJsonStrings = jsonObj.mapValues(stringify)
      stringMap <- valuesAsJsonStrings.toList.traverse[Decoder.Result, (String, String)] {
        case (key, json) => json.asString.toRight(failWithMsg(s"$json is not a string")).map(key -> _)
      }
    } yield stringMap.toMap
  }
} 
Example 2
Source File: VwMultilabelModelPluginJsonReader.scala    From aloha   with MIT License 5 votes vote down vote up
package com.eharmony.aloha.models.vw.jni.multilabel.json

import com.eharmony.aloha.dataset.vw.multilabel.VwMultilabelRowCreator.{LabelNamespaces, determineLabelNamespaces}
import com.eharmony.aloha.models.multilabel.SparsePredictorProducer
import com.eharmony.aloha.models.vw.jni.Namespaces
import com.eharmony.aloha.models.vw.jni.multilabel.VwSparseMultilabelPredictorProducer
import com.eharmony.aloha.util.Logging
import spray.json.{DeserializationException, JsValue, JsonReader}

import scala.collection.breakOut
import scala.collection.immutable.ListMap


case class VwMultilabelModelPluginJsonReader[K](featureNames: Seq[String], numLabelsInTrainingSet: Int)
   extends JsonReader[SparsePredictorProducer[K]]
      with VwMultilabelModelJson
      with Namespaces
      with Logging {

  import VwMultilabelModelPluginJsonReader._

  override def read(json: JsValue): VwSparseMultilabelPredictorProducer[K] = {
    val ast = json.asJsObject(notObjErr(json)).convertTo[VwMultilabelAst]
    val (namespaces, defaultNs, missing) =
      allNamespaceIndices(featureNames, ast.namespaces.getOrElse(ListMap.empty))

    if (missing.nonEmpty)
      info(s"features in namespaces not found in featureNames: $missing")

    val namespaceNames: Set[String] = namespaces.map(_._1)(breakOut)
    val labelAndDummyLabelNss = determineLabelNamespaces(namespaceNames)

    labelAndDummyLabelNss match {
      case Some(LabelNamespaces(labelNs, _)) =>
        VwSparseMultilabelPredictorProducer[K](ast.modelSource, defaultNs, namespaces, labelNs, numLabelsInTrainingSet)
      case _ =>
        throw new DeserializationException(
          "Could not determine label namespace.  Found namespaces: " +
            namespaceNames.mkString(", ")
        )
    }
  }
}

object VwMultilabelModelPluginJsonReader extends Logging {
  private val JsonErrStrLength = 100

  private[multilabel] def notObjErr(json: JsValue): String = {
    val str = json.prettyPrint
    val substr = str.substring(0, JsonErrStrLength)
    s"JSON object expected.  Found " + substr + (if (str.length != substr.length) " ..." else "")
  }
} 
Example 3
Source File: Assert.scala    From midas   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
// See LICENSE for license details.

package midas.widgets

import chisel3._
import chisel3.util._
import chisel3.experimental.{DataMirror, Direction}

import scala.collection.immutable.ListMap

import freechips.rocketchip.config.{Parameters}
import freechips.rocketchip.util.{DecoupledHelper}

class AssertBundle(val numAsserts: Int) extends Bundle {
  val asserts = Output(UInt(numAsserts.W))
}

class AssertBridgeModule(numAsserts: Int)(implicit p: Parameters) extends BridgeModule[HostPortIO[UInt]]()(p) {
  val io = IO(new WidgetIO())
  val hPort = IO(HostPort(Input(UInt(numAsserts.W))))
  val resume = WireInit(false.B)
  val cycles = RegInit(0.U(64.W))
  val asserts = hPort.hBits
  val assertId = PriorityEncoder(asserts)
  val assertFire = asserts.orR

  val stallN = (!assertFire || resume)
  val dummyPredicate = true.B

  val tFireHelper = DecoupledHelper(hPort.toHost.hValid, stallN, dummyPredicate)
  val targetFire = tFireHelper.fire() // FIXME: On next RC bump
  hPort.toHost.hReady := tFireHelper.fire(hPort.toHost.hValid)
  // We only sink tokens, so tie off the return channel
  hPort.fromHost.hValid := true.B
  when (targetFire) {
    cycles := cycles + 1.U
  }

  genROReg(assertId, "id")
  genROReg(assertFire, "fire")
  // FIXME: no hardcode
  genROReg(cycles(31, 0), "cycle_low")
  genROReg(cycles >> 32, "cycle_high")
  Pulsify(genWORegInit(resume, "resume", false.B), pulseLength = 1)
  genCRFile()
} 
Example 4
Source File: RichSchemaTest.scala    From morpheus   with Apache License 2.0 5 votes vote down vote up
package org.opencypher.okapi.ir.impl

import org.opencypher.okapi.api.schema.PropertyGraphSchema
import org.opencypher.okapi.api.types._
import org.opencypher.okapi.ir.api.IRField
import org.opencypher.okapi.ir.api.pattern.{DirectedRelationship, Pattern}
import org.opencypher.okapi.testing.BaseTestSuite

import scala.collection.immutable.ListMap

class RichSchemaTest extends BaseTestSuite {
    describe("fromFields") {
      it("can convert fields in a pattern") {
        val schema = PropertyGraphSchema.empty
          .withNodePropertyKeys("Person")("name" -> CTString)
          .withNodePropertyKeys("City")("name" -> CTString, "region" -> CTBoolean)
          .withRelationshipPropertyKeys("KNOWS")("since" -> CTFloat.nullable)
          .withRelationshipPropertyKeys("BAR")("foo" -> CTInteger)

        val actual = Pattern(
          Set(
            IRField("n")(CTNode("Person")),
            IRField("r")(CTRelationship("BAR")),
            IRField("m")(CTNode("Person"))
          ),
          ListMap(
            IRField("r")(CTRelationship("BAR")) -> DirectedRelationship(IRField("n")(CTNode("Person")), IRField("m")(CTNode("Person")))
          )
        ).fields.map(f => schema.forElementType(f.cypherType)).reduce(_ ++ _)

        val expected = PropertyGraphSchema.empty
          .withNodePropertyKeys("Person")("name" -> CTString)
          .withRelationshipPropertyKeys("BAR")("foo" -> CTInteger)

        actual should be(expected)
      }

      it("can compute a schema when a field is unknown") {
        val schema = PropertyGraphSchema.empty
          .withNodePropertyKeys("Person")("name" -> CTString)
          .withNodePropertyKeys("City")("name" -> CTString, "region" -> CTBoolean)
          .withRelationshipPropertyKeys("KNOWS")("since" -> CTFloat.nullable)
          .withRelationshipPropertyKeys("BAR")("foo" -> CTInteger)

        val actual = Pattern(
          Set(
            IRField("n")(CTNode("Person")),
            IRField("r")(CTRelationship("BAR")),
            IRField("m")(CTNode())
          ),
          ListMap(
            IRField("r")(CTRelationship("BAR")) -> DirectedRelationship(IRField("n")(CTNode("Person")), IRField("m")(CTNode()))
          )
        ).fields.map(f => schema.forElementType(f.cypherType)).reduce(_ ++ _)

        val expected = PropertyGraphSchema.empty
          .withNodePropertyKeys("Person")("name" -> CTString)
          .withNodePropertyKeys("City")("name" -> CTString, "region" -> CTBoolean)
          .withRelationshipPropertyKeys("BAR")("foo" -> CTInteger)

        actual should be(expected)
      }
    }
} 
Example 5
Source File: Fuser.scala    From quill   with Apache License 2.0 5 votes vote down vote up
package io.getquill.codegen.model

import io.getquill.codegen.util.MapExtensions._
import io.getquill.codegen.dag.CatalogBasedAncestry
import io.getquill.codegen.dag.dag.ClassAncestry
import io.getquill.codegen.model.Stereotyper.Fuser

import scala.collection.immutable.ListMap

trait FuserBase[TableMeta, ColumnMeta] extends Fuser[TableMeta, ColumnMeta] {

  val ancestry: ClassAncestry

  protected def unifyColumns(a: ColumnFusion[ColumnMeta], b: ColumnFusion[ColumnMeta]) = {
    val commonAncestor = ancestry(a.dataType, b.dataType)
    ColumnFusion(
      a.name,
      commonAncestor,
      a.nullable || b.nullable,
      a.meta ++ b.meta
    )
  }

  protected def fuseColumns(a: Seq[ColumnFusion[ColumnMeta]], b: Seq[ColumnFusion[ColumnMeta]]): Seq[ColumnFusion[ColumnMeta]] = {
    // join the two sets of columns by name, take only the ones with columns in common
    // and then unify the columns.
    val aOrdered = new ListMap() ++ a.map(c => (c.name, c))
    val bOrdered = new ListMap() ++ b.map(c => (c.name, c))

    aOrdered.zipOnKeysOrdered(bOrdered)
      .map({ case (_, column) => column })
      .collect {
        case (Some(a), Some(b)) => unifyColumns(a, b)
      }.toSeq
  }

  
  protected def fuseTableVariations(variations: Seq[TableStereotype[TableMeta, ColumnMeta]]): Seq[ColumnFusion[ColumnMeta]] = {
    variations
      .map(_.columns)
      .reduce((a, b) => fuseColumns(a, b))
  }
}

class DefaultFuser[TableMeta, ColumnMeta](val ancestry: ClassAncestry = new CatalogBasedAncestry()) extends FuserBase[TableMeta, ColumnMeta] {
  override def apply(collidingTables: Seq[TableStereotype[TableMeta, ColumnMeta]]): TableStereotype[TableMeta, ColumnMeta] = {
    TableStereotype(
      // Grab all the table schemas from the tables merged since we might want to keep track of them later
      collidingTables.head.table.copy(
        meta = collidingTables.flatMap(_.table.meta)
      ),
      fuseTableVariations(collidingTables)
    )
  }
} 
Example 6
Source File: MapExtensions.scala    From quill   with Apache License 2.0 5 votes vote down vote up
package io.getquill.codegen.util

import scala.collection.immutable.{ ListMap, ListSet }

object MapExtensions {

  implicit class MapOps[K, V](m: Map[K, V]) {
    def zipOnKeys(o: Map[K, V]) = zipMapsOnKeys(m, o)
    def zipOnKeysOrdered(o: Map[K, V]) = zipMapsOnKeysOrdered(m, o)
  }

  def zipMapsOnKeys[K, V](one: Map[K, V], two: Map[K, V]): Map[K, (Option[V], Option[V])] = {
    (for (key <- one.keys ++ two.keys)
      yield (key, (one.get(key), two.get(key))))
      .toMap
  }

  def zipMapsOnKeysOrdered[K, V](one: Map[K, V], two: Map[K, V]): ListMap[K, (Option[V], Option[V])] = {
    val outList = (for (key <- (ListSet() ++ one.keys.toSeq.reverse) ++ (ListSet() ++ two.keys.toSeq.reverse))
      yield (key, (one.get(key), two.get(key))))
    (new ListMap() ++ outList.toSeq.reverse)
  }
} 
Example 7
Source File: DiffMagnoliaDerivation.scala    From diffx   with Apache License 2.0 5 votes vote down vote up
package com.softwaremill.diffx
import acyclic.skipped
import magnolia._

import scala.collection.immutable.ListMap
import scala.language.experimental.macros

trait DiffMagnoliaDerivation extends LowPriority {
  type Typeclass[T] = Derived[Diff[T]]

  def combine[T](ctx: ReadOnlyCaseClass[Typeclass, T]): Derived[Diff[T]] =
    Derived(new Diff[T] {
      override def apply(left: T, right: T, toIgnore: List[FieldPath]): DiffResult = {
        val map = ListMap(ctx.parameters.map { p =>
          val lType = p.dereference(left)
          val pType = p.dereference(right)
          if (toIgnore.contains(List(p.label))) {
            p.label -> Identical(lType)
          } else {
            val nestedIgnore =
              if (toIgnore.exists(_.headOption.exists(h => h == p.label))) toIgnore.map(_.drop(1)) else Nil
            p.label -> p.typeclass.value(lType, pType, nestedIgnore)
          }
        }: _*)
        if (map.values.forall(p => p.isIdentical)) {
          Identical(left)
        } else {
          DiffResultObject(ctx.typeName.short, map)
        }
      }
    })

  def dispatch[T](ctx: SealedTrait[Typeclass, T]): Derived[Diff[T]] =
    Derived({ (left: T, right: T, toIgnore: List[FieldPath]) =>
      {
        val lType = ctx.dispatch(left)(a => a)
        val rType = ctx.dispatch(right)(a => a)
        if (lType == rType) {
          lType.typeclass.value(lType.cast(left), lType.cast(right), toIgnore)
        } else {
          DiffResultValue(lType.typeName.full, rType.typeName.full)
        }
      }
    })

  implicit def gen[T]: Derived[Diff[T]] = macro Magnolia.gen[T]
}

trait LowPriority {
  def fallback[T]: Derived[Diff[T]] =
    Derived((left: T, right: T, toIgnore: List[FieldPath]) => {
      if (left != right) {
        DiffResultValue(left, right)
      } else {
        Identical(left)
      }
    })
} 
Example 8
Source File: DiffInstances.scala    From diffx   with Apache License 2.0 5 votes vote down vote up
package com.softwaremill.diffx
import acyclic.skipped
import com.softwaremill.diffx.Matching._

import scala.collection.immutable.ListMap

trait DiffInstances extends DiffMagnoliaDerivation {
  implicit def diffForNumeric[T: Numeric]: Derived[Diff[T]] =
    Derived((left: T, right: T, _: List[FieldPath]) => {
      val numeric = implicitly[Numeric[T]]
      if (!numeric.equiv(left, right)) {
        DiffResultValue(left, right)
      } else {
        Identical(left)
      }
    })

  implicit def diffForOption[T](implicit ddt: Diff[T]): Derived[Diff[Option[T]]] =
    Derived((left: Option[T], right: Option[T], toIgnore: List[FieldPath]) => {
      (left, right) match {
        case (Some(l), Some(r)) => ddt.apply(l, r, toIgnore)
        case (None, None)       => Identical(None)
        case (l, r)             => DiffResultValue(l, r)
      }
    })

  implicit def diffForSet[T: ObjectMatcher, C[W] <: scala.collection.Set[W]](implicit
      ddt: Diff[T],
      matcher: ObjectMatcher[T]
  ): Derived[Diff[C[T]]] =
    Derived((left: C[T], right: C[T], toIgnore: List[FieldPath]) => {
      val MatchingResults(unMatchedLeftInstances, unMatchedRightInstances, matchedInstances) =
        matching[T](left.toSet, right.toSet, matcher, ddt, toIgnore)
      val leftDiffs = unMatchedLeftInstances
        .diff(unMatchedRightInstances)
        .map(DiffResultAdditional(_))
        .toList
      val rightDiffs = unMatchedRightInstances
        .diff(unMatchedLeftInstances)
        .map(DiffResultMissing(_))
        .toList
      val matchedDiffs = matchedInstances.map { case (l, r) => ddt(l, r, toIgnore) }.toList
      diffResultSet(left, leftDiffs, rightDiffs, matchedDiffs)
    })

  private def diffResultSet[T](
      left: T,
      leftDiffs: List[DiffResult],
      rightDiffs: List[DiffResult],
      matchedDiffs: List[DiffResult]
  ): DiffResult = {
    val diffs = leftDiffs ++ rightDiffs ++ matchedDiffs
    if (diffs.forall(_.isIdentical)) {
      Identical(left)
    } else {
      DiffResultSet(diffs)
    }
  }

  implicit def diffForIterable[T, C[W] <: Iterable[W]](implicit
      ddot: Diff[Option[T]]
  ): Derived[Diff[C[T]]] =
    Derived((left: C[T], right: C[T], toIgnore: List[FieldPath]) => {
      val indexes = Range(0, Math.max(left.size, right.size))
      val leftAsMap = left.toList.lift
      val rightAsMap = right.toList.lift
      val differences = ListMap(indexes.map { index =>
        index.toString -> (ddot.apply(leftAsMap(index), rightAsMap(index), toIgnore) match {
          case DiffResultValue(Some(v), None) => DiffResultAdditional(v)
          case DiffResultValue(None, Some(v)) => DiffResultMissing(v)
          case d                              => d
        })
      }: _*)

      if (differences.values.forall(_.isIdentical)) {
        Identical(left)
      } else {
        DiffResultObject(
          "List",
          differences
        )
      }
    })

  implicit def diffForMap[K, V, C[KK, VV] <: scala.collection.Map[KK, VV]](implicit
      ddot: Diff[Option[V]],
      ddk: Diff[K],
      matcher: ObjectMatcher[K]
  ): Derived[Diff[C[K, V]]] = Derived(new DiffForMap[K, V, C](matcher, ddk, ddot))

  implicit val diffForString: Derived[Diff[String]] = Derived(new DiffForString)

  implicit val diffForRange: Derived[Diff[Range]] = Derived(Diff.fallback[Range])
} 
Example 9
Source File: DOperationCategoryNodeJsonProtocolSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.models.json.workflow

import scala.collection.immutable.SortedMap
import scala.collection.immutable.ListMap

import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FlatSpec, Matchers}
import spray.json._
import ai.deepsense.deeplang.DOperation
import ai.deepsense.deeplang.catalogs.SortPriority
import ai.deepsense.deeplang.catalogs.doperations.{DOperationCategory, DOperationCategoryNode, DOperationDescriptor}
import ai.deepsense.models.json.workflow.DOperationCategoryNodeJsonProtocol._

object SortPriorityTest

class DOperationCategoryNodeJsonProtocolSpec extends FlatSpec with Matchers with MockitoSugar {

  "DOperationCategoryNode" should "be correctly serialized to json" in {
    val childCategory =
      new DOperationCategory(DOperationCategory.Id.randomId, "mock child name", SortPriority.coreDefault) {}
    val childNode = DOperationCategoryNode(Some(childCategory))

    val operationDescriptor = mock[DOperationDescriptor]
    when(operationDescriptor.id) thenReturn DOperation.Id.randomId
    when(operationDescriptor.name) thenReturn "mock operation descriptor name"
    when(operationDescriptor.description) thenReturn "mock operator descriptor description"

    val node = DOperationCategoryNode(
      None,
      successors = SortedMap(childCategory -> childNode),
      operations = List(operationDescriptor))

    val expectedJson = JsObject(
      "catalog" -> JsArray(
        JsObject(
          "id" -> JsString(childCategory.id.toString),
          "name" -> JsString(childCategory.name),
          "catalog" -> JsArray(),
          "items" -> JsArray())
      ),
      "items" -> JsArray(
        JsObject(
          "id" -> JsString(operationDescriptor.id.toString),
          "name" -> JsString(operationDescriptor.name),
          "description" -> JsString(operationDescriptor.description)
        )
      )
    )

    node.toJson shouldBe expectedJson
  }
} 
Example 10
Source File: AccessControlLists.scala    From nexus-iam   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.iam.client.types

import ch.epfl.bluebrain.nexus.iam.client.config.Contexts._
import ch.epfl.bluebrain.nexus.iam.client.config.IamClientConfig
import ch.epfl.bluebrain.nexus.iam.client.config.Vocabulary._
import ch.epfl.bluebrain.nexus.rdf.Iri.Path
import ch.epfl.bluebrain.nexus.rdf.implicits._
import io.circe._
import io.circe.syntax._

import scala.collection.immutable.ListMap


  final def apply(tuple: (Path, ResourceAccessControlList)*): AccessControlLists = AccessControlLists(tuple.toMap)

  implicit def aclsEncoder(implicit http: IamClientConfig): Encoder[AccessControlLists] = Encoder.encodeJson.contramap {
    case AccessControlLists(value) =>
      val arr = value.map {
        case (path, acl) =>
          Json.obj("_path" -> Json.fromString(path.asString)) deepMerge acl.asJson.removeKeys("@context")
      }
      Json
        .obj(nxv.total.prefix -> Json.fromInt(arr.size), nxv.results.prefix -> Json.arr(arr.toSeq: _*))
        .addContext(resourceCtxUri)
        .addContext(iamCtxUri)
        .addContext(searchCtxUri)
  }

  implicit def aclsDecoder: Decoder[AccessControlLists] = {
    import cats.implicits._

    def jsonToPathedAcl(hc: HCursor): Either[DecodingFailure, (Path, ResourceAccessControlList)] =
      for {
        path <- hc.get[Path]("_path")
        acl  <- hc.value.as[ResourceAccessControlList]
      } yield path -> acl

    Decoder.instance { hc =>
      hc.downField(nxv.results.prefix)
        .focus
        .flatMap(_.asArray)
        .toRight(DecodingFailure(s"'${nxv.results.prefix}' field not found", hc.history))
        .flatMap { results =>
          results
            .foldM(Map.empty[Path, ResourceAccessControlList]) { (acc, json) =>
              jsonToPathedAcl(json.hcursor).map(acc + _)
            }
            .map(AccessControlLists(_))
        }
    }
  }
} 
Example 11
Source File: AccessControlLists.scala    From nexus-iam   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.iam.acls

import ch.epfl.bluebrain.nexus.commons.circe.syntax._
import ch.epfl.bluebrain.nexus.iam.config.AppConfig.HttpConfig
import ch.epfl.bluebrain.nexus.iam.config.Contexts._
import ch.epfl.bluebrain.nexus.iam.config.Vocabulary.nxv
import ch.epfl.bluebrain.nexus.iam.types.Identity
import ch.epfl.bluebrain.nexus.rdf.Iri.Path
import ch.epfl.bluebrain.nexus.rdf.implicits._
import io.circe.syntax._
import io.circe.{Encoder, Json}

import scala.collection.immutable.ListMap


  final def apply(tuple: (Path, Resource)*): AccessControlLists = AccessControlLists(tuple.toMap)

  implicit def aclsEncoder(implicit http: HttpConfig): Encoder[AccessControlLists] = Encoder.encodeJson.contramap {
    case AccessControlLists(value) =>
      val arr = value.map {
        case (path, acl) =>
          Json.obj("_path" -> Json.fromString(path.asString)) deepMerge acl.asJson.removeKeys("@context")
      }
      Json
        .obj(nxv.total.prefix -> Json.fromInt(arr.size), nxv.results.prefix -> Json.arr(arr.toSeq: _*))
        .addContext(resourceCtxUri)
        .addContext(iamCtxUri)
        .addContext(searchCtxUri)
  }
} 
Example 12
Source File: QueryExecutionSpec.scala    From neotypes   with MIT License 5 votes vote down vote up
package neotypes

import neotypes.implicits.mappers.results._
import neotypes.implicits.syntax.string._
import neotypes.internal.syntax.async._
import scala.collection.immutable.{ListMap, ListSet, SortedMap}
import scala.concurrent.Future


final class QueryExecutionSpec[F[_]](testkit: EffectTestkit[F]) extends BaseIntegrationSpec(testkit) {
  behavior of s"Excuting queries using: ${effectName}"

  it should "retrieve multiple results as a List" in executeAsFuture { s =>
    "match (p:Person) return p.name"
      .query[Int]
      .list(s)
      .map {
        names => assert(names == (0 to 10).toList)
      }
  }

  it should "retrieve multiple results as a Set" in executeAsFuture { s =>
    "match (p:Person) return p.name"
      .query[Int]
      .set(s)
      .map {
        names => assert(names == (0 to 10).toSet)
      }
  }

  it should "retrieve multiple results as a Vector" in executeAsFuture { s =>
    "match (p:Person) return p.name"
      .query[Int]
      .vector(s)
      .map {
        names => assert(names == (0 to 10).toVector)
      }
  }

  it should "retrieve multiple results as a Map" in executeAsFuture { s =>
    "match (p:Person) return p.name, 1"
      .query[(Int, Int)]
      .map(s)
      .map {
        names => assert(names == (0 to 10).map(k => k -> 1).toMap)
      }
  }

  it should "retrieve multiple results as a custom collection (ListSet)" in executeAsFuture { s =>
    "match (p:Person) return p.name"
      .query[Int]
      .collectAs(ListSet)(s)
      .map {
        names => assert(names == ListSet((0 to 10) : _*))
      }
  }

  it should "retrieve multiple results as a custom collection (ListMap)" in executeAsFuture { s =>
    "match (p:Person) return p.name, 1"
      .query[(Int, Int)]
      .collectAs(ListMap)(s)
      .map {
        names => assert(names == ListMap((0 to 10).map(k => k -> 1) : _*))
      }
  }

  it should "retrieve multiple results as a custom collection (SortedMap)" in executeAsFuture { s =>
    "match (p:Person) return p.name, 1"
      .query[(Int, Int)]
      .collectAs(SortedMap)(s)
      .map {
        names => assert(names == SortedMap((0 to 10).map(k => k -> 1) : _*))
      }
  }

  it should "retrieve a Neo4j LIST into a Scala collection (List)" in executeAsFuture { s =>
    "unwind [1, 2] as x return x"
      .query[Int]
      .list(s)
      .map {
        names => assert(names == List(1, 2))
      }
  }

  it should "retrieve a Neo4j LIST into a Scala collection (ListSet)" in executeAsFuture { s =>
    "unwind [1, 2] as x return x"
      .query[Int]
      .collectAs(ListSet)(s)
      .map {
        names => assert(names == ListSet(1, 2))
      }
  }

  override final val initQuery: String = BaseIntegrationSpec.MULTIPLE_VALUES_INIT_QUERY
} 
Example 13
Source File: PrettyPrinterSpec.scala    From unicorn   with Apache License 2.0 5 votes vote down vote up
package unicorn.json

import scala.collection.immutable.ListMap
import org.specs2.mutable._

class PrettyPrinterSpec extends Specification {

  "The PrettyPrinter" should {
    "print a more complicated JsObject nicely aligned" in {
      val json = JsonParser {
        """{
          |  "Boolean no": false,
          |  "Boolean yes":true,
          |  "Unic\u00f8de" :  "Long string with newline\nescape",
          |  "key with \"quotes\"" : "string",
          |  "key with spaces": null,
          |  "number": -1.2323424E-5,
          |  "simpleKey" : "some value",
          |  "sub object" : {
          |    "sub key": 26.5,
          |    "a": "b",
          |    "array": [1, 2, { "yes":1, "no":0 }, ["a", "b", null], false]
          |  },
          |  "zero": 0
          |}""".stripMargin
      }

      PrettyPrinter(json) mustEqual {
        """{
          |  "simpleKey": "some value",
          |  "Boolean yes": true,
          |  "key with \"quotes\"": "string",
          |  "sub object": {
          |    "sub key": 26.5,
          |    "a": "b",
          |    "array": [1, 2, {
          |      "no": 0,
          |      "yes": 1
          |    }, ["a", "b", null], false]
          |  },
          |  "Boolean no": false,
          |  "key with spaces": null,
          |  "number": -1.2323424E-5,
          |  "Unic\u00f8de": "Long string with newline\nescape",
          |  "zero": 0
          |}""".stripMargin
      }
    }
  }
} 
Example 14
Source File: ExplorationParameter.scala    From lift   with MIT License 5 votes vote down vote up
package exploration.utils

import play.api.libs.json._

import scala.collection.immutable.ListMap

object ExplorationParameter {

  def getValue[T](option: Option[T], config: Option[T], default: T): T = {
    if (option.isDefined && config.isDefined && config.get != option.get)
      println("[ExplorationParameter] Warning: Command line arg overrides existing config file arg")

    option.getOrElse(config.getOrElse(default))
  }

  def getValue[T](option: T, config: Option[T], default: T): T = {
    if (config.isDefined && config.get != option)
      println("[ExplorationParameter] Warning: Command line arg overrides existing config file arg")

    option
  }

  def getValue[T](option: Option[T], config: T): T = {
    if (option.isDefined && config != option.get)
      println("[ExplorationParameter] Warning: Command line arg overrides existing config file arg")

    option.getOrElse(config)
  }

  def getValue[T](option: T, config: T): T = {
    if (config != option)
      println("[ExplorationParameter] Warning: Command line arg overrides existing config file arg")

    option
  }

  def checkUnwantedEntry[T](jsv: JsValue, validKeys: Set[String], result: JsSuccess[T]) = {
    val obj = jsv.asInstanceOf[JsObject]
      val keys = obj.keys
      val unwanted = keys.diff(validKeys)
      if (unwanted.isEmpty) {
        result
      } else {
        JsError(s"Keys: ${unwanted.mkString(",")} found in the incoming JSON")
      }
  }

  def reads[T](jsv: JsValue, read: Reads[T], keys: Set[String]) : JsResult[T] = {
      read.reads(jsv).flatMap { entry =>
        val obj = jsv.asInstanceOf[JsObject]

        ExplorationParameter.checkUnwantedEntry[T](
          obj, keys, JsSuccess(entry))
      }
    }

  def generateConfigString(key: String, parameters: ListMap[String, Any]): String = {
    s"""{
       |${generateInnerJson(key, parameters)}
       |}
     """.stripMargin
  }

  def generateInnerJson(key: String, parameters: ListMap[String, Any]): String = {
    s"""
       |  "$key" : {
       |${parameters.zipWithIndex.map { x =>
      val name = x._1._1
      val value = x._1._2
      val i = x._2
      s"""    "$name": ${
        value match {
          case s: String => s""""$s"""" // wrap string args in ""
          case _ => value.toString()
        }
      }${
        if (i < parameters.size - 1) // drop comma for last parameter
          ","
        else ""
      }"""
    }.mkString("\n")}
       |  }""".stripMargin
  }
} 
Example 15
Source File: StateView.scala    From Waves   with MIT License 5 votes vote down vote up
package com.wavesplatform.lang.v1.repl

import com.wavesplatform.lang.v1.compiler.CompilerContext

import scala.collection.immutable.ListMap

case class StateView(ctx: CompilerContext) {
  lazy val totalCtx: String =
    declMap.values.mkString("\n")

  lazy val declMap: Map[String, String] =
     (ListMap() ++ funcs ++ values ++ types).withDefault(s => s"$s not found in context")

  private lazy val funcs: Map[String, String] =
    ctx.functionDefs
      .filterNot(_._1.startsWith(internalFuncPrefix))
      .map { case (name, info) => (name, DeclPrinter.overloadFuncStr(name, info.fSigList))}

  private lazy val values: Map[String, String] =
    ctx.varDefs
      .map { case (name, info) => (name, DeclPrinter.letStr(name, info.vType)) }

  private lazy val types: Map[String, String] =
    ctx.predefTypes.view.mapValues(DeclPrinter.typeStr).toMap
} 
Example 16
Source File: Artefacts.scala    From releaser   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.releaser

import java.nio.file.Path

import uk.gov.hmrc.Logger

import scala.collection.immutable.ListMap

trait TransformerProvider extends Logger {
  def regexTransformers: ListMap[String, Option[Transformer]]

  def transformersForSupportedFiles(filePaths: List[String]): List[(String, Option[Transformer])] = {
    filePaths
      .filter(isRelevantFile)
      .map { f => f -> findTransformer(f).flatten }
  }

  private def isRelevantFile(filePath: String): Boolean = {
    val isRelevant = findTransformer(filePath).isDefined
    if (!isRelevant) log.warn(s"$filePath was ignored because it is an unsupported file type")
    isRelevant
  }

  private def findTransformer(filePath: String): Option[Option[Transformer]] = {
    val fileName = filePath.split("/").last
    regexTransformers
      .find(t => t._1.r.findFirstIn(fileName).isDefined)
      .map(t => {
        val transformer = t._2
        if (transformer.isEmpty) log.warn(s"$filePath was ignored because it is a blacklisted file type")
        transformer
      })
  }
}

object IvyArtefacts{
  def apply(map:VersionMapping, localDir:Path) = new IvyArtefacts(map, localDir)
}

class IvyArtefacts(map:VersionMapping, localDir:Path) extends TransformerProvider {

  val regexTransformers = ListMap(
    map.artefactName+"-javadoc\\.jar"  -> None,
    s"ivy\\.xml$$"                     -> Some(new IvyTransformer),
    s".+\\.jar$$"                      -> Some(new JarManifestTransformer),
    s".+\\.tgz$$"                      -> Some(new CopyAndRenameTransformer),
    s".+\\.zip$$"                      -> Some(new CopyAndRenameTransformer))

}


object MavenArtefacts{
  def apply(map:VersionMapping, localDir:Path) = new MavenArtefacts(map, localDir)
}

class MavenArtefacts(map:VersionMapping, localDir:Path) extends TransformerProvider {

  val regexTransformers = ListMap(
    s".*-javadoc\\.jar$$" -> None,
    s".+\\.pom$$" -> Some(new PomTransformer),
    s".+\\.jar$$" -> Some(new JarManifestTransformer),
    s".+\\.tgz$$" -> Some(new TgzTransformer),
    s".+\\.zip$$" -> Some(new CopyAndRenameTransformer))

} 
Example 17
Source File: Namespaces.scala    From aloha   with MIT License 5 votes vote down vote up
package com.eharmony.aloha.models.vw.jni

import scala.collection.immutable.ListMap


trait Namespaces {

  def namespaceIndicesAndMissing(
      indices: Map[String, Int],
      namespaces: ListMap[String, Seq[String]]
  ): (List[(String, List[Int])], List[(String, List[String])]) = {
    val nss = namespaces.map { case (ns, fs) =>
      val indMissing = fs.map { f =>
        val oInd = indices.get(f)
        if (oInd.isEmpty)
          (oInd, Option(f))
        else (oInd, None)
      }

      val (oInd, oMissing) = indMissing.unzip
      (ns, oInd.flatten.toList, oMissing.flatten.toList)
    }

    val nsInd = nss.collect { case (ns, ind, _) if ind.nonEmpty => (ns, ind) }.toList
    val nsMissing = nss.collect { case (ns, _, m) if m.nonEmpty => (ns, m) }.toList
    (nsInd, nsMissing)
  }

  def defaultNamespaceIndices(
      indices: Map[String, Int],
      namespaces: ListMap[String, Seq[String]]
  ): List[Int] = {
    val featuresInNss = namespaces.foldLeft(Set.empty[String]){ case (s, (_, fs)) => s ++ fs }
    val unusedFeatures = indices.keySet -- featuresInNss
    unusedFeatures.flatMap(indices.get).toList
  }

  def allNamespaceIndices(
      featureNames: Seq[String],
      namespaces: ListMap[String, Seq[String]]
  ): (List[(String, List[Int])], List[Int], List[(String, List[String])]) = {
    val indices = featureNames.zipWithIndex.toMap
    val (nsi, missing) = namespaceIndicesAndMissing(indices, namespaces)
    val defaultNsInd = defaultNamespaceIndices(indices, namespaces)

    (nsi, defaultNsInd, missing)
  }
} 
Example 18
Source File: Resource.scala    From cloudformation-template-generator   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.monsanto.arch.cloudformation.model.resource

import com.monsanto.arch.cloudformation.model._
import spray.json._

import scala.collection.immutable.ListMap
import scala.language.implicitConversions
import scala.reflect.ClassTag
import scala.reflect.NameTransformer




// serializes to Type and Properties
abstract class Resource[R <: Resource[R] : ClassTag : JsonFormat]{ self: Resource[R] =>
  val ResourceType = NameTransformer.decode(implicitly[ClassTag[R]].runtimeClass.getSimpleName)
  val name: String

  val Condition:      Option[ConditionRef]   = None
  val DependsOn:      Option[Seq[String]]    = None
  val DeletionPolicy: Option[DeletionPolicy] = None

  private val _format: JsonFormat[R] = implicitly[JsonFormat[R]] // the magic
  type RR = Resource[R] // and his assistant

  def when(newCondition: Option[ConditionRef] = Condition): R
}
object Resource extends DefaultJsonProtocol {
  implicit object seqFormat extends JsonWriter[Seq[Resource[_]]]{

    implicit object format extends JsonWriter[Resource[_]]{

      def write(obj: Resource[_]) = {
        val bar: obj.RR = obj.asInstanceOf[obj.RR]
        val raw = bar._format.asInstanceOf[JsonFormat[obj.RR]].write(bar).asJsObject

        val outputFields = Map(
          "Type" -> JsString(obj.ResourceType),
          "Metadata" -> raw.fields.getOrElse("Metadata", JsNull),
          "Properties" -> JsObject(raw.fields - "name" - "Metadata" - "UpdatePolicy" - "Condition" - "DependsOn" - "DeletionPolicy"),
          "UpdatePolicy" -> raw.fields.getOrElse("UpdatePolicy", JsNull),
          "Condition" -> obj.Condition.map(_.toJson).getOrElse(JsNull),
          "DependsOn" -> obj.DependsOn.map(dependencies => JsArray(dependencies.map(_.toJson).toVector)).getOrElse(JsNull),
          "DeletionPolicy" -> obj.DeletionPolicy.map(_.toJson).getOrElse(JsNull)
        ).filter(_._2 != JsNull)

        JsObject(outputFields)
      }
    }

    def write(objs: Seq[Resource[_]]) = JsObject( ListMap(objs.map( o => o.name -> format.write(o) ): _*) )
  }
}

// http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html
sealed trait DeletionPolicy
object DeletionPolicy extends DefaultJsonProtocol {
  case object Delete   extends DeletionPolicy
  case object Retain   extends DeletionPolicy
  case object Snapshot extends DeletionPolicy // only available for AWS::EC2::Volume, AWS::RDS::DBInstance, and AWS::Redshift::Cluster
  val values = Seq(Delete, Retain, Snapshot)
  implicit val format: JsonFormat[DeletionPolicy] = new EnumFormat[DeletionPolicy](values)
} 
Example 19
Source File: formats.scala    From aloha   with MIT License 5 votes vote down vote up
package com.eharmony.aloha.factory

import java.{lang => jl}
import spray.json._
import spray.json.DefaultJsonProtocol.{BooleanJsonFormat, ByteJsonFormat, ShortJsonFormat, IntJsonFormat, LongJsonFormat, FloatJsonFormat, DoubleJsonFormat}
import scala.collection.immutable.ListMap

object Formats
    extends JavaJsonFormats
    with ScalaJsonFormats

object JavaJsonFormats extends JavaJsonFormats

trait JavaJsonFormats {
    implicit object JavaBooleanJsonFormat extends JsonFormat[jl.Boolean] {
        def write(i: jl.Boolean) = BooleanJsonFormat write i
        def read(json: JsValue) = BooleanJsonFormat read json
    }

    implicit object JavaByteJsonFormat extends JsonFormat[jl.Byte] {
        def write(i: jl.Byte) = ByteJsonFormat write i
        def read(json: JsValue) = ByteJsonFormat read json
    }

    implicit object JavaShortJsonFormat extends JsonFormat[jl.Short] {
        def write(i: jl.Short) = ShortJsonFormat write i
        def read(json: JsValue) = ShortJsonFormat read json
    }

    implicit object JavaIntJsonFormat extends JsonFormat[jl.Integer] {
        def write(i: jl.Integer) = IntJsonFormat write i
        def read(json: JsValue) = IntJsonFormat read json
    }

    implicit object JavaLongJsonFormat extends JsonFormat[jl.Long] {
        def write(i: jl.Long) = LongJsonFormat write i
        def read(json: JsValue) = LongJsonFormat read json
    }

    implicit object JavaFloatJsonFormat extends JsonFormat[jl.Float] {
        def write(i: jl.Float) = FloatJsonFormat write i
        def read(json: JsValue) = FloatJsonFormat read json
    }

    implicit object JavaDoubleJsonFormat extends JsonFormat[jl.Double] {
        def write(i: jl.Double) = DoubleJsonFormat write i
        def read(json: JsValue) = DoubleJsonFormat read json
    }

    def enumFormat[E <: Enum[E]](clas: Class[E]) = new JsonFormat[E] {
        val lookup = clas.getEnumConstants.map{ c => c.name -> c }.toMap
        override def write(e: E): JsValue = JsString(e.name())
        override def read(json: JsValue): E = json match {
            case JsString(v) => lookup.getOrElse(v, throw new DeserializationException(s" ${clas.getCanonicalName} doesn't have a value: $v"))
            case d           => throw new DeserializationException(s"Expected JsString, Found ${d.getClass.getSimpleName}")
        }
    }
}

object ScalaJsonFormats extends ScalaJsonFormats

trait ScalaJsonFormats {

    implicit def listMapFormat[K :JsonFormat, V :JsonFormat] = new RootJsonFormat[ListMap[K, V]] {
        def write(m: ListMap[K, V]) = JsObject {
            m.map { field =>
                field._1.toJson match {
                    case JsString(x) => x -> field._2.toJson
                    case x => throw new SerializationException("Map key must be formatted as JsString, not '" + x + "'")
                }
            }
        }

        @throws[DeserializationException](cause = "When duplicate keys are detected in the map to be created.")
        def read(value: JsValue) = value match {
            case x: JsObject =>
                val duplicateKeys =
                    x.fields.view.unzip._1.groupBy(identity).collect{ case (k, v) if v.size > 1 => k }.toSeq.sorted

                if (duplicateKeys.nonEmpty)
                    deserializationError(s"ListMap to be deserialized has duplicate keys: ${duplicateKeys.mkString(", ")}.")

                x.fields.map {
                    field => (JsString(field._1).convertTo[K], field._2.convertTo[V])
                } (collection.breakOut)
            case x => deserializationError("Expected Map as JsObject, but got " + x)
        }
    }
} 
Example 20
Source File: RegressionModelJson.scala    From aloha   with MIT License 5 votes vote down vote up
package com.eharmony.aloha.models.reg.json

import com.eharmony.aloha.factory.ScalaJsonFormats.listMapFormat
import com.eharmony.aloha.id.ModelId
import com.eharmony.aloha.models.reg.ConstantDeltaSpline
import spray.json.DefaultJsonProtocol._

import scala.collection.immutable.ListMap

trait RegressionModelJson extends SpecJson {

  protected[this] case class Hof(features: Map[String, Seq[String]], wt: Double) extends Serializable

  protected[this] case class RegData(
    modelType: String,
    modelId: ModelId,
    notes: Option[Seq[String]],
    features: ListMap[String, Spec],
    weights: ListMap[String, Double],
    higherOrderFeatures: Option[Seq[Hof]],
    spline: Option[ConstantDeltaSpline],
    numMissingThreshold: Option[Int])


  protected[this] final implicit val hofJsonFormat = jsonFormat2(Hof)
  protected[this] final implicit val regSplineJsonFormat = jsonFormat(ConstantDeltaSpline, "min", "max", "knots")
  protected[this] final implicit val regDataJsonFormat = jsonFormat8(RegData)
} 
Example 21
Source File: MultilabelModelJson.scala    From aloha   with MIT License 5 votes vote down vote up
package com.eharmony.aloha.models.multilabel.json

import com.eharmony.aloha.id.ModelId
import com.eharmony.aloha.models.multilabel.PluginInfo
import com.eharmony.aloha.models.reg.json.{Spec, SpecJson}
import spray.json.DefaultJsonProtocol._
import spray.json.{JsObject, JsonFormat, RootJsonFormat}

import scala.collection.immutable.ListMap
import com.eharmony.aloha.factory.ScalaJsonFormats

trait MultilabelModelJson extends SpecJson with ScalaJsonFormats {

  protected[this] case class Plugin(`type`: String)

  
  protected[this] case class MultilabelData[K](
      modelType: String,
      modelId: ModelId,
      features: ListMap[String, Spec],
      numMissingThreshold: Option[Int],
      labelsInTrainingSet: Vector[K],
      labelsOfInterest: Option[String],
      underlying: JsObject
  ) extends PluginInfo[K]

  protected[this] final implicit def multilabelDataJsonFormat[K: JsonFormat]: RootJsonFormat[MultilabelData[K]] =
    jsonFormat7(MultilabelData[K])

  protected[this] final implicit val pluginJsonFormat: RootJsonFormat[Plugin] =
    jsonFormat1(Plugin)
} 
Example 22
Source File: Metrics.scala    From cave   with MIT License 5 votes vote down vote up
package controllers

import com.gilt.cavellc.models.Role

import scala.collection.immutable.ListMap
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class Metrics extends AbstractCaveController {

  def deleteTeamMetric(organizationName: String, teamName: String, metricName: String) = caveAsyncAction { implicit request =>
    withCaveClient { client =>
      for {
        deleteResult <- client.Teams.deleteOrganizationsAndMetricNamesByOrganizationNameAndTeamNameAndMetric(organizationName, teamName, metricName)
      } yield {
        // TODO: Will need to handle error scenarios (e.g., when server returns 404 or 500)
        Redirect(routes.Metrics.teamMetrics(organizationName, teamName))
      }
    }
  }

  def deleteOrganizationMetric(organizationName: String, metricName: String) = caveAsyncAction { implicit request =>
    withCaveClient { client =>
      for {
        deleteResult <- client.Organizations.deleteMetricNamesByNameAndMetric(organizationName, metricName)
      } yield {
        // TODO: Will need to handle error scenarios (e.g., when server returns 404 or 500)
        Redirect(routes.Metrics.organizationMetrics(organizationName))
      }
    }
  }

  def organizationMetrics(organizationName: String) = caveAsyncAction { implicit request =>
    withCaveClient { client =>
      val breadcrumb = ListMap("Home" -> routes.Application.index,
        organizationName -> routes.Organizations.organization(organizationName),
        "metrics" -> routes.Metrics.organizationMetrics(organizationName))
      for {
        metrics <- client.Metrics.getOrganizationsAndMetricNamesByOrganization(organizationName)
        role <- client.Users.getOrganizations().map(_.filter(_.name == organizationName).map(_.role).headOption.getOrElse(Role.Viewer))
      } yield Ok(views.html.metrics.metrics(metrics, organizationName, None, role, breadcrumb))
    }
  }

  def teamMetrics(organizationName: String, teamName: String) = caveAsyncAction { implicit request =>
    withCaveClient { client =>
      for {
        organizations <- client.Users.getOrganizations()
        metrics <- client.Metrics.getOrganizationsAndTeamsAndMetricNamesByOrganizationAndTeam(organizationName, teamName)
        roleInOrganization <- Future.successful(organizations.filter(_.name == organizationName).head.role)
        userTeams <- getUserTeams(organizations, client)
        role <- getRoleInTeam(organizationName, teamName, userTeams, roleInOrganization)
        breadcrumb <- Future.successful(ListMap(
          "Home" -> routes.Application.index,
          organizationName -> buildOrganizationUrlForBreadcrumb(organizationName, organizations),
          teamName -> routes.Teams.team(organizationName, teamName),
          "metrics" -> routes.Metrics.teamMetrics(organizationName, teamName))
        )
      } yield Ok(views.html.metrics.metrics(metrics, organizationName, Some(teamName), role, breadcrumb))
    }
  }
} 
Example 23
Source File: RichRowTest.scala    From scio   with Apache License 2.0 5 votes vote down vote up
package com.spotify.scio.bigtable

import com.google.bigtable.v2.{Cell, Column, Family, Row}
import com.google.protobuf.ByteString
import org.scalatest.matchers.should.Matchers
import org.scalatest.flatspec.AnyFlatSpec

import scala.jdk.CollectionConverters._
import scala.collection.immutable.ListMap

class RichRowTest extends AnyFlatSpec with Matchers {
  def bs(s: String): ByteString = ByteString.copyFromUtf8(s)

  val FAMILY_NAME = "family"

  val dataMap = Seq(
    "a" -> Seq(10 -> "x", 9 -> "y", 8 -> "z"),
    "b" -> Seq(7 -> "u", 6 -> "v", 5 -> "w"),
    "c" -> Seq(4 -> "r", 3 -> "s", 2 -> "t")
  ).map {
    case (q, cs) =>
      val kvs = cs.map(kv => (kv._1.toLong, bs(kv._2)))
      (bs(q), ListMap(kvs: _*))
  }.toMap

  val columns = dataMap.map {
    case (q, cs) =>
      val cells = cs.map {
        case (t, v) =>
          Cell.newBuilder().setTimestampMicros(t).setValue(v).build()
      }
      Column
        .newBuilder()
        .setQualifier(q)
        .addAllCells(cells.asJava)
        .build()
  }

  val row = Row
    .newBuilder()
    .addFamilies(
      Family
        .newBuilder()
        .setName(FAMILY_NAME)
        .addAllColumns(columns.asJava)
    )
    .build()

  "RichRow" should "support getColumnCells" in {
    for ((q, cs) <- dataMap) {
      val cells = cs.map {
        case (t, v) =>
          Cell.newBuilder().setTimestampMicros(t).setValue(v).build()
      }
      row.getColumnCells(FAMILY_NAME, q) shouldBe cells
    }
  }

  it should "support getColumnLatestCell" in {
    for ((q, cs) <- dataMap) {
      val cells = cs.map {
        case (t, v) =>
          Cell.newBuilder().setTimestampMicros(t).setValue(v).build()
      }
      row.getColumnLatestCell(FAMILY_NAME, q) shouldBe cells.headOption
    }
  }

  it should "support getFamilyMap" in {
    val familyMap = dataMap.map { case (q, cs) => (q, cs.head._2) }
    row.getFamilyMap(FAMILY_NAME) shouldBe familyMap
  }

  it should "support getMap" in {
    row.getMap shouldBe Map(FAMILY_NAME -> dataMap)
  }

  it should "support getNoVersionMap" in {
    val noVerMap = dataMap.map { case (q, cs) => (q, cs.head._2) }
    row.getNoVersionMap shouldBe Map(FAMILY_NAME -> noVerMap)
  }

  it should "support getValue" in {
    for ((q, cs) <- dataMap) {
      row.getValue(FAMILY_NAME, q) shouldBe Some(cs.head._2)
    }
  }
} 
Example 24
Source File: EndpointToOpenApiComponents.scala    From tapir   with Apache License 2.0 5 votes vote down vote up
package sttp.tapir.docs.openapi

import sttp.tapir.docs.openapi.schema.SchemaKey
import sttp.tapir.openapi.OpenAPI.ReferenceOr
import sttp.tapir.openapi.{Schema => OSchema, _}

import scala.collection.immutable.ListMap

private[openapi] class EndpointToOpenApiComponents(
    keyToSchema: ListMap[SchemaKey, ReferenceOr[OSchema]],
    securitySchemes: SecuritySchemes
) {
  def components: Option[Components] = {
    if (keyToSchema.nonEmpty || securitySchemes.nonEmpty)
      Some(Components(keyToSchema, securitySchemes.values.toMap.mapValues(Right(_)).toListMap))
    else None
  }
} 
Example 25
Source File: EndpointToOperationResponse.scala    From tapir   with Apache License 2.0 5 votes vote down vote up
package sttp.tapir.docs.openapi

import sttp.tapir._
import sttp.tapir.docs.openapi.schema.ObjectSchemas
import sttp.tapir.internal._
import sttp.tapir.openapi.OpenAPI.ReferenceOr
import sttp.tapir.openapi.{Schema => OSchema, SchemaType => OSchemaType, _}

import scala.collection.immutable.ListMap

private[openapi] class EndpointToOperationResponse(objectSchemas: ObjectSchemas, codecToMediaType: CodecToMediaType) {
  def apply(e: Endpoint[_, _, _, _]): ListMap[ResponsesKey, ReferenceOr[Response]] = {
    // There always needs to be at least a 200 empty response
    outputToResponses(e.output, ResponsesCodeKey(200), Some(Response("", ListMap.empty, ListMap.empty))) ++
      outputToResponses(e.errorOutput, ResponsesDefaultKey, None)
  }

  private def outputToResponses(
      output: EndpointOutput[_],
      defaultResponseKey: ResponsesKey,
      defaultResponse: Option[Response]
  ): ListMap[ResponsesKey, ReferenceOr[Response]] = {
    val responses: ListMap[ResponsesKey, ReferenceOr[Response]] = output.asBasicOutputsMap.flatMap {
      case (sc, outputs) =>
        // there might be no output defined at all
        outputsToResponse(sc, outputs)
          .map { response =>
            // using the "default" response key, if no status code is provided
            val responseKey = sc.map(c => ResponsesCodeKey(c.code)).getOrElse(defaultResponseKey)
            responseKey -> Right(response)
          }
    }

    if (responses.isEmpty) {
      // no output at all - using default if defined
      defaultResponse.map(defaultResponseKey -> Right(_)).toIterable.toListMap
    } else responses
  }

  private def outputsToResponse(sc: Option[sttp.model.StatusCode], outputs: Vector[EndpointOutput[_]]): Option[Response] = {
    val headers = outputs.collect {
      case EndpointIO.Header(name, codec, info) =>
        name -> Right(
          Header(
            info.description,
            Some(!codec.schema.exists(_.isOptional)),
            None,
            None,
            None,
            None,
            None,
            Some(objectSchemas(codec)),
            info.example.flatMap(exampleValue(codec, _)),
            ListMap.empty,
            ListMap.empty
          )
        )
      case EndpointIO.FixedHeader(h, _, info) =>
        h.name -> Right(
          Header(
            info.description,
            Some(true),
            None,
            None,
            None,
            None,
            None,
            Option(Right(OSchema(OSchemaType.String))),
            None,
            ListMap.empty,
            ListMap.empty
          )
        )
    }

    val bodies = outputs.collect {
      case EndpointIO.Body(_, codec, info) => (info.description, codecToMediaType(codec, info.examples))
      case EndpointIO.StreamBodyWrapper(StreamingEndpointIO.Body(codec, info, _)) =>
        (info.description, codecToMediaType(codec, info.examples))
    }
    val body = bodies.headOption

    val statusCodeDescriptions = outputs.flatMap {
      case EndpointOutput.StatusCode(possibleCodes, _, _)                          => possibleCodes.filter(c => sc.contains(c._1)).flatMap(_._2.description)
      case EndpointOutput.FixedStatusCode(_, _, EndpointIO.Info(Some(desc), _, _)) => Vector(desc)
      case _                                                                       => Vector()
    }

    val description = body.flatMap(_._1).getOrElse(statusCodeDescriptions.headOption.getOrElse(""))

    val content = body.map(_._2).getOrElse(ListMap.empty)

    if (body.isDefined || headers.nonEmpty) {
      Some(Response(description, headers.toListMap, content))
    } else if (outputs.nonEmpty) {
      Some(Response(description, ListMap.empty, ListMap.empty))
    } else {
      None
    }
  }
} 
Example 26
Source File: CodecToMediaType.scala    From tapir   with Apache License 2.0 5 votes vote down vote up
package sttp.tapir.docs.openapi

import sttp.tapir.docs.openapi.schema.ObjectSchemas
import sttp.tapir.openapi.{MediaType => OMediaType}
import sttp.tapir.{CodecFormat, _}

import scala.collection.immutable.ListMap

private[openapi] class CodecToMediaType(objectSchemas: ObjectSchemas) {
  def apply[T, CF <: CodecFormat](o: Codec[_, T, CF], examples: List[EndpointIO.Example[T]]): ListMap[String, OMediaType] = {
    val convertedExamples = ExampleConverter.convertExamples(o, examples)

    ListMap(
      o.format.mediaType.noCharset.toString -> OMediaType(
        Some(objectSchemas(o)),
        convertedExamples.singleExample,
        convertedExamples.multipleExamples,
        ListMap.empty
      )
    )
  }
} 
Example 27
Source File: EndpointToOpenAPIDocs.scala    From tapir   with Apache License 2.0 5 votes vote down vote up
package sttp.tapir.docs.openapi

import sttp.tapir.docs.openapi.schema.ObjectSchemasForEndpoints
import sttp.tapir.openapi._
import sttp.tapir.{EndpointInput, _}

import scala.collection.immutable.ListMap

object EndpointToOpenAPIDocs {
  def toOpenAPI(api: Info, es: Iterable[Endpoint[_, _, _, _]], options: OpenAPIDocsOptions): OpenAPI = {
    val es2 = es.map(nameAllPathCapturesInEndpoint)
    val (keyToSchema, objectSchemas) = ObjectSchemasForEndpoints(es2)
    val securitySchemes = SecuritySchemesForEndpoints(es2)
    val pathCreator = new EndpointToOpenApiPaths(objectSchemas, securitySchemes, options)
    val componentsCreator = new EndpointToOpenApiComponents(keyToSchema, securitySchemes)

    val base = apiToOpenApi(api, componentsCreator)

    es2.map(pathCreator.pathItem).foldLeft(base) {
      case (current, (path, pathItem)) =>
        current.addPathItem(path, pathItem)
    }
  }

  private def apiToOpenApi(info: Info, componentsCreator: EndpointToOpenApiComponents): OpenAPI = {
    OpenAPI(
      info = info,
      tags = List.empty,
      servers = List.empty,
      paths = ListMap.empty,
      components = componentsCreator.components,
      security = List.empty
    )
  }

  private def nameAllPathCapturesInEndpoint(e: Endpoint[_, _, _, _]): Endpoint[_, _, _, _] = {
    val (input2, _) = new EndpointInputMapper[Int](
      {
        case (EndpointInput.PathCapture(None, codec, info), i) =>
          (EndpointInput.PathCapture(Some(s"p$i"), codec, info), i + 1)
      },
      PartialFunction.empty
    ).mapInput(e.input, 1)

    e.copy(input = input2)
  }
} 
Example 28
Source File: ExampleConverter.scala    From tapir   with Apache License 2.0 5 votes vote down vote up
package sttp.tapir.docs.openapi

import sttp.tapir.openapi.OpenAPI.ReferenceOr
import sttp.tapir.{Codec, EndpointIO}
import sttp.tapir.openapi.{Example, ExampleValue}

import scala.collection.immutable.ListMap

private[openapi] object ExampleConverter {
  case class Examples(singleExample: Option[ExampleValue], multipleExamples: ListMap[String, ReferenceOr[Example]])

  def convertExamples[T](o: Codec[_, T, _], examples: List[EndpointIO.Example[T]]): Examples = {
    convertExamples(examples)(exampleValue(o, _))
  }

  private def convertExamples[T](examples: List[EndpointIO.Example[T]])(exampleValue: T => Option[ExampleValue]): Examples = {
    examples match {
      case (example @ EndpointIO.Example(_, None, _)) :: Nil =>
        Examples(exampleValue(example.value), ListMap.empty)
      case examples =>
        val exampleValues = examples.zipWithIndex.map {
          case (example, i) =>
            example.name.getOrElse(s"Example$i") ->
              Right(Example(summary = example.summary, description = None, value = exampleValue(example.value), externalValue = None))
        }.toListMap
        Examples(None, exampleValues)
    }
  }
} 
Example 29
Source File: package.scala    From tapir   with Apache License 2.0 5 votes vote down vote up
package sttp.tapir.openapi

import com.github.ghik.silencer.silent
import io.circe.generic.semiauto._
import io.circe.parser._
import io.circe.syntax._
import io.circe.{Encoder, Json}
import sttp.tapir.openapi.OpenAPI.ReferenceOr

import scala.collection.immutable.ListMap

package object circe extends TapirOpenAPICirceEncoders

trait TapirOpenAPICirceEncoders {
  // note: these are strict val-s, order matters!

  @silent("possible missing interpolator")
  implicit def encoderReferenceOr[T: Encoder]: Encoder[ReferenceOr[T]] = {
    case Left(Reference(ref)) => Json.obj(("$ref", Json.fromString(ref)))
    case Right(t)             => implicitly[Encoder[T]].apply(t)
  }

  implicit val encoderOAuthFlow: Encoder[OAuthFlow] = deriveEncoder[OAuthFlow]
  implicit val encoderOAuthFlows: Encoder[OAuthFlows] = deriveEncoder[OAuthFlows]
  implicit val encoderSecurityScheme: Encoder[SecurityScheme] = deriveEncoder[SecurityScheme]
  implicit val encoderExampleValue: Encoder[ExampleValue] = {
    case ExampleSingleValue(value)    => parse(value).right.getOrElse(Json.fromString(value))
    case ExampleMultipleValue(values) => Json.arr(values.map(v => parse(v).right.getOrElse(Json.fromString(v))): _*)
  }
  implicit val encoderSchemaType: Encoder[SchemaType.SchemaType] = Encoder.encodeEnumeration(SchemaType)
  implicit val encoderSchema: Encoder[Schema] = deriveEncoder[Schema]
  implicit val encoderReference: Encoder[Reference] = deriveEncoder[Reference]
  implicit val encoderHeader: Encoder[Header] = deriveEncoder[Header]
  implicit val encoderExample: Encoder[Example] = deriveEncoder[Example]
  implicit val encoderResponse: Encoder[Response] = deriveEncoder[Response]
  implicit val encoderEncoding: Encoder[Encoding] = deriveEncoder[Encoding]
  implicit val encoderMediaType: Encoder[MediaType] = deriveEncoder[MediaType]
  implicit val encoderRequestBody: Encoder[RequestBody] = deriveEncoder[RequestBody]
  implicit val encoderParameterStyle: Encoder[ParameterStyle.ParameterStyle] = Encoder.encodeEnumeration(ParameterStyle)
  implicit val encoderParameterIn: Encoder[ParameterIn.ParameterIn] = Encoder.encodeEnumeration(ParameterIn)
  implicit val encoderParameter: Encoder[Parameter] = deriveEncoder[Parameter]
  implicit val encoderResponseMap: Encoder[ListMap[ResponsesKey, ReferenceOr[Response]]] =
    (responses: ListMap[ResponsesKey, ReferenceOr[Response]]) => {
      val fields = responses.map {
        case (ResponsesDefaultKey, r)    => ("default", r.asJson)
        case (ResponsesCodeKey(code), r) => (code.toString, r.asJson)
      }

      Json.obj(fields.toSeq: _*)
    }
  implicit val encoderOperation: Encoder[Operation] = {
    // this is needed to override the encoding of `security: List[SecurityRequirement]`. An empty security requirement
    // should be represented as an empty object (`{}`), not `null`, which is the default encoding of `ListMap`s.
    implicit def encodeListMap[V: Encoder]: Encoder[ListMap[String, V]] = doEncodeListMap(nullWhenEmpty = false)
    deriveEncoder[Operation]
  }
  implicit val encoderPathItem: Encoder[PathItem] = deriveEncoder[PathItem]
  implicit val encoderComponents: Encoder[Components] = deriveEncoder[Components]
  implicit val encoderServerVariable: Encoder[ServerVariable] = deriveEncoder[ServerVariable]
  implicit val encoderServer: Encoder[Server] = deriveEncoder[Server]
  implicit val encoderExternalDocumentation: Encoder[ExternalDocumentation] = deriveEncoder[ExternalDocumentation]
  implicit val encoderTag: Encoder[Tag] = deriveEncoder[Tag]
  implicit val encoderInfo: Encoder[Info] = deriveEncoder[Info]
  implicit val encoderContact: Encoder[Contact] = deriveEncoder[Contact]
  implicit val encoderLicense: Encoder[License] = deriveEncoder[License]
  implicit val encoderOpenAPI: Encoder[OpenAPI] = deriveEncoder[OpenAPI]
  implicit val encoderDiscriminator: Encoder[Discriminator] = deriveEncoder[Discriminator]
  implicit def encodeList[T: Encoder]: Encoder[List[T]] = {
    case Nil        => Json.Null
    case l: List[T] => Json.arr(l.map(i => implicitly[Encoder[T]].apply(i)): _*)
  }
  implicit def encodeListMap[V: Encoder]: Encoder[ListMap[String, V]] = doEncodeListMap(nullWhenEmpty = true)

  private def doEncodeListMap[V: Encoder](nullWhenEmpty: Boolean): Encoder[ListMap[String, V]] = {
    case m: ListMap[String, V] if m.isEmpty && nullWhenEmpty => Json.Null
    case m: ListMap[String, V] =>
      val properties = m.mapValues(v => implicitly[Encoder[V]].apply(v)).toList
      Json.obj(properties: _*)
  }
} 
Example 30
Source File: TapirAuth.scala    From tapir   with Apache License 2.0 5 votes vote down vote up
package sttp.tapir

import sttp.tapir.EndpointInput.Auth
import sttp.tapir.model.UsernamePassword

import scala.collection.immutable.ListMap

object TapirAuth {
  private val BasicAuthType = "Basic"
  private val BearerAuthType = "Bearer"

  
  def bearer[T: Codec[List[String], *, CodecFormat.TextPlain]]: EndpointInput.Auth.Http[T] = httpAuth(BearerAuthType)

  object oauth2 {
    def authorizationCode(
        authorizationUrl: String,
        tokenUrl: String,
        scopes: ListMap[String, String],
        refreshUrl: Option[String] = None
    ): Auth.Oauth2[String] =
      EndpointInput.Auth.Oauth2(
        authorizationUrl,
        tokenUrl,
        scopes,
        refreshUrl,
        header[String]("Authorization").map(stringPrefixWithSpace(BearerAuthType))
      )
  }

  private def httpAuth[T: Codec[List[String], *, CodecFormat.TextPlain]](authType: String): EndpointInput.Auth.Http[T] = {
    val codec = implicitly[Codec[List[String], T, CodecFormat.TextPlain]]
    val authCodec = Codec.list(Codec.string.map(stringPrefixWithSpace(authType))).map(codec).schema(codec.schema)
    EndpointInput.Auth.Http(authType, header[T]("Authorization")(authCodec))
  }

  private def stringPrefixWithSpace(prefix: String) = Mapping.stringPrefix(prefix + " ")
} 
Example 31
Source File: DOperationCategoryNodeJsonProtocolSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.models.json.workflow

import scala.collection.immutable.ListMap

import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FlatSpec, Matchers}
import spray.json._

import io.deepsense.deeplang.DOperation
import io.deepsense.deeplang.catalogs.doperations.{DOperationCategory, DOperationCategoryNode, DOperationDescriptor}
import io.deepsense.models.json.workflow.DOperationCategoryNodeJsonProtocol._

class DOperationCategoryNodeJsonProtocolSpec extends FlatSpec with Matchers with MockitoSugar {

  "DOperationCategoryNode" should "be correctly serialized to json" in {
    val childCategory =
      new DOperationCategory(DOperationCategory.Id.randomId, "mock child name", None) {}
    val childNode = DOperationCategoryNode(Some(childCategory))

    val operationDescriptor = mock[DOperationDescriptor]
    when(operationDescriptor.id) thenReturn DOperation.Id.randomId
    when(operationDescriptor.name) thenReturn "mock operation descriptor name"
    when(operationDescriptor.description) thenReturn "mock operator descriptor description"

    val node = DOperationCategoryNode(
      None,
      successors = ListMap(childCategory -> childNode),
      operations = List(operationDescriptor))

    val expectedJson = JsObject(
      "catalog" -> JsArray(
        JsObject(
          "id" -> JsString(childCategory.id.toString),
          "name" -> JsString(childCategory.name),
          "catalog" -> JsArray(),
          "items" -> JsArray())
      ),
      "items" -> JsArray(
        JsObject(
          "id" -> JsString(operationDescriptor.id.toString),
          "name" -> JsString(operationDescriptor.name),
          "description" -> JsString(operationDescriptor.description)
        )
      )
    )

    node.toJson shouldBe expectedJson
  }
} 
Example 32
Source File: UserAgesChart.scala    From Machine-Learning-with-Spark-Second-Edition   with MIT License 5 votes vote down vote up
package org.sparksamples

import scala.collection.immutable.ListMap
import scalax.chart.module.ChartFactories


object UserAgesChart {

  def main(args: Array[String]) {

    val userDataFrame = Util.getUserFieldDataFrame()
    val ages_array = userDataFrame.select("age").collect()

    val min = 0
    val max = 80
    val bins = 16
    val step = (80/bins).toInt
    var mx = Map(0 -> 0)
    for (i <- step until (max + step) by step) {
      mx += (i -> 0)
    }
    for( x <- 0 until ages_array.length) {
      val age = Integer.parseInt(ages_array(x)(0).toString)
      for (j <- 0 until (max + step) by step) {
        if(age >= j && age < (j + step)){
          mx = mx + (j -> (mx(j) + 1))
        }
      }
    }

    val mx_sorted =  ListMap(mx.toSeq.sortBy(_._1):_*)
    val ds = new org.jfree.data.category.DefaultCategoryDataset
    mx_sorted.foreach{ case (k,v) => ds.addValue(v,"UserAges", k)}

    val chart = ChartFactories.BarChart(ds)

    chart.show()
    Util.sc.stop()
  }
} 
Example 33
Source File: CdxBasedRecord.scala    From ArchiveSpark   with MIT License 5 votes vote down vote up
package org.archive.archivespark.specific.warc

import io.circe.Json
import org.archive.archivespark.model.TypedEnrichRoot
import org.archive.archivespark.sparkling.cdx.CdxRecord
import org.archive.archivespark.util.Json._

import scala.collection.immutable.ListMap

trait CdxBasedRecord extends TypedEnrichRoot[CdxRecord] {
  override def metaToJson: Json = {
    val cdx = get
    json(ListMap[String, Any](
      "surtUrl" -> cdx.surtUrl,
      "timestamp" -> cdx.timestamp,
      "originalUrl" -> cdx.originalUrl,
      "mime" -> cdx.mime,
      "status" -> cdx.status,
      "digest" -> cdx.digest,
      "redirectUrl" -> cdx.redirectUrl,
      "meta" -> cdx.meta,
      "compressedSize" -> cdx.compressedSize
    ))
  }
}

object CdxBasedRecord {
  implicit def cdxBasedRecordToCdxRecord(record: CdxBasedRecord): CdxRecord = record.get
} 
Example 34
Source File: MultiValueEnrichable.scala    From ArchiveSpark   with MIT License 5 votes vote down vote up
package org.archive.archivespark.model

import io.circe.{Json => Circe}
import org.archive.archivespark.util.Json._
import org.archive.archivespark.util.{Json, SerializedException}

import scala.collection.immutable.ListMap

class MultiValueEnrichable[T] private (private var _children: Seq[TypedEnrichable[T]]) extends TypedEnrichable[Seq[T]] {
  def children: Seq[TypedEnrichable[T]] = _children
  def get: Seq[T] = children.map(e => e.get)

  override protected[model] def setHierarchy(parent: Enrichable, field: String, root: EnrichRoot): Unit = {
    super.setHierarchy(parent, field, root)
    for ((c, i) <- _children.zipWithIndex) c.setHierarchy(this, s"[$i]", root)
  }

  override protected[archivespark] def excludeFromOutput(value: Boolean, overwrite: Boolean): Unit = {
    super.excludeFromOutput(value, overwrite)
    for (child <- children) child.excludeFromOutput(value, overwrite)
  }

  override def enrich[D](path: Seq[String], func: EnrichFunc[_, D, _], excludeFromOutput: Boolean): Enrichable = {
    if (path.nonEmpty && path.head == "*") {
      var hasEnriched = false
      var lastException: Option[SerializedException] = None
      val enriched = children.map{c =>
        val enriched = c.enrich(path.tail, func, excludeFromOutput)
        if (enriched != c) {
          hasEnriched = true
          lastException = enriched._lastException.orElse(lastException)
          enriched.asInstanceOf[TypedEnrichable[T]]
        } else c
      }
      if (hasEnriched) {
        val clone = copy().asInstanceOf[MultiValueEnrichable[T]]
        clone._children = enriched
        clone._lastException = lastException
        clone
      } else this
    } else if (path.nonEmpty && path.head.matches("\\[\\d+\\]")) {
      val index = path.head.substring(1, path.head.length - 1).toInt
      if (index < children.length) {
        val enriched = children(index).enrich(path.tail, func, excludeFromOutput).asInstanceOf[TypedEnrichable[T]]
        if (children(index) == enriched) this
        else {
          val clone = copy().asInstanceOf[MultiValueEnrichable[T]]
          clone._children = children.zipWithIndex.map{case (c, i) => if (i == index) enriched else c}
          clone._lastException = enriched._lastException
          clone
        }
      } else this
    } else super.enrich(path, func, excludeFromOutput)
  }

  def apply(index: Int): TypedEnrichable[T] = children(index)

  def get(index: Int): T = apply(index).get

  override def apply[D](path: Seq[String]): Option[TypedEnrichable[D]] = {
    if (path.nonEmpty && path.head == "*") {
      val values = children.map(c => c(path.tail)).filter(_.isDefined).map(_.get)
      if (values.isEmpty) None else {
        val clone = new MultiValueEnrichable(values)
        clone.setHierarchy(parent, field, root)
        Some(clone.asInstanceOf[TypedEnrichable[D]])
      }
    } else if (path.nonEmpty && path.head.matches("\\[\\d+\\]")) {
      val index = path.head.substring(1, path.head.length - 1).toInt
      if (index < children.length) children(index).apply(path.tail) else None
    } else super.apply(path)
  }

  def toJson: Map[String, Circe] = {
    val children = _children.map(c => c.toJson).filter(_.nonEmpty).map(mapToJson)
    (if (isExcludedFromOutput && children.isEmpty) Map() else ListMap(
      null.asInstanceOf[String] -> Json.json(children)
    )) ++ enrichments.map{e => (e, mapToJson(enrichment(e).get.toJson)) }.filter{ case (_, field) => field != null }
  }
}

object MultiValueEnrichable {
  def apply[T](values: Seq[T]): MultiValueEnrichable[T] = new MultiValueEnrichable[T](values.map(v => SingleValueEnrichable(v)))
} 
Example 35
Source File: HttpMessage.scala    From ArchiveSpark   with MIT License 5 votes vote down vote up
package org.archive.archivespark.sparkling.http

import java.io.{BufferedInputStream, InputStream}
import java.util.zip.GZIPInputStream

import org.apache.commons.httpclient.ChunkedInputStream
import org.apache.http.client.entity.DeflateInputStream
import org.archive.archivespark.sparkling.io.IOUtil
import org.archive.archivespark.sparkling.util.StringUtil

import scala.collection.immutable.ListMap
import scala.util.Try

class HttpMessage (val statusLine: String, val headers: Map[String, String], val payload: InputStream) {
  import HttpMessage._

  lazy val lowerCaseHeaders: Map[String, String] = headers.map{case (k,v) => (k.toLowerCase, v)}

  def contentEncoding: Option[String] = lowerCaseHeaders.get("content-encoding").map(_.toLowerCase)
  def mime: Option[String] = lowerCaseHeaders.get("content-type").map(_.split(';').head.trim.toLowerCase)
  def charset: Option[String] = {
    lowerCaseHeaders.get("content-type").flatMap(_.split(';').drop(1).headOption).map(_.trim)
      .filter(_.startsWith("charset="))
      .map(_.drop(8).trim.stripPrefix("\"").stripPrefix("'").stripSuffix("'").stripSuffix("\"").split(",", 2).head.trim)
      .filter(_.nonEmpty).map(_.toUpperCase)
  }
  def redirectLocation: Option[String] = lowerCaseHeaders.get("location").map(_.trim)
  def isChunked: Boolean = lowerCaseHeaders.get("transfer-encoding").map(_.toLowerCase).contains("chunked")

  def status: Int = statusLine.split(" +").drop(1).headOption.flatMap(s => Try{s.toInt}.toOption).getOrElse(-1)

  lazy val body: InputStream = Try {
    var decoded = if (isChunked) new ChunkedInputStream(payload) else payload
    val decoders = contentEncoding.toSeq.flatMap(_.split(',').map(_.trim).flatMap(DecoderRegistry.get))
    for (decoder <- decoders) decoded = decoder(decoded)
    new BufferedInputStream(decoded)
  }.getOrElse(IOUtil.emptyStream)

  lazy val bodyString: String = StringUtil.fromInputStream(body, charset.toSeq ++ BodyCharsets)
}

object HttpMessage {
  val Charset: String = "UTF-8"
  val HttpMessageStart = "HTTP/"
  val BodyCharsets: Seq[String] = Seq("UTF-8", "ISO-8859-1", "WINDOWS-1252")

  // see org.apache.http.client.protocol.ResponseContentEncoding
  val DecoderRegistry: Map[String, InputStream => InputStream] = Map(
    "gzip" -> ((in: InputStream) => new GZIPInputStream(in)),
    "x-gzip" -> ((in: InputStream) => new GZIPInputStream(in)),
    "deflate" -> ((in: InputStream) => new DeflateInputStream(in))
  )

  def get(in: InputStream): Option[HttpMessage] = {
    var line = StringUtil.readLine(in, Charset)
    while (line != null && !{
      if (line.startsWith(HttpMessageStart)) {
        val statusLine = line
        val headers = collection.mutable.Buffer.empty[(String, String)]
        line = StringUtil.readLine(in, Charset)
        while (line != null && line.trim.nonEmpty) {
          val split = line.split(":", 2)
          if (split.length == 2) headers += ((split(0).trim, split(1).trim))
          line = StringUtil.readLine(in, Charset)
        }
        return Some(new HttpMessage(statusLine, ListMap(headers: _*), in))
      }
      false
    }) line = StringUtil.readLine(in, Charset)
    None
  }
} 
Example 36
Source File: Json.scala    From ArchiveSpark   with MIT License 5 votes vote down vote up
package org.archive.archivespark.util

import io.circe.generic.auto._
import io.circe.syntax._
import io.circe.{Json => Circe}

import scala.collection.immutable.ListMap

object Json extends Serializable {
  val SingleValueKey = "_"

  def toJsonString(json: Circe, pretty: Boolean = true): String = if (pretty) json.spaces4 else json.noSpaces

  def mapToJson(map: Map[String, Circe]): Circe = {
    if (map == null || map.isEmpty) return null
    if (map.size == 1 && map.keys.head == null) map.values.head
    else ListMap(map.toSeq.filter{case (key, value) => value != null}.map{ case (key, value) => if (key == null) (SingleValueKey, value) else (key, value) }: _*).asJson
  }

  def json[A](obj: A): Circe = if (obj == null) Circe.Null else obj match {
    case json: Circe => json
    case json: JsonConvertible => json.toJson.asJson
    case map: Map[_, _] => map.map{case (k, v) => (k.toString, json(v))}.asJson
    case bytes: Array[Byte] => s"bytes(length: ${bytes.length})".asJson
    case iterable: TraversableOnce[_] => iterable.map(e => json(e)).toSeq.asJson
    case array: Array[_] => array.map(e => json(e)).asJson
    case str: String => str.asJson
    case n: Long => n.asJson
    case n: Int => n.asJson
    case n: Double => n.asJson
    case n: Float => n.asJson
    case n: Boolean => n.asJson
    case _ => s"obj:${obj.hashCode}".asJson
  }
} 
Example 37
Source File: ParSeqTraceBaseVisualizer.scala    From play-parseq   with Apache License 2.0 5 votes vote down vote up
package com.linkedin.playparseq.trace.utils

import com.linkedin.parseq.trace.Trace
import com.linkedin.parseq.trace.codec.json.JsonTraceCodec
import java.io.File
import play.api.Environment
import play.api.http.HttpConfiguration
import scala.collection.immutable.ListMap
import scala.io.Source



  protected[this] def showTrace(trace: Trace, environment: Environment, httpConfiguration: HttpConfiguration): String = {
    // Get Trace JSON
    val traceJson = new JsonTraceCodec().encode(trace)
    // Generate pre-fill script for onload Trace JSON
    val preFillScript =
      """
        |<base href="%s">
        |<script>
        |  var ESC_FLAGS = "gi";
        |  var EMBED_ESCAPES = __EMBED_ESCAPES__;
        |  var unescapeForEmbedding = function (str) {
        |    for (var key in EMBED_ESCAPES) {
        |      if (EMBED_ESCAPES.hasOwnProperty(key)) {
        |        str = str.replace(new RegExp(EMBED_ESCAPES[key], ESC_FLAGS), key);
        |      }
        |    }
        |    return str;
        |  };
        |  var getEmbeddedContent = function(id) {
        |    var contentElem = document.getElementById(id);
        |    var innerContent = contentElem.firstChild.nodeValue;
        |    return JSON.parse(unescapeForEmbedding(innerContent));
        |  };
        |  window.onload = function() {
        |    var json = getEmbeddedContent('injected-json');
        |    // The renderTrace method does not yet support normal JS objects, but expects stringified JSON
        |    renderTrace(JSON.stringify(json));
        |  }
        |</script>
      """.stripMargin.format(httpConfiguration.context.stripSuffix("/") + TracevisRoot + "/")
    // Generate injected JSON placeholder
    val injectedJson = """<code id="injected-json"><!--__JSON__--></code>"""
    // Build HTML page
    environment.resourceAsStream(new File(TracevisRoot, TraceName).getPath).map(stream => {
      // Escape script and JSON
      val script = preFillScript.replace("__EMBED_ESCAPES__", """{"&":"&amp;","-":"&dsh;"}""")
      val json = injectedJson.replace("__JSON__", ListMap("&" -> "&amp;", "-" -> "&dsh;").foldLeft(traceJson)((acc, escape) => acc.replaceAll(escape._1, escape._2)))
      // Inject script and JSON
      Source.fromInputStream(stream).mkString.replace("<title>", script + "\n<title>").replace("</style>", "</style>\n" + json)
    }).orNull
  }

} 
Example 38
Source File: View.scala    From sangria-subscriptions-example   with Apache License 2.0 5 votes vote down vote up
package generic

import language.postfixOps

import akka.actor.{ActorLogging, ActorRef}
import akka.stream.actor.{OneByOneRequestStrategy, ActorSubscriber}

import akka.stream.actor.ActorSubscriberMessage._

import scala.collection.immutable.ListMap
import scala.concurrent.duration._

abstract class View[Entity <: Versioned, Ev <: Event] extends ActorSubscriber with ActorLogging {
  import View._

  private var entities = ListMap.empty[String, Entity]
  private var waiting = Map.empty[(String, Long), ActorRef]

  import context.dispatcher

  def receive = {
    case OnNext(event: Event) if handleEvent.isDefinedAt(event.asInstanceOf[Ev]) ⇒
      handleEvent(event.asInstanceOf[Ev])

      val waitingKey = event.id → event.version

      waiting.get(waitingKey) foreach { senderRef ⇒
        senderRef ! entities.get(event.id)
        waiting = waiting.filterNot(_._1 == waitingKey)
      }
    case RemoveWaiting(key) ⇒
      waiting.get(key) foreach { senderRef ⇒
        senderRef ! None
        waiting = waiting.filterNot(_._1 == key)
      }
    case List(offset, limit) ⇒
      sender() ! entities.values.slice(offset, offset + limit)
    case Get(id, None) ⇒
      sender() ! entities.get(id)
    case GetMany(ids) ⇒
      sender() ! entities.collect{case (key, value) if ids.contains(key) ⇒ value}.toVector
    case Get(id, Some(version)) ⇒
      entities.get(id) match {
        case Some(entity) if entity.version == version ⇒
          sender() ! entities.get(id)
        case _ ⇒
          waiting = waiting + ((id → version) → sender())
          context.system.scheduler.scheduleOnce(5 seconds, self, RemoveWaiting(id → version))
      }
  }

  def add(entity: Entity) =
    entities = entities + (entity.id → entity)

  def update(event: Ev)(fn: Entity ⇒ Entity) =
    change(event)(entity ⇒ entities = entities.updated(entity.id, fn(entity)))

  def delete(event: Ev) =
    change(event)(entity ⇒ entities = entities.filterNot(_._1 == entity.id))

  private def change(event: Ev)(fn: Entity ⇒ Unit) =
    entities.get(event.id) match {
      case Some(entity) if entity.version != event.version - 1 ⇒
        log.error(s"Entity ${event.id}: version mismatch: expected ${entity.version + 1}, but got ${event.version}")
      case Some(entity) ⇒
        fn(entity)
      case None ⇒
        log.error(s"Entity ${event.id}: not found")
    }

  val requestStrategy = OneByOneRequestStrategy

  def handleEvent: Handler

  type Handler = PartialFunction[Ev, Unit]
}

object View {
  case class List(offset: Int, limit: Int)
  case class Get(id: String, version: Option[Long] = None)
  case class GetMany(ids: Seq[String])

  private case class RemoveWaiting(key: (String, Long))
} 
Example 39
Source File: Access.scala    From spatial   with MIT License 5 votes vote down vote up
package fringe.utils

import scala.collection.immutable.ListMap
import fringe.Ledger._
import emul.ResidualGenerator.ResidualGenerator

object AccessHelper {
  def singular(bitWidth: Int): Access = Access(0, 0, 0, List(0), List(0), None, PortInfo(None, 1, 1, List(1), bitWidth, List(List(ResidualGenerator(1,0,0)))))
  def singularOnPort(port: Int, bitWidth: Int): Access = Access(port, 0, 0, List(0), List(0), None, PortInfo(Some(port), 1, 1, List(1), bitWidth, List(List(ResidualGenerator(1,0,0)))))
}

case class PortInfo(
  val bufPort: Option[Int],
  val portWidth: Int,
  val ofsWidth: Int,
  val banksWidth: List[Int],
  val dataWidth: Int,
  val visibleBanks: List[List[ResidualGenerator]]
){
  def randomBanks: PortInfo = PortInfo(bufPort, portWidth, ofsWidth, banksWidth, dataWidth, List.tabulate(portWidth){i => List.tabulate(banksWidth.size){j => ResidualGenerator(1,0,0)}})
}
case class Access(
  val accHash: OpHash,
  val muxPort: Int,
  val muxOfs: Int,
  val castgroup: List[Int],
  val broadcast: List[Int],
  val shiftAxis: Option[Int],
  val port: PortInfo
) {
  def par: Int = castgroup.size
  def coreBroadcastVisibleBanks: List[(List[ResidualGenerator], Int)] = port.visibleBanks.zipWithIndex.collect{case (vb, i) if (broadcast(i) == 0) => (vb, i)}
  def randomBanks: Access = Access(accHash, muxPort, muxOfs, castgroup, broadcast, shiftAxis, port.randomBanks)
} 
Example 40
Source File: HVec.scala    From spatial   with MIT License 5 votes vote down vote up
// See LICENSE for license details.

package fringe.utils

import chisel3._
import scala.collection.immutable.ListMap

class HVec[T<:Data](wires: Seq[T]) extends Record with collection.IndexedSeq[T] {
  def apply(x: Int): T = wires(x)
  val elements = ListMap(wires.zipWithIndex.map { case (n,i) => (i.toString, n) }:_*)
  def length: Int = wires.length

  def even: Seq[T] = wires.zipWithIndex.collect{case (w,i) if i % 2 == 0 => w}
  def odd: Seq[T] = wires.zipWithIndex.collect{case (w,i) if i % 2 == 1 => w}

  override def cloneType: this.type = new HVec(wires.map(_.cloneType)).asInstanceOf[this.type]
}

object HVec {
  def apply[T<:Data](wires: Seq[T]) = new HVec(wires)
  def tabulate[T<:Data](size: Int)(gen: Int => T) = HVec(Seq.tabulate(size) { i => gen(i)})
} 
Example 41
Source File: HealthComponent.scala    From wookiee   with Apache License 2.0 5 votes vote down vote up
package com.webtrends.harness.health


case class HealthComponent(name: String,
                           state: ComponentState = ComponentState.NORMAL,
                           details: String,
                           extra: Option[AnyRef] = None,
                           var components: List[HealthComponent] = List.empty) extends JsonSerializable {
  override def toJson(): String = {
    val extraDetails = extra match {
      case Some(s) => s.toString
      case None => ""
    }

    val props = ListMap[String, Any](
      "name" -> name,
      "state" -> state.toString,
      "details" -> details,
      "extra" -> extraDetails,
      "components" -> components
    )
    Json.build(props, sort = false).toString
  }

  def addComponent(component: HealthComponent): Unit = components = components :+ component
} 
Example 42
Source File: ApplicationHealth.scala    From wookiee   with Apache License 2.0 5 votes vote down vote up
package com.webtrends.harness.health

import ComponentState.ComponentState
import com.webtrends.harness.utils.{Json, JsonSerializable}
import org.joda.time.DateTime

import scala.collection.immutable.ListMap

case class ApplicationHealth(applicationName: String,
                             version: String,
                             time: DateTime,
                             state: ComponentState,
                             details: String,
                             components: Seq[HealthComponent]) extends JsonSerializable {
  override def toJson(): String = {
    val props = ListMap[String, Any](
      "applicationName" -> applicationName,
      "startedTime" -> time,
      "version" -> version,
      "state" -> state.toString,
      "details" -> details,
      "components" -> components
    )
    Json.build(props, sort = false).toString
  }
}

case class ComponentHealth(state: ComponentState, details: String) 
Example 43
Source File: ProgrammaticBundle.scala    From barstools   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package barstools.tapeout.transforms

import chisel3._
import scala.collection.immutable.ListMap

class CustomBundle[T <: Data](elts: (String, T)*) extends Record {
  val elements = ListMap(elts map { case (field, elt) => field -> chiselTypeOf(elt) }: _*)
  def apply(elt: String): T = elements(elt)
  def apply(elt: Int): T = elements(elt.toString)
  override def cloneType = (new CustomBundle(elements.toList: _*)).asInstanceOf[this.type]
}

class CustomIndexedBundle[T <: Data](elts: (Int, T)*) extends Record {
  // Must be String, Data
  val elements = ListMap(elts map { case (field, elt) => field.toString -> chiselTypeOf(elt) }: _*)
  // TODO: Make an equivalent to the below work publicly (or only on subclasses?)
  def indexedElements = ListMap(elts map { case (field, elt) => field -> chiselTypeOf(elt) }: _*)
  def apply(elt: Int): T = elements(elt.toString)
  override def cloneType = (new CustomIndexedBundle(indexedElements.toList: _*)).asInstanceOf[this.type]
}

object CustomIndexedBundle {
  def apply[T <: Data](gen: T, idxs: Seq[Int]) = new CustomIndexedBundle(idxs.map(_ -> gen): _*)
  // Allows Vecs of elements of different types/widths
  def apply[T <: Data](gen: Seq[T]) = new CustomIndexedBundle(gen.zipWithIndex.map{ case (elt, field) => field -> elt }: _*)
} 
Example 44
Source File: OrderPreserving.scala    From play-ws   with Apache License 2.0 5 votes vote down vote up
package play.api.libs.ws.ahc

import scala.collection.immutable.ListMap
import scala.collection.mutable

private[ahc] object OrderPreserving {

  def groupBy[K, V](seq: Seq[(K, V)])(f: ((K, V)) => K): Map[K, Seq[V]] = {
    // This mutable map will not retain insertion order for the seq, but it is fast for retrieval. The value is
    // a builder for the desired Seq[String] in the final result.
    val m = mutable.Map.empty[K, mutable.Builder[V, Seq[V]]]

    // Run through the seq and create builders for each unique key, effectively doing the grouping
    for ((key, value) <- seq) m.getOrElseUpdate(key, Seq.newBuilder[V]) += value

    // Create a builder for the resulting ListMap. Note that this one is immutable and will retain insertion order
    val b = ListMap.newBuilder[K, Seq[V]]

    // Note that we are NOT going through m (didn't retain order) but we are iterating over the original seq
    // just to get the keys so we can look up the values in m with them. This is how order is maintained.
    for ((k, v) <- seq.iterator) b += k -> m.getOrElse(k, Seq.newBuilder[V]).result

    // Get the builder to produce the final result
    b.result
  }
} 
Example 45
Source File: NodeShape.scala    From mleap   with Apache License 2.0 5 votes vote down vote up
package ml.combust.mleap.core.types

import scala.collection.immutable.ListMap


case class Socket(port: String, name: String)

object NodeShape {
  def apply(inputs: Seq[Socket], outputs: Seq[Socket]): NodeShape = {
    val imap = ListMap(inputs.map(s => (s.port, s)): _*)
    val omap = ListMap(outputs.map(s => (s.port, s)): _*)

    NodeShape(imap, omap)
  }

  def feature(inputPort: String = "input",
              outputPort: String = "output",
              inputCol: String = "input",
              outputCol: String = "output"): NodeShape = {
    NodeShape().withInput(inputPort, inputCol).
          withOutput(outputPort, outputCol)
  }

  def regression(featuresCol: String = "features",
                 predictionCol: String = "prediction"): NodeShape = {
    NodeShape().withInput("features", featuresCol).
          withOutput("prediction", predictionCol)
  }

  def basicClassifier(featuresCol: String = "features",
                      predictionCol: String = "prediction"): NodeShape = {
    NodeShape().withInput("features", featuresCol).
          withOutput("prediction", predictionCol)
  }

  def probabilisticClassifier(featuresCol: String = "features",
                              predictionCol: String = "prediction",
                              rawPredictionCol: Option[String] = None,
                              probabilityCol: Option[String] = None): NodeShape = {
    var ns = NodeShape().withInput("features", featuresCol)

    for(rp <- rawPredictionCol) { ns = ns.withOutput("raw_prediction", rp) }
    for(p <- probabilityCol) { ns = ns.withOutput("probability", p) }

    ns.withOutput("prediction", predictionCol)
  }

  def basicCluster(featuresCol: String = "features",
                   predictionCol: String = "prediction"): NodeShape = {
    NodeShape().withInput("features", featuresCol).
          withOutput("prediction", predictionCol)
  }

  def probabilisticCluster(featuresCol: String = "features",
                           predictionCol: String = "prediction",
                           probabilityCol: Option[String] = None): NodeShape = {
    var ns = NodeShape().withInput("features", featuresCol)

    for(p <- probabilityCol) { ns = ns.withOutput("probability", p) }

    ns.withOutput("prediction", predictionCol)
  }
}

case class NodeShape(inputs: ListMap[String, Socket] = ListMap(),
                     outputs: ListMap[String, Socket] = ListMap()) {
  def getInput(port: String): Option[Socket] = inputs.find(_._1 == port).map(_._2)
  def getOutput(port: String): Option[Socket] = outputs.find(_._1 == port).map(_._2)

  def input(port: String): Socket = getInput(port).get
  def output(port: String): Socket = getOutput(port).get

  def standardInput: Socket = input("input")
  def standardOutput: Socket = output("output")

  def withInput(port: String, name: String): NodeShape = {
    copy(inputs = inputs + (port -> Socket(port, name)))
  }

  def withOutput(port: String, name: String): NodeShape = {
    copy(outputs = outputs + (port -> Socket(port, name)))
  }

  def withStandardInput(name: String): NodeShape = {
    withInput("input", name)
  }

  def withStandardOutput(name: String): NodeShape = {
    withOutput("output", name)
  }
} 
Example 46
Source File: CategoryFeature.scala    From jigg   with Apache License 2.0 5 votes vote down vote up
package jigg.nlp.ccg.lexicon


trait CategoryFeature {
  def kvs: Seq[(String, String)]
  def unify(lhs: CategoryFeature): Boolean = false // TODO: implement
}

@SerialVersionUID(-8236395926230742650L)
case class JPCategoryFeature(values: Seq[String]) extends CategoryFeature {
  import JPCategoryFeature._

  override def kvs = keys zip values
  override def toString = kvs.filter(_._2 != "").map { case (k, v) => k + "=" + v }.mkString(",")
}

object JPCategoryFeature {
  // This is a hard-coded mapping of feature structure of Japanese category.
  private val k2vals = Map(
    "mod" -> Array("adv", "adn", "nm"),
    "form" -> Array("attr", "base", "cont", "hyp", "imp",
      "beg", "stem", "ta", "te", "pre", "r", "neg", "s", "da"),
    "case" -> Array("ga", "o", "ni", "to", "nc", "caus"),
    "fin" -> Array("f", "t"))

  private val keys = k2vals.keys.toSeq
  private val v2keyIdx = {
    val key2idx = keys.zipWithIndex.toMap
    k2vals.flatMap { case (key, vals) =>
      vals.map { v => v -> key2idx(key) }
    }
  }
  val kvpair = """\w+=(\w+)""".r

  def createFromValues(values: Seq[String]) = values match {
    case Seq() => emptyFeature
    case _ =>
      val sortedValues = Array.fill(keys.size)("")
      values.filter(_!="").foreach { value =>
        val v = value match { case kvpair(v) => v; case v => v }

        if (v(0) != 'X')
          v2keyIdx(v) match { case i => sortedValues(i) = v }
      }
      JPCategoryFeature(sortedValues)
  }
  // We cache this because most categories don't have a feature
  private val emptyFeature = JPCategoryFeature(Array.fill(keys.size)(""))
}

case class EnCategoryFeature(values: Seq[String]) extends CategoryFeature {
  override def kvs = values.zipWithIndex.map { case (v, k) => (k.toString, v) }
  override def toString = values.mkString(",")
}

object EnCategoryFeature {
  def createFromValues(values: Seq[String]) = EnCategoryFeature(values.sortWith(_ < _))
} 
Example 47
Source File: Output.scala    From cloudformation-template-generator   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.monsanto.arch.cloudformation.model

import spray.json._
import DefaultJsonProtocol._
import scala.reflect.ClassTag

import scala.collection.immutable.ListMap



case class Output[R](name: String, Description: Option[String] = None, Value: Token[R], Export: Option[Token[String]] = None)(implicit format: JsonFormat[Token[R]]) {
  def valueAsJson = Token.format[Token[R]].write(Value)
}

object Output extends DefaultJsonProtocol {
  implicit def format[A] : JsonWriter[Output[A]] = new JsonWriter[Output[A]]  {
    override def write(obj: Output[A]) = {
      val m1 = obj.Description map (d => Map("Description" -> JsString(d))) getOrElse Map.empty[String, JsValue]
      JsObject(m1 ++ Map("Value" -> obj.valueAsJson) ++ exportAsJsonTuple(obj))
    }

    def exportAsJsonTuple(obj: Output[A]): Option[(String, JsValue)] = obj.Export match {
      case None => None
      case Some(x) => "Export" -> JsObject("Name" -> x.toJson)
    }
  }

  implicit val seqFormat: JsonWriter[Seq[Output[_]]] = new JsonWriter[Seq[Output[_]]] {
    def write(objs: Seq[Output[_]]) = JsObject(ListMap(objs.map(o => o.name -> format.write(o)): _*))
  }
} 
Example 48
package org.sparksamples

import scala.collection.immutable.ListMap
import scalax.chart.module.ChartFactories
import java.awt.Font
import org.jfree.chart.axis.CategoryLabelPositions


object CountByRatingChart {

  def main(args: Array[String]) {
    val rating_data_raw = Util.sc.textFile("../../data/ml-100k/u.data")
    val rating_data = rating_data_raw.map(line => line.split("\t"))
    val ratings = rating_data.map(fields => fields(2).toInt)
    val ratings_count = ratings.countByValue()

    val sorted =  ListMap(ratings_count.toSeq.sortBy(_._1):_*)
    val ds = new org.jfree.data.category.DefaultCategoryDataset
    sorted.foreach{ case (k,v) => ds.addValue(v,"Rating Values", k)}

    val chart = ChartFactories.BarChart(ds)
    val font = new Font("Dialog", Font.PLAIN,5);

    chart.peer.getCategoryPlot.getDomainAxis().
      setCategoryLabelPositions(CategoryLabelPositions.UP_90);
    chart.peer.getCategoryPlot.getDomainAxis.setLabelFont(font)
    chart.show()
    Util.sc.stop()
  }
} 
Example 49
package org.sparksamples

import scala.collection.immutable.ListMap
import scalax.chart.module.ChartFactories
import java.awt.Font
import org.jfree.chart.axis.CategoryLabelPositions


object UserOccupationChart {

  def main(args: Array[String]) {
    val user_data = Util.getUserData()
    val user_fields = user_data.map(l => l.split("\\|"))
    val count_by_occupation = user_fields.map( fields => (fields(3), 1)).
      reduceByKey( (x, y) => x + y).collect()
    println(count_by_occupation)

    val sorted =  ListMap(count_by_occupation.toSeq.sortBy(_._2):_*)
    val ds = new org.jfree.data.category.DefaultCategoryDataset
    sorted.foreach{ case (k,v) => ds.addValue(v,"UserAges", k)}

    val chart = ChartFactories.BarChart(ds)
    val font = new Font("Dialog", Font.PLAIN,5);

    chart.peer.getCategoryPlot.getDomainAxis().
      setCategoryLabelPositions(CategoryLabelPositions.UP_90);
    chart.peer.getCategoryPlot.getDomainAxis.setLabelFont(font)
    chart.show()
    Util.sc.stop()
  }
} 
Example 50
Source File: UserAgesChart.scala    From Machine-Learning-with-Spark-Second-Edition   with MIT License 5 votes vote down vote up
package org.sparksamples

import scala.collection.immutable.ListMap
import scalax.chart.module.ChartFactories


object UserAgesChart {

  def main(args: Array[String]) {
    val user_data = Util.getUserData()
    val user_fields = user_data.map(l => l.split("\\|"))
    val ages = user_fields.map( x => (x(1).toInt)).collect()
    println(ages.getClass.getName)
    val min = 0
    val max = 80
    val bins = 16
    val step = (80/bins).toInt

    var mx = Map(0 -> 0)
    for (i <- step until (max + step) by step) {
      mx += (i -> 0);
    }

    for(i <- 0 until ages.length){
      for (j <- 0 until (max + step) by step) {
        if(ages(i) >= (j) && ages(i) < (j + step)){
          mx = mx + (j -> (mx(j) + 1))
        }
      }
    }

    val mx_sorted =  ListMap(mx.toSeq.sortBy(_._1):_*)
    val ds = new org.jfree.data.category.DefaultCategoryDataset
    mx_sorted.foreach{ case (k,v) => ds.addValue(v,"UserAges", k)}

    val chart = ChartFactories.BarChart(ds)

    chart.show()
    Util.sc.stop()
  }
} 
Example 51
package org.sparksamples

import scala.collection.immutable.ListMap
import scalax.chart.module.ChartFactories


object UserRatingsChart {

  def main(args: Array[String]) {
    val user_data = Util.getUserData()
    val user_fields = user_data.map(l => l.split("\\|"))
    val ages = user_fields.map( x => (x(1).toInt)).collect()

    val rating_data_raw = Util.sc.textFile("../../data/ml-100k/u.data")
    val rating_data = rating_data_raw.map(line => line.split("\t"))
    val user_ratings_grouped = rating_data.map(
      fields => (fields(0).toInt, fields(2).toInt)).groupByKey()
    val user_ratings_byuser = user_ratings_grouped.map(v =>  (v._1,v._2.size))
    val user_ratings_byuser_local = user_ratings_byuser.map(v =>  v._2).collect()
    val input = user_ratings_byuser_local
    val min = 0
    val max = 500
    val bins = 200
    val step = (max/bins).toInt

    var mx = Map(0 -> 0)
    for (i <- step until (max + step) by step) {
      mx += (i -> 0);
    }

    for(i <- 0 until input.length){
      for (j <- 0 until (max + step) by step) {
        if(ages(i) >= (j) && input(i) < (j + step)){
          mx = mx + (j -> (mx(j) + 1))
        }
      }
    }

    val mx_sorted =  ListMap(mx.toSeq.sortBy(_._1):_*)
    val ds = new org.jfree.data.category.DefaultCategoryDataset
    mx_sorted.foreach{ case (k,v) => ds.addValue(v,"Ratings", k)}

    val chart = ChartFactories.BarChart(ds)

    chart.show()
    Util.sc.stop()
  }
} 
Example 52
Source File: MovieAgesChart.scala    From Machine-Learning-with-Spark-Second-Edition   with MIT License 5 votes vote down vote up
package org.sparksamples

import scala.collection.immutable.ListMap
import scalax.chart.module.ChartFactories


object MovieAgesChart {

  def main(args: Array[String]) {
    val movie_data = Util.getMovieData()
    val movie_ages = Util.getMovieAges(movie_data)
    val movie_ages_sorted = ListMap(movie_ages.toSeq.sortBy(_._1):_*)
    val ds = new org.jfree.data.category.DefaultCategoryDataset
    movie_ages_sorted foreach (x => ds.addValue(x._2,"Movies", x._1))
    //0 -> 65, 1 -> 286, 2 -> 355, 3 -> 219, 4 -> 214, 5 -> 126
    val chart = ChartFactories.BarChart(ds)
    chart.show()
    Util.sc.stop()
  }
} 
Example 53
Source File: PlotLogData.scala    From Machine-Learning-with-Spark-Second-Edition   with MIT License 5 votes vote down vote up
package org.sparksamples

//import org.sparksamples.Util

//import _root_.scalax.chart.ChartFactories
import java.awt.Font

import org.jfree.chart.axis.CategoryLabelPositions

import scala.collection.immutable.ListMap
import scalax.chart.module.ChartFactories



object PlotLogData {

  def main(args: Array[String]) {
    val records = Util.getRecords()._1
    val records_x = records.map(r => Math.log(r(r.length -1).toDouble))
    var records_int = new Array[Int](records_x.collect().length)
    print(records_x.first())
    val records_collect = records_x.collect()

    for (i <- 0 until records_collect.length){
      records_int(i) = records_collect(i).toInt
    }
    val min_1 = records_int.min
    val max_1 = records_int.max

    val min = min_1.toFloat
    val max = max_1.toFloat
    val bins = 10
    val step = (max/bins).toFloat

    var mx = Map(0.0.toString -> 0)
    for (i <- step until (max + step) by step) {
      mx += (i.toString -> 0);
    }

    for(i <- 0 until records_collect.length){
      for (j <- 0.0 until (max + step) by step) {
        if(records_int(i) >= (j) && records_int(i) < (j + step)){
          mx = mx + (j.toString -> (mx(j.toString) + 1))
        }
      }
    }
    val mx_sorted = ListMap(mx.toSeq.sortBy(_._1.toFloat):_*)
    val ds = new org.jfree.data.category.DefaultCategoryDataset
    var i = 0
    mx_sorted.foreach{ case (k,v) => ds.addValue(v,"", k)}

    val chart = ChartFactories.BarChart(ds)
    val font = new Font("Dialog", Font.PLAIN,4);

    chart.peer.getCategoryPlot.getDomainAxis().
      setCategoryLabelPositions(CategoryLabelPositions.UP_90);
    chart.peer.getCategoryPlot.getDomainAxis.setLabelFont(font)
    chart.show()
    Util.sc.stop()
  }
} 
Example 54
Source File: PlotRawData.scala    From Machine-Learning-with-Spark-Second-Edition   with MIT License 5 votes vote down vote up
package org.sparksamples

//import org.sparksamples.Util

//import _root_.scalax.chart.ChartFactories
import java.awt.Font

import org.jfree.chart.axis.CategoryLabelPositions

import scala.collection.immutable.ListMap
import scalax.chart.module.ChartFactories



object PlotRawData {

  def main(args: Array[String]) {
    val records = Util.getRecords()._1
    val records_x = records.map(r => r(r.length -1))
    var records_int = new Array[Int](records_x.collect().length)
    print(records_x.first())
    val records_collect = records_x.collect()

    for (i <- 0 until records_collect.length){
      records_int(i) = records_collect(i).toInt
    }
    val min_1 = records_int.min
    val max_1 = records_int.max

    val min = min_1
    val max = max_1
    val bins = 40
    val step = (max/bins).toInt

    var mx = Map(0 -> 0)
    for (i <- step until (max + step) by step) {
      mx += (i -> 0);
    }

    for(i <- 0 until records_collect.length){
      for (j <- 0 until (max + step) by step) {
        if(records_int(i) >= (j) && records_int(i) < (j + step)){
          print(mx(j))
          print(mx)
          mx = mx + (j -> (mx(j) + 1))
        }
      }
    }
    val mx_sorted = ListMap(mx.toSeq.sortBy(_._1):_*)
    val ds = new org.jfree.data.category.DefaultCategoryDataset
    var i = 0
    mx_sorted.foreach{ case (k,v) => ds.addValue(v,"", k)}

    val chart = ChartFactories.BarChart(ds)
    val font = new Font("Dialog", Font.PLAIN,4);

    chart.peer.getCategoryPlot.getDomainAxis().
      setCategoryLabelPositions(CategoryLabelPositions.UP_90);
    chart.peer.getCategoryPlot.getDomainAxis.setLabelFont(font)
    chart.show()
    Util.sc.stop()
  }
} 
Example 55
Source File: EidosShell.scala    From eidos   with Apache License 2.0 5 votes vote down vote up
package org.clulab.wm.eidos.apps

import com.typesafe.config.Config
import org.clulab.wm.eidos.EidosSystem
import org.clulab.wm.eidos.serialization.web.WebSerializer
import org.clulab.wm.eidos.utils.DisplayUtils.displayMentions
import org.clulab.wm.eidos.utils.{CliReader, IdeReader}

import scala.collection.immutable.ListMap



object EidosShell extends App {
  val reader = {
    val prompt = "(Eidos)>>> "

    if (args.length == 0) new CliReader(prompt, "user.home", ".eidosshellhistory")
    else new IdeReader(prompt)
  }
  val commands = ListMap(
    ":help" -> "show commands",
    ":reload" -> "reload grammar",
    ":exit" -> "exit system"
  )
  val eidosConfig: Config = EidosSystem.defaultConfig
  var ieSystem = new EidosSystem(eidosConfig)
  val webSerializer = new WebSerializer(ieSystem, eidosConfig)

  println("\nWelcome to the Eidos Shell!")
  printCommands()

  def processMenu: Boolean = {
    val continue = Option(reader.readLine).map { line =>
      if (line == ":exit") false
      else {
        line match {
          case ":help" => printCommands()
          case ":reload" => ieSystem = new EidosSystem(EidosSystem.defaultConfig, Some(ieSystem))
          case text => extractFrom(text)
        }
        true
      }
    }.getOrElse(false)

    continue
  }

  while (processMenu) { }

  // summarize available commands
  def printCommands(): Unit = {
    println("\nCOMMANDS:")
    for ((cmd, msg) <- commands)
      println(s"\t$cmd\t=> $msg")
    println()
  }

  def extractFrom(text: String): Unit = {
    val annotatedDocument = ieSystem.extractFromText(text)
    val doc = annotatedDocument.document
    val mentions = annotatedDocument.odinMentions
    val sortedMentions = mentions.sortBy(m => (m.sentence, m.getClass.getSimpleName))

    webSerializer.serialize(annotatedDocument, cagRelevantOnly = true, "eidosshell.html")
    displayMentions(sortedMentions, doc, true)
  }
} 
Example 56
Source File: JGenCodecTest.scala    From scala-commons   with MIT License 5 votes vote down vote up
package com.avsystem.commons
package serialization

import java.lang.annotation.RetentionPolicy

import scala.collection.immutable.ListMap

class JGenCodecTest extends JCodecTestBase with SimpleIOCodecTest {
  test("java collection test (TreeMap)") {
    testWrite[JSortedMap[String, Int]](jTreeMap, ListMap("1" -> 1, "2" -> 2, "3" -> 3))
    testWrite[JNavigableMap[String, Int]](jTreeMap, ListMap("1" -> 1, "2" -> 2, "3" -> 3))
    testWrite[JTreeMap[String, Int]](jTreeMap, ListMap("1" -> 1, "2" -> 2, "3" -> 3))
  }

  test("java enum test") {
    testWrite(RetentionPolicy.RUNTIME, "RUNTIME")
    testWrite(RetentionPolicy.SOURCE, "SOURCE")
  }
} 
Example 57
Source File: GenCodecErrorsTest.scala    From scala-commons   with MIT License 5 votes vote down vote up
package com.avsystem.commons
package serialization

import com.avsystem.commons.serialization.GenCodec.{ReadFailure, WriteFailure}
import org.scalatest.funsuite.AnyFunSuite

import scala.collection.immutable.ListMap

case class Inner(int: Int)
object Inner extends HasGenCodec[Inner]

class Unwritable
object Unwritable {
  implicit val codec: GenCodec[Unwritable] = GenCodec.create(
    _ => throw new ReadFailure("cannot"),
    (_, _) => throw new WriteFailure("cannot")
  )
}

sealed trait Base
case class Outer(inner: Inner) extends Base
case class Other(unwritable: Unwritable) extends Base
object Base extends HasGenCodec[Base]

class GenCodecErrorsTest extends AnyFunSuite {
  def causeChain(t: Throwable): List[Throwable] =
    if (t == null) Nil
    else t :: causeChain(t.getCause)

  test("deep reading failure test") {
    val failure = intercept[ReadFailure] {
      SimpleValueInput.read[Base](ListMap("Outer" -> ListMap("inner" -> ListMap("int" -> "NOT INT"))))
    }
    assert(causeChain(failure).size == 4)
  }

  test("deep writing failure test") {
    val failure = intercept[WriteFailure] {
      SimpleValueOutput.write[Base](Other(new Unwritable))
    }
    assert(causeChain(failure).size == 3)
  }
} 
Example 58
Source File: PutInstTest.scala    From vm   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.mmadt.processor.inst.sideeffect

import org.mmadt.language.obj.value.StrValue
import org.mmadt.language.obj.{Obj, Rec}
import org.mmadt.storage.StorageFactory._
import org.scalatest.FunSuite

import scala.collection.immutable.ListMap


class PutInstTest extends FunSuite {
  test("[put] w/ rec value") {
    val marko: Rec[StrValue, Obj] = rec(str("name") -> str("marko"))
    val markoFull = marko.put(str("age"), int(29))
    assertResult(rec(str("name") -> str("marko"), str("age") -> int(29)))(markoFull)
    assertResult(rec(str("name") -> str("marko"), str("age") -> int(29)))(markoFull.put(str("name"), str("marko")))
    assertResult(rec(str("name") -> str("kuppitz"), str("age") -> int(29)))(markoFull.put(str("name"), str("kuppitz")))
    assertResult(rec(str("name") -> str("marko"), str("age") -> int(28)))(markoFull.put(str("age"), int(28)))
    // test rec key/value ordering
    assertResult(ListMap(str("name") -> str("kuppitz"), str("age") -> int(29)))(markoFull.put(str("name"), str("kuppitz")).gmap)
    assertResult(ListMap(str("name") -> str("marko"), str("age") -> int(28)))(markoFull.put(str("age"), int(28)).gmap)
    assertResult(int(29))(markoFull.get(str("age")))
  }
} 
Example 59
Source File: SchemaSetIncludesAndImportsMixins.scala    From incubator-daffodil   with Apache License 2.0 5 votes vote down vote up
package org.apache.daffodil.dsom

import scala.collection.immutable.ListMap
import org.apache.daffodil.util._
import IIUtils._
import org.apache.daffodil.xml.XMLUtils
import org.apache.daffodil.util.Delay
import org.apache.daffodil.api.WarnID


  lazy val fakeXMLSchemaDocument = {
    val xsd = XMLUtils.XSD_NAMESPACE.toString

    // Any time we synthesize xml we have to grab the namespace definitions and
    // make sure we drag them along onto the new structures.
    val fakeImportStatementsXML =
      <xs:import schemaLocation={ schemaSource.uriForLoading.toString } xmlns:xs={ xsd }/>

    val fakeSchemaDocXML =
      <xs:schema xmlns:xs={ xsd }>{ fakeImportStatementsXML }</xs:schema>

    val initialEmptyIIMap: IIMap = Delay(ListMap.empty)

    val fakeSD = new XMLSchemaDocument(fakeSchemaDocXML, self, None, None, initialEmptyIIMap, isBootStrapSD = true)
    fakeSD
  }

  def allSchemaDocuments = LV('allSchemaDocuments) {
    allSchemaFiles.map { _.iiSchemaDocument }
  }.value

  def allSchemaFiles = LV('allSchemaFiles) {
    val fd = fakeXMLSchemaDocument //bootstrap
    val sa = fd.seenAfter
    val sfl = sa.value.flatMap {
      case (_, ii) => {
        val sf = ii.iiSchemaFileMaybe // maybe not if we've already seen this file for the same namespace.
        sf.filter { f =>
          if (f.isDFDLSchemaFile)
            true
          else {
            f.SDW(WarnID.IgnoreImport, "Non-DFDL Schema file ignored. Does not have DFDL namespace definition on schema root element.\n" +
              "Add xmlns:dfdl='%s' to the root element if this file must be part of the DFDL schema.", XMLUtils.DFDL_NAMESPACE)
            false
          }
        }
      }
    }.toList
    sfl
  }.value

} 
Example 60
Source File: TestListMap.scala    From incubator-daffodil   with Apache License 2.0 5 votes vote down vote up
package org.apache.daffodil.util

import org.junit.Assert.assertEquals
import org.junit.Test
import scala.collection.immutable.ListMap
import scala.util.Random

class TestListMap {

  
  @Test def test_listMap = {
    val orig = Random.shuffle((0 until 1000).toList)
    val mt: ListMap[Int, String] = ListMap.empty
    val listMap: ListMap[Int, String] = orig.foldLeft(mt) {

      (lm, n) => lm + (n -> n.toString)
    }

    // test that removals still maintain order
    val removeKey = Random.nextInt(1000)
    val smallerOrig = orig.filter(_ != removeKey)
    val smallerMap = listMap - removeKey

    val zipped = smallerOrig.zip(smallerMap)
    zipped.foreach { case (o, (k, v)) => assertEquals(o, k) }
  }
} 
Example 61
Source File: PerformanceReport.scala    From ohara   with Apache License 2.0 5 votes vote down vote up
package oharastream.ohara.it.performance

import java.util.Objects

import oharastream.ohara.common.setting.ObjectKey
import oharastream.ohara.common.util.CommonUtils

import scala.collection.immutable.ListMap
import scala.collection.mutable

trait PerformanceReport {
  
  def records: Map[Long, Map[String, Double]]
}

object PerformanceReport {
  def builder = new Builder

  final class Builder private[PerformanceReport] extends oharastream.ohara.common.pattern.Builder[PerformanceReport] {
    private[this] var key: ObjectKey    = _
    private[this] var className: String = _
    private[this] val records           = mutable.Map[Long, Map[String, Double]]()

    def connectorKey(key: ObjectKey): Builder = {
      this.key = Objects.requireNonNull(key)
      this
    }

    def className(className: String): Builder = {
      this.className = CommonUtils.requireNonEmpty(className)
      this
    }

    def resetValue(duration: Long, header: String): Builder = {
      records.put(duration, Map(header -> 0.0))
      this
    }

    def record(duration: Long, header: String, value: Double): Builder = {
      val record = records.getOrElse(duration, Map(header -> 0.0))
      records.put(
        duration,
        record + (header -> (record.getOrElse(header, 0.0) + value))
      )
      this
    }

    override def build: PerformanceReport = new PerformanceReport {
      override val className: String = CommonUtils.requireNonEmpty(Builder.this.className)

      override val records: Map[Long, Map[String, Double]] = ListMap(
        Builder.this.records.toSeq.sortBy(_._1)((x: Long, y: Long) => y.compare(x)): _*
      )

      override def key: ObjectKey = Objects.requireNonNull(Builder.this.key)
    }
  }
}