Skip to content

Commit

Permalink
apply scalariform
Browse files Browse the repository at this point in the history
  • Loading branch information
jrudolph committed Nov 14, 2014
1 parent 2025e58 commit b18574d
Show file tree
Hide file tree
Showing 22 changed files with 198 additions and 186 deletions.
20 changes: 20 additions & 0 deletions project/ScalariformSupport.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import sbt._

import com.typesafe.sbt.SbtScalariform
import com.typesafe.sbt.SbtScalariform.ScalariformKeys

object ScalariformSupport {
lazy val formatSettings = SbtScalariform.scalariformSettings ++ Seq(
ScalariformKeys.preferences in Compile := formattingPreferences,
ScalariformKeys.preferences in Test := formattingPreferences
)

import scalariform.formatter.preferences._
def formattingPreferences =
FormattingPreferences()
.setPreference(RewriteArrowSymbols, true)
.setPreference(AlignParameters, true)
.setPreference(AlignSingleLineCaseStatements, true)
.setPreference(DoubleIndentClassDeclaration, true)

}
2 changes: 2 additions & 0 deletions publish.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ ghpages.settings
git.remoteRepo := "git@github.com:jrudolph/json-lenses.git"

lsSettings

ScalariformSupport.formatSettings
2 changes: 1 addition & 1 deletion src/main/scala/spray/json/lenses/Join.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait Join[M1[_], M2[_], R[_]] {
}

object Join {
def apply[M1[_], M2[_], R[_]](f: ((Ops[M1], Ops[M2])) => Ops[R]): Join[M1, M2, R] = new Join[M1, M2, R] {
def apply[M1[_], M2[_], R[_]](f: ((Ops[M1], Ops[M2])) Ops[R]): Join[M1, M2, R] = new Join[M1, M2, R] {
def get(outer: Ops[M1], inner: Ops[M2]): Ops[R] = f(outer, inner)
}

Expand Down
16 changes: 5 additions & 11 deletions src/main/scala/spray/json/lenses/JsonLenses.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ package lenses
* An aggregate option to import all of the functionality of JsonLenses with one
* import.
*/
object JsonLenses extends
ScalarLenses with
OptionLenses with
SeqLenses with
Operations with
JsonPathIntegration with
ExtraImplicits {
object JsonLenses extends ScalarLenses with OptionLenses with SeqLenses with Operations with JsonPathIntegration with ExtraImplicits {

class OptionalFieldBuilder(fieldName: String) {
def `?`: OptLens = optionalField(fieldName)
Expand All @@ -27,13 +21,13 @@ object JsonLenses extends
*/
def combine[M[_], M2[_], R[_]](outer: Lens[M], inner: Lens[M2])(implicit ev: Join[M2, M, R]): Lens[R] =
new LensImpl[R]()(ev.get(inner.ops, outer.ops)) {
def retr: JsValue => Validated[R[JsValue]] = parent =>
def retr: JsValue Validated[R[JsValue]] = parent
for {
outerV <- outer.retr(parent)
innerV <- ops.allRight(outer.ops.flatMap(outerV)(x => inner.ops.toSeq(inner.retr(x))))
outerV outer.retr(parent)
innerV ops.allRight(outer.ops.flatMap(outerV)(x inner.ops.toSeq(inner.retr(x))))
} yield innerV

def updated(f: SafeJsValue => SafeJsValue)(parent: JsValue): SafeJsValue =
def updated(f: SafeJsValue SafeJsValue)(parent: JsValue): SafeJsValue =
outer.updated(_.flatMap(inner.updated(f)))(parent)
}
}
8 changes: 4 additions & 4 deletions src/main/scala/spray/json/lenses/JsonPath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ object JsonPath {
}
case class Lt(expr1: Expr, expr2: SimpleExpr) extends BinOpPredicate {
def predicate(v1: JsValue, v2: JsValue): Boolean = (v1, v2) match {
case (JsNumber(n1), JsNumber(n2)) => n1 < n2
case _ => false
case (JsNumber(n1), JsNumber(n2)) n1 < n2
case _ false
}
}
case class Gt(expr1: Expr, expr2: SimpleExpr) extends BinOpPredicate {
def predicate(v1: JsValue, v2: JsValue): Boolean = (v1, v2) match {
case (JsNumber(n1), JsNumber(n2)) => n1 > n2
case _ => false
case (JsNumber(n1), JsNumber(n2)) n1 > n2
case _ false
}
}
case class Exists(path: Path) extends Predicate
Expand Down
34 changes: 17 additions & 17 deletions src/main/scala/spray/json/lenses/JsonPathIntegration.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package spray.json
package lenses

trait JsonPathIntegration { self: ScalarLenses with SeqLenses with OptionLenses =>
trait JsonPathIntegration { self: ScalarLenses with SeqLenses with OptionLenses
/**
* Create a Lens from a json-path expression.
*/
Expand All @@ -10,35 +10,35 @@ trait JsonPathIntegration { self: ScalarLenses with SeqLenses with OptionLenses

def fromPath(ast: JsonPath.Path): Lens[Seq] = {
def convertPath(path: JsonPath.Path): Lens[Seq] = path match {
case JsonPath.Root => value.toSeq
case JsonPath.Selection(inner, proj) => convertPath(inner) / convertLens(proj)
case JsonPath.Root value.toSeq
case JsonPath.Selection(inner, proj) convertPath(inner) / convertLens(proj)
}
def convertLens(proj: JsonPath.Projection): Lens[Seq] =
proj match {
case JsonPath.ByField(name) => optionalField(name).toSeq
case JsonPath.ByIndex(i) => element(i).toSeq
case JsonPath.AllElements => elements
case JsonPath.ByPredicate(pred) => filter(convertPredicate(pred))
case JsonPath.ByField(name) optionalField(name).toSeq
case JsonPath.ByIndex(i) element(i).toSeq
case JsonPath.AllElements elements
case JsonPath.ByPredicate(pred) filter(convertPredicate(pred))
}
def convertPredicate(pred: JsonPath.Predicate): JsPred = pred match {
case op: JsonPath.BinOpPredicate =>
case op: JsonPath.BinOpPredicate
val f1 = convertExpr(op.expr1)
val f2 = convertSimpleExpr(op.expr2)

js => {
js {
val v2 = f2(js)
f1(js).right.forall(_.forall(v1 => op.predicate(v1, v2)))
f1(js).right.forall(_.forall(v1 op.predicate(v1, v2)))
}

case JsonPath.Exists(path) =>
js => convertPath(path).retr(js).exists(_.nonEmpty)
case JsonPath.Exists(path)
js convertPath(path).retr(js).exists(_.nonEmpty)
}
def convertExpr(expr: JsonPath.Expr): JsValue => Validated[Seq[JsValue]] = expr match {
case JsonPath.PathExpr(path) => js => convertPath(path).retr(js)
case simple: JsonPath.SimpleExpr => js => Right(Seq(convertSimpleExpr(simple)(js)))
def convertExpr(expr: JsonPath.Expr): JsValue Validated[Seq[JsValue]] = expr match {
case JsonPath.PathExpr(path) js convertPath(path).retr(js)
case simple: JsonPath.SimpleExpr js Right(Seq(convertSimpleExpr(simple)(js)))
}
def convertSimpleExpr(expr: JsonPath.SimpleExpr): JsValue => JsValue = expr match {
case JsonPath.Constant(x) => _ => x
def convertSimpleExpr(expr: JsonPath.SimpleExpr): JsValue JsValue = expr match {
case JsonPath.Constant(x) _ x
}

convertPath(ast)
Expand Down
33 changes: 16 additions & 17 deletions src/main/scala/spray/json/lenses/JsonPathParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.lang.StringBuilder

import org.parboiled.Context
import org.parboiled.scala._
import org.parboiled.errors.{ErrorUtils, ParsingException}
import org.parboiled.errors.{ ErrorUtils, ParsingException }

/**
* A parser for json-path expression as specified here:
Expand All @@ -21,14 +21,14 @@ object JsonPathParser extends Parser with BasicRules {
anyOf("$@") ~ push(JsonPath.Root)
}

def OptionalSelection : ReductionRule1[JsonPath.Path, JsonPath.Path] = rule {
def OptionalSelection: ReductionRule1[JsonPath.Path, JsonPath.Path] = rule {
Projection ~~> JsonPath.Selection ~ OptionalSelection |
EMPTY ~~> identity
EMPTY ~~> identity
}

def Projection: Rule1[JsonPath.Projection] = rule {
"." ~ DotProjection |
"[" ~ BracketProjection ~"]"
"." ~ DotProjection |
"[" ~ BracketProjection ~ "]"
}

def DotProjection: Rule1[JsonPath.Projection] = rule {
Expand All @@ -38,10 +38,10 @@ object JsonPathParser extends Parser with BasicRules {
def ByFieldName = rule { FieldName ~~> JsonPath.ByField }

def BracketProjection: Rule1[JsonPath.Projection] = rule {
Digits ~> (d => JsonPath.ByIndex(d.toInt)) |
SingleQuotedString ~~> JsonPath.ByField |
AllElements |
"?(" ~ WhiteSpace ~ Predicate ~ WhiteSpace ~ ")" ~~> JsonPath.ByPredicate
Digits ~> (d JsonPath.ByIndex(d.toInt)) |
SingleQuotedString ~~> JsonPath.ByField |
AllElements |
"?(" ~ WhiteSpace ~ Predicate ~ WhiteSpace ~ ")" ~~> JsonPath.ByPredicate
}

def Predicate: Rule1[JsonPath.Predicate] = rule {
Expand All @@ -54,19 +54,19 @@ object JsonPathParser extends Parser with BasicRules {
Path ~~> JsonPath.Exists
}

def op[T](op: String)(cons: (JsonPath.Expr, JsonPath.SimpleExpr) => T) =
def op[T](op: String)(cons: (JsonPath.Expr, JsonPath.SimpleExpr) T) =
Expr ~ WhiteSpace ~ op ~ WhiteSpace ~ SimpleExpr ~~> cons

def Expr: Rule1[JsonPath.Expr] = rule {
Path ~~> JsonPath.PathExpr |
SimpleExpr
SimpleExpr
}
def SimpleExpr: Rule1[JsonPath.SimpleExpr] = rule {
JsConstant ~~> JsonPath.Constant
}
def JsConstant: Rule1[JsValue] = rule {
JsonNumber |
SingleQuotedString ~~> (JsString(_))
SingleQuotedString ~~> (JsString(_))
}

val WhiteSpaceChars = " \n\r\t\f"
Expand Down Expand Up @@ -95,16 +95,15 @@ object JsonPathParser extends Parser with BasicRules {

// a set of basic rules taken from the old spray-json parser
// see https://github.com/spray/spray-json/blob/v1.2.6/src/main/scala/spray/json/JsonParser.scala
trait BasicRules { _: Parser =>
def EscapedChar = rule (
trait BasicRules { _: Parser
def EscapedChar = rule(
anyOf("\"\\/") ~:% withContext(appendToSb(_)(_))
| "b" ~ appendToSb('\b')
| "f" ~ appendToSb('\f')
| "n" ~ appendToSb('\n')
| "r" ~ appendToSb('\r')
| "t" ~ appendToSb('\t')
| Unicode ~~% withContext((code, ctx) => appendToSb(code.asInstanceOf[Char])(ctx))
)
| Unicode ~~% withContext((code, ctx) appendToSb(code.asInstanceOf[Char])(ctx)))

def NormalChar = rule { !anyOf("\"\\") ~ ANY ~:% (withContext(appendToSb(_)(_))) }
def Unicode = rule { "u" ~ group(HexDigit ~ HexDigit ~ HexDigit ~ HexDigit) ~> (java.lang.Integer.parseInt(_, 16)) }
Expand All @@ -119,7 +118,7 @@ trait BasicRules { _: Parser =>
def HexDigit = rule { "0" - "9" | "a" - "f" | "A" - "F" }
def WhiteSpace: Rule0 = rule { zeroOrMore(anyOf(" \n\r\t\f")) }

def appendToSb(c: Char): Context[Any] => Unit = { ctx =>
def appendToSb(c: Char): Context[Any] Unit = { ctx
ctx.getValueStack.peek.asInstanceOf[StringBuilder].append(c)
()
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/scala/spray/json/lenses/Lens.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ trait Lens[M[_]] extends UpdateLens with ReadLens[M] {
* This implements most of the methods of `Lens`. Implementors of a new type of lens
* must implement `retr` for the read side of the lens and `updated` for the update side of the lens.
*/
abstract class LensImpl[M[_]](implicit val ops: Ops[M]) extends Lens[M] { outer =>
abstract class LensImpl[M[_]](implicit val ops: Ops[M]) extends Lens[M] { outer
import ExtraImplicits.richValue

def tryGet[T: Reader](p: JsValue): Validated[M[T]] =
Expand All @@ -37,14 +37,14 @@ abstract class LensImpl[M[_]](implicit val ops: Ops[M]) extends Lens[M] { outer
updated(op)(parent).getOrThrow
}

def is[U: Reader](f: U => Boolean): JsPred = value =>
tryGet[U](value) exists (x => ops.map(x)(f).forall(identity))
def is[U: Reader](f: U Boolean): JsPred = value
tryGet[U](value) exists (x ops.map(x)(f).forall(identity))

def /[M2[_], R[_]](next: Lens[M2])(implicit ev: Join[M2, M, R]): Lens[R] =
JsonLenses.combine(this, next)

def toSeq: Lens[Seq] = this / SeqLenses.asSeq

private[this] def mapValue[T](value: M[JsValue])(f: JsValue => Validated[T]): Validated[M[T]] =
private[this] def mapValue[T](value: M[JsValue])(f: JsValue Validated[T]): Validated[M[T]] =
ops.allRight(ops.map(value)(f))
}
12 changes: 6 additions & 6 deletions src/main/scala/spray/json/lenses/Operations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ package lenses
/**
* Defines a set of operations to update Json values.
*/
trait Operations extends OptionalFieldOperations { _: ExtraImplicits =>
trait Operations extends OptionalFieldOperations { _: ExtraImplicits
/**
* The set operation sets or creates a value.
*/
def set[T: JsonWriter](t: => T): Operation = new Operation {
def set[T: JsonWriter](t: T): Operation = new Operation {
def apply(value: SafeJsValue): SafeJsValue =
// ignore existence of old value
// ignore existence of old value
Right(t.toJson)
}

Expand All @@ -26,14 +26,14 @@ trait Operations extends OptionalFieldOperations { _: ExtraImplicits =>
/**
* The `modify` operation applies a function on the (converted) value
*/
def modify[T: Reader : JsonWriter](f: T => T): Operation = new MapOperation {
def modify[T: Reader: JsonWriter](f: T T): Operation = new MapOperation {
def apply(value: JsValue): SafeJsValue =
value.as[T] map (v => f(v).toJson)
value.as[T] map (v f(v).toJson)
}

def append(update: Update): Operation = ???
def update(update: Update): Operation = ???
def extract[M[_], T](value: Lens[M])(f: M[T] => Update): Operation = ???
def extract[M[_], T](value: Lens[M])(f: M[T] Update): Operation = ???
}

object Operations extends Operations with ExtraImplicits
36 changes: 18 additions & 18 deletions src/main/scala/spray/json/lenses/Ops.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package spray.json.lenses
* lenses.
*/
trait Ops[M[_]] {
def flatMap[T, U](els: M[T])(f: T => Seq[U]): Seq[U]
def flatMap[T, U](els: M[T])(f: T Seq[U]): Seq[U]

/**
* Checks if all the elements of the Seq are valid and then
Expand All @@ -24,57 +24,57 @@ trait Ops[M[_]] {
*/
def toSeq[T](v: Validated[M[T]]): Seq[Validated[T]]

def map[T, U](els: M[T])(f: T => U): Seq[U] =
flatMap(els)(v => Seq(f(v)))
def map[T, U](els: M[T])(f: T U): Seq[U] =
flatMap(els)(v Seq(f(v)))
}

object Ops {
implicit def idOps: Ops[Id] = new Ops[Id] {
def flatMap[T, U](els: T)(f: T => Seq[U]): Seq[U] = f(els)
def flatMap[T, U](els: T)(f: T Seq[U]): Seq[U] = f(els)

def allRight[T](v: Seq[Validated[T]]): Validated[T] = v.head

def toSeq[T](v: Validated[T]): Seq[Validated[T]] = Seq(v)
}

implicit def optionOps: Ops[Option] = new Ops[Option] {
def flatMap[T, U](els: Option[T])(f: T => Seq[U]): Seq[U] =
def flatMap[T, U](els: Option[T])(f: T Seq[U]): Seq[U] =
els.toSeq.flatMap(f)

def allRight[T](v: Seq[Validated[T]]): Validated[Option[T]] =
v match {
case Nil => Right(None)
case Seq(Right(x)) => Right(Some(x))
case Seq(Left(e)) => Left(e)
case Nil Right(None)
case Seq(Right(x)) Right(Some(x))
case Seq(Left(e)) Left(e)
}

def toSeq[T](v: Validated[Option[T]]): Seq[Validated[T]] = v match {
case Right(Some(x)) => Seq(Right(x))
case Right(None) => Nil
case Left(e) => Seq(Left(e))
case Right(Some(x)) Seq(Right(x))
case Right(None) Nil
case Left(e) Seq(Left(e))
}
}

implicit def seqOps: Ops[Seq] = new Ops[Seq] {
def flatMap[T, U](els: Seq[T])(f: T => Seq[U]): Seq[U] =
def flatMap[T, U](els: Seq[T])(f: T Seq[U]): Seq[U] =
els.flatMap(f)

def allRight[T](v: Seq[Validated[T]]): Validated[Seq[T]] = {
def inner(l: List[Validated[T]]): Validated[List[T]] = l match {
case head :: tail =>
case head :: tail
for {
headM <- head
tailM <- inner(tail)
headM head
tailM inner(tail)
} yield headM :: tailM
case Nil =>
case Nil
Right(Nil)
}
inner(v.toList)
}

def toSeq[T](x: Validated[Seq[T]]): Seq[Validated[T]] = x match {
case Right(x) => x.map(Right(_))
case Left(e) => List(Left(e))
case Right(x) x.map(Right(_))
case Left(e) List(Left(e))
}
}
}
Loading

0 comments on commit b18574d

Please sign in to comment.