Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final Scala 2.13 Pieces: documentation-helpers, json-scalaz7 #1988

Merged
merged 8 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,9 @@ core/documentation-helpers/src/test/**Test.scala

# JavaScript testing
node_modules

# Metals
.metals

# VSCode
.vscode
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ lazy val documentationHelpers =
coreProject("documentation-helpers")
.settings(description := "Documentation Helpers")
.dependsOn(util)
.settings(crossScalaVersions := crossUpTo213)

lazy val json_scalaz7 =
coreProject("json-scalaz7")
Expand All @@ -104,6 +105,7 @@ lazy val json_scalaz7 =
description := "JSON Library based on Scalaz 7",
libraryDependencies ++= Seq(scalaz7)
)
.settings(crossScalaVersions := crossUpTo213)

lazy val json_ext =
coreProject("json-ext")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2009-2010 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.liftweb.json.scalaz

import scalaz.ValidationNel
import scalaz.Validation._
import scalaz.std.option._
import scalaz.std.list._
import scalaz.syntax.traverse._
import net.liftweb.json._
import scala.collection.breakOut

trait Base { this: Types =>
implicit def boolJSON: JSON[Boolean] = new JSON[Boolean] {
def read(json: JValue) = json match {
case JBool(b) => success(b)
case x => failure(UnexpectedJSONError(x, classOf[JBool])).toValidationNel
}

def write(value: Boolean) = JBool(value)
}

implicit def intJSON: JSON[Int] = new JSON[Int] {
def read(json: JValue) = json match {
case JInt(x) => success(x.intValue)
case x => failure(UnexpectedJSONError(x, classOf[JInt])).toValidationNel
}

def write(value: Int) = JInt(BigInt(value))
}

implicit def longJSON: JSON[Long] = new JSON[Long] {
def read(json: JValue) = json match {
case JInt(x) => success(x.longValue)
case x => failure(UnexpectedJSONError(x, classOf[JInt])).toValidationNel
}

def write(value: Long) = JInt(BigInt(value))
}

implicit def doubleJSON: JSON[Double] = new JSON[Double] {
def read(json: JValue) = json match {
case JDouble(x) => success(x)
case x => failure(UnexpectedJSONError(x, classOf[JDouble])).toValidationNel
}

def write(value: Double) = JDouble(value)
}

implicit def stringJSON: JSON[String] = new JSON[String] {
def read(json: JValue) = json match {
case JString(x) => success(x)
case x => failure(UnexpectedJSONError(x, classOf[JString])).toValidationNel
}

def write(value: String) = JString(value)
}

implicit def bigintJSON: JSON[BigInt] = new JSON[BigInt] {
def read(json: JValue) = json match {
case JInt(x) => success(x)
case x => failure(UnexpectedJSONError(x, classOf[JInt])).toValidationNel
}

def write(value: BigInt) = JInt(value)
}

implicit def jvalueJSON: JSON[JValue] = new JSON[JValue] {
def read(json: JValue) = success(json)
def write(value: JValue) = value
}

implicit def listJSONR[A: JSONR]: JSONR[List[A]] = new JSONR[List[A]] {
def read(json: JValue) = json match {
case JArray(xs) => {
xs.map(fromJSON[A]).sequence[({type λ[α]=ValidationNel[Error, α]})#λ, A]
}
case x => failure(UnexpectedJSONError(x, classOf[JArray])).toValidationNel
}
}
implicit def listJSONW[A: JSONW]: JSONW[List[A]] = new JSONW[List[A]] {
def write(values: List[A]) = JArray(values.map(x => toJSON(x)))
}

implicit def optionJSONR[A: JSONR]: JSONR[Option[A]] = new JSONR[Option[A]] {
def read(json: JValue) = json match {
case JNothing | JNull => success(None)
case x => fromJSON[A](x).map(some)
}
}
implicit def optionJSONW[A: JSONW]: JSONW[Option[A]] = new JSONW[Option[A]] {
def write(value: Option[A]) = value.map(x => toJSON(x)).getOrElse(JNothing)
}

implicit def mapJSONR[A: JSONR]: JSONR[Map[String, A]] = new JSONR[Map[String, A]] {
def read(json: JValue) = json match {
case JObject(fs) =>
val r = fs.map(f => fromJSON[A](f.value).map(v => (f.name, v))).sequence[({type λ[α]=ValidationNel[Error, α]})#λ, (String, A)]
r.map(_.toMap)
case x => failure(UnexpectedJSONError(x, classOf[JObject])).toValidationNel
}
}
implicit def mapJSONW[A: JSONW]: JSONW[Map[String, A]] = new JSONW[Map[String, A]] {
def write(values: Map[String, A]) = JObject(values.map { case (k, v) => JField(k, toJSON(v)) }(breakOut): _*)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2009-2010 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.liftweb.json.scalaz

import scalaz.ValidationNel
import scalaz.Validation._
import scalaz.std.option._
import scalaz.std.list._
import scalaz.syntax.traverse._
import net.liftweb.json._

trait Base { this: Types =>
implicit def boolJSON: JSON[Boolean] = new JSON[Boolean] {
def read(json: JValue) = json match {
case JBool(b) => success(b)
case x => failure(UnexpectedJSONError(x, classOf[JBool])).toValidationNel
}

def write(value: Boolean) = JBool(value)
}

implicit def intJSON: JSON[Int] = new JSON[Int] {
def read(json: JValue) = json match {
case JInt(x) => success(x.intValue)
case x => failure(UnexpectedJSONError(x, classOf[JInt])).toValidationNel
}

def write(value: Int) = JInt(BigInt(value))
}

implicit def longJSON: JSON[Long] = new JSON[Long] {
def read(json: JValue) = json match {
case JInt(x) => success(x.longValue)
case x => failure(UnexpectedJSONError(x, classOf[JInt])).toValidationNel
}

def write(value: Long) = JInt(BigInt(value))
}

implicit def doubleJSON: JSON[Double] = new JSON[Double] {
def read(json: JValue) = json match {
case JDouble(x) => success(x)
case x => failure(UnexpectedJSONError(x, classOf[JDouble])).toValidationNel
}

def write(value: Double) = JDouble(value)
}

implicit def stringJSON: JSON[String] = new JSON[String] {
def read(json: JValue) = json match {
case JString(x) => success(x)
case x => failure(UnexpectedJSONError(x, classOf[JString])).toValidationNel
}

def write(value: String) = JString(value)
}

implicit def bigintJSON: JSON[BigInt] = new JSON[BigInt] {
def read(json: JValue) = json match {
case JInt(x) => success(x)
case x => failure(UnexpectedJSONError(x, classOf[JInt])).toValidationNel
}

def write(value: BigInt) = JInt(value)
}

implicit def jvalueJSON: JSON[JValue] = new JSON[JValue] {
def read(json: JValue) = success(json)
def write(value: JValue) = value
}

implicit def listJSONR[A: JSONR]: JSONR[List[A]] = new JSONR[List[A]] {
def read(json: JValue) = json match {
case JArray(xs) => {
xs.map(fromJSON[A]).sequence[({type λ[α]=ValidationNel[Error, α]})#λ, A]
}
case x => failure(UnexpectedJSONError(x, classOf[JArray])).toValidationNel
}
}
implicit def listJSONW[A: JSONW]: JSONW[List[A]] = new JSONW[List[A]] {
def write(values: List[A]) = JArray(values.map(x => toJSON(x)))
}

implicit def optionJSONR[A: JSONR]: JSONR[Option[A]] = new JSONR[Option[A]] {
def read(json: JValue) = json match {
case JNothing | JNull => success(None)
case x => fromJSON[A](x).map(some)
}
}
implicit def optionJSONW[A: JSONW]: JSONW[Option[A]] = new JSONW[Option[A]] {
def write(value: Option[A]) = value.map(x => toJSON(x)).getOrElse(JNothing)
}

implicit def mapJSONR[A: JSONR]: JSONR[Map[String, A]] = new JSONR[Map[String, A]] {
def read(json: JValue) = json match {
case JObject(fs) =>
val r = fs.map(f => fromJSON[A](f.value).map(v => (f.name, v))).sequence[({type λ[α]=ValidationNel[Error, α]})#λ, (String, A)]
r.map(_.toMap)
case x => failure(UnexpectedJSONError(x, classOf[JObject])).toValidationNel
}
}
implicit def mapJSONW[A: JSONW]: JSONW[Map[String, A]] = new JSONW[Map[String, A]] {
def write(values: Map[String, A]) = {
JObject(
values.map {
case (k, v) => JField(k, toJSON(v))
}.to(List): _*
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ trait Types {
case class UncategorizedError(key: String, desc: String, args: List[Any]) extends Error

case object Fail {
def apply[A](key: String, desc: String, args: List[Any]): Result[A] =
def apply[A](key: String, desc: String, args: List[Any]): Result[A] =
failure(UncategorizedError(key, desc, args)).toValidationNel

def apply[A](key: String, desc: String): Result[A] =
def apply[A](key: String, desc: String): Result[A] =
failure(UncategorizedError(key, desc, Nil)).toValidationNel
}

Expand Down Expand Up @@ -70,7 +70,7 @@ trait Types {
def toJSON[A: JSONW](value: A): JValue = implicitly[JSONW[A]].write(value)

def field[A: JSONR](name: String)(json: JValue): Result[A] = json match {
case JObject(fs) =>
case JObject(fs) =>
fs.find(_.name == name)
.map(f => implicitly[JSONR[A]].read(f.value))
.orElse(implicitly[JSONR[A]].read(JNothing).fold(_ => none, x => some(success(x))))
Expand All @@ -80,7 +80,7 @@ trait Types {

def validate[A: JSONR](name: String): Kleisli[Result, JValue, A] = Kleisli(field[A](name))

def makeObj(fields: Traversable[(String, JValue)]): JObject =
def makeObj(fields: Traversable[(String, JValue)]): JObject =
JObject(fields.toList.map { case (n, v) => JField(n, v) })
}

Expand Down
2 changes: 1 addition & 1 deletion travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if [ "${TRAVIS_PULL_REQUEST}" = "false" ] && [ -z "$DISABLE_PUBLISH" ]; then

if [[ "$TRAVIS_SCALA_VERSION" =~ ^2.13 ]]; then
# we only have certain modules available for publishing in 2.13
sbt ++$TRAVIS_SCALA_VERSION lift-webkit/publish lift-json/publish lift-actor/publish lift-json-ext/publish lift-record/publish lift-proto/publish lift-mapper/publish lift-common/publish lift-db/publish lift-markdown/publish lift-util/publish lift-testkit/publish lift-mongodb/publish lift-mongodb-record/publish
sbt ++$TRAVIS_SCALA_VERSION lift-webkit/publish lift-json/publish lift-actor/publish lift-json-ext/publish lift-record/publish lift-proto/publish lift-mapper/publish lift-common/publish lift-db/publish lift-markdown/publish lift-util/publish lift-testkit/publish lift-mongodb/publish lift-mongodb-record/publish lift-json-scalaz7/publish
else
sbt ++$TRAVIS_SCALA_VERSION publish
fi
Expand Down