Skip to content

Commit

Permalink
make ninny less ferrous (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
csubj authored Jun 15, 2023
1 parent 77701a4 commit d80e896
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 177 deletions.
2 changes: 1 addition & 1 deletion forProductN.sc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def generateProductToJson = {
out ++= (0 until i).map(j => s"a$j").mkString(", ")
out ++= ") = f(target)\n"
out ++= "obj("
out ++= (0 until i).map(j => s"(nameA$j, a$j)").mkString(", ")
out ++= (0 until i).map(j => s"(nameA$j ~> a$j)").mkString(", ")
out ++= ")\n}\n"
}

Expand Down
2 changes: 1 addition & 1 deletion json4s-compat/src/nrktkt/ninny/compat/Json4sCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object Json4sCompat {
def ~[B: ToJson](newPair: (String, B))(implicit
aToJson: ToJson[A]
): JsonObject =
obj(pair._1 -> pair._2, newPair._1 -> newPair._2)
obj(pair._1 ~> pair._2, newPair._1 ~> newPair._2)
}

implicit class JsonObjectSyntax(json: JsonObject) {
Expand Down
29 changes: 15 additions & 14 deletions json4s-compat/test/src/nrktkt/ninny/compat/Json4sCompatSpec.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nrktkt.ninny.compat

import nrktkt.ninny.ast._
import nrktkt.ninny._

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should
Expand Down Expand Up @@ -31,27 +32,27 @@ class Json4sCompatSpec
)

val ninny = nrktkt.ninny.obj(
"long" -> JsonDouble(1),
"bigInt" -> BigDecimal(1),
"exactNumber" -> BigDecimal("1.23"),
"bool" -> true,
"string" -> "value",
"null" -> JsonNull,
"array" -> Seq(JsonNull)
"long" ~> JsonDouble(1),
"bigInt" ~> BigDecimal(1),
"exactNumber" ~> BigDecimal("1.23"),
"bool" ~> true,
"string" ~> "value",
"null" ~> JsonNull,
"array" ~> Seq(JsonNull)
)

Json4sCompat.toNinnyJson(json4s).value shouldEqual ninny
}

"Conversion from ninny to json4s" should "convert all types" in {
val ninny = nrktkt.ninny.obj(
"long" -> JsonDouble(1),
"bigInt" -> BigDecimal(1),
"exactNumber" -> BigDecimal("1.23"),
"bool" -> true,
"string" -> "value",
"null" -> JsonNull,
"array" -> Seq(JsonNull)
"long" ~> JsonDouble(1),
"bigInt" ~> BigDecimal(1),
"exactNumber" ~> BigDecimal("1.23"),
"bool" ~> true,
"string" ~> "value",
"null" ~> JsonNull,
"array" ~> Seq(JsonNull)
)

val json4s = JObject(
Expand Down
21 changes: 10 additions & 11 deletions ninny/src/nrktkt/ninny/package.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package nrktkt

import nrktkt.ninny.ast._
import nrktkt.ninny.magnetic.{JsonMagnet, SomeJsonMagnet}

import nrktkt.ninny.magnetic.{SomeJsonMagnet}
import scala.language.dynamics
import scala.util.Try

Expand Down Expand Up @@ -65,23 +64,23 @@ package object ninny {
}

implicit class ArrowSyntax(val s: String) extends AnyVal {
def -->[A: ToJson](a: A) = s -> JsonMagnet(a)
def -->[A: ToJson](a: A): (String, Option[JsonValue]) = s -> a.toJson
// I couldn't decide on syntax, so I'll throw both out there
def ~>[A: ToJson](a: A) = s -> JsonMagnet(a)
def ~>[A: ToJson](a: A): (String, Option[JsonValue]) = s -> a.toJson
}

type ToJson[A] = ToJsonValue[A, JsonValue]
type ToJsonObject[A] = ToJsonValue[A, JsonObject]
type ToSomeJson[A] = ToSomeJsonValue[A, JsonValue]
type ToSomeJsonObject[A] = ToSomeJsonValue[A, JsonObject]

// @deprecated(
// message =
// "Use nrktkt.ninny.magnetic.obj instead, this may be replaced with a magnet-free signature in the future",
// since = ""
// )
def obj(nameValues: (String, JsonMagnet)*): JsonObject =
magnetic.obj(nameValues: _*)
// Import nrktkt.ninny.magnetic.obj if the magnet pattern is preferrable
def obj(fields: (String, Option[JsonValue])*): JsonObject =
JsonObject(
fields.collect { case (name, Some(json)) =>
name -> json
}.toMap
)

// @deprecated(
// message =
Expand Down
132 changes: 66 additions & 66 deletions ninny/test/src-2/nrktkt/ninny/JsonSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import java.util.UUID
import scala.util.Random
import scala.collection.compat.immutable.ArraySeq
import java.util.Base64
import nrktkt.ninny.magnetic.JsonMagnet
import nrktkt.ninny.magnetic.SomeJsonMagnet
import nrktkt.ninny.ToAndFromJson
import scala.util.Failure
import nrktkt.ninny.magnetic.JsonMagnet
import nrktkt.ninny.magnetic.SomeJsonMagnet

class JsonSpec
extends AnyFlatSpec
Expand All @@ -27,13 +27,13 @@ class JsonSpec

it should "work" in {
val sampleValues = obj(
"string" -> """¯\_(ツ)_/¯""",
"number" -> 1.79e308,
"bool" -> true,
"false" -> false,
"null" -> JsonNull,
"unit" -> ((): Unit),
"some" -> "str"
"string" ~> """¯\_(ツ)_/¯""",
"number" ~> 1.79e308,
"bool" ~> true,
"false" ~> false,
"null" ~> JsonNull,
"unit" ~> ((): Unit),
"some" ~> "str"
)

val sampleArray =
Expand All @@ -42,8 +42,8 @@ class JsonSpec
val sampleObject =
JsonObject(
sampleValues.values ++ obj(
"object" -> sampleValues,
"array" -> sampleArray
"object" ~> sampleValues,
"array" ~> sampleArray
).values
)

Expand Down Expand Up @@ -92,13 +92,13 @@ class JsonSpec
}

val sampleValuesAst = obj(
"string" -> """¯\_(ツ)_/¯""",
"number" -> 1.79e308,
"bool" -> true,
"false" -> false,
"null" -> JsonNull,
"unit" -> ((): Unit),
"some" -> Some("str")
"string" ~> """¯\_(ツ)_/¯""",
"number" ~> 1.79e308,
"bool" ~> true,
"false" ~> false,
"null" ~> JsonNull,
"unit" ~> ((): Unit),
"some" ~> Some("str")
)

val sampleValuesObj = SampleValues(
Expand Down Expand Up @@ -182,40 +182,40 @@ class JsonSpec
|""".stripMargin

val exampleObjectAst = obj(
"Image" -> obj(
"Width" -> 800,
"Height" -> 600,
"Title" -> "View from 15th Floor",
"Thumbnail" -> obj(
"Url" -> "http://www.example.com/image/481989943",
"Height" -> 125,
"Width" -> 100
"Image" ~> obj(
"Width" ~> 800,
"Height" ~> 600,
"Title" ~> "View from 15th Floor",
"Thumbnail" ~> obj(
"Url" ~> "http://www.example.com/image/481989943",
"Height" ~> 125,
"Width" ~> 100
),
"Animated" -> false,
"IDs" -> arr(116, 943, 234, 38793)
"Animated" ~> false,
"IDs" ~> arr(116, 943, 234, 38793)
)
)

val exampleArrayAst = arr(
obj(
"precision" -> "zip",
"Latitude" -> 37.7668,
"Longitude" -> -122.3959,
"Address" -> "",
"City" -> "SAN FRANCISCO",
"State" -> "CA",
"Zip" -> "94107",
"Country" -> "US"
"precision" ~> "zip",
"Latitude" ~> 37.7668,
"Longitude" ~> -122.3959,
"Address" ~> "",
"City" ~> "SAN FRANCISCO",
"State" ~> "CA",
"Zip" ~> "94107",
"Country" ~> "US"
),
obj(
"precision" -> "zip",
"Latitude" -> 37.371991,
"Longitude" -> -122.026020,
"Address" -> "",
"City" -> "SUNNYVALE",
"State" -> "CA",
"Zip" -> "94085",
"Country" -> "US"
"precision" ~> "zip",
"Latitude" ~> 37.371991,
"Longitude" ~> -122.026020,
"Address" ~> "",
"City" ~> "SUNNYVALE",
"State" ~> "CA",
"Zip" ~> "94085",
"Country" ~> "US"
)
)

Expand Down Expand Up @@ -244,13 +244,13 @@ class JsonSpec

implicit val toJson: ToSomeJson[Image] = a =>
obj(
"Width" -> a.Width,
"Height" -> a.Height,
"Title" -> a.Title,
"Thumbnail" -> a.Thumbnail,
"Url" -> a.Url,
"Animated" -> a.Animated,
"IDs" -> (if (a.IDs.isEmpty) None else a.IDs)
"Width" ~> a.Width,
"Height" ~> a.Height,
"Title" ~> a.Title,
"Thumbnail" ~> a.Thumbnail,
"Url" ~> a.Url,
"Animated" ~> a.Animated,
"IDs" ~> (if (a.IDs.isEmpty) None else Some(a.IDs))
)
}

Expand Down Expand Up @@ -280,14 +280,14 @@ class JsonSpec

implicit val toJson: ToSomeJson[Address] = a =>
obj(
"precision" -> a.precision,
"Latitude" -> a.Latitude,
"Longitude" -> a.Longitude,
"Address" -> a.Address,
"City" -> a.City,
"State" -> a.State,
"Zip" -> a.Zip,
"Country" -> a.Country
"precision" ~> a.precision,
"Latitude" ~> a.Latitude,
"Longitude" ~> a.Longitude,
"Address" ~> a.Address,
"City" ~> a.City,
"State" ~> a.State,
"Zip" ~> a.Zip,
"Country" ~> a.Country
)
}

Expand Down Expand Up @@ -461,7 +461,7 @@ class JsonSpec
}

"JsonObjects" should "rename fields" in {
val json = obj("foo" -> "bar")
val json = obj("foo" ~> "bar")
val renamed = json.renameField("foo", "baz")
(renamed / "baz").value shouldEqual JsonString("bar")
(renamed / "foo") shouldEqual None
Expand Down Expand Up @@ -566,7 +566,7 @@ class JsonSpec

"magnet free syntax" should "work" in {
val o = obj(
"foo" -> 5,
"foo" ~> 5,
"bar" --> 7,
"baz" ~> 11
)
Expand Down Expand Up @@ -640,7 +640,7 @@ class JsonSpec
val example = Example("bar", 1)
val js = example.toSomeJson
val fromJs = js.to[Example].success.value
js shouldEqual obj("baz" -> "bar", "bop" -> 1)
js shouldEqual obj("baz" ~> "bar", "bop" ~> 1)
fromJs shouldEqual example
}
{
Expand All @@ -652,8 +652,8 @@ class JsonSpec
ToAndFromJson.auto[Example]
}

obj("qix" -> 1).to[Example].success.value shouldEqual Example(1)
obj("qix" -> 1, "foo" -> "baz")
obj("qix" ~> 1).to[Example].success.value shouldEqual Example(1)
obj("qix" ~> 1, "foo" ~> "baz")
.to[Example]
.success
.value shouldEqual Example(1, "baz")
Expand All @@ -662,8 +662,8 @@ class JsonSpec
it should "ignore defaults if the default import isn't available" in {
import Auto._

obj("qix" -> 1).to[Example] shouldBe a[Failure[_]]
obj("qix" -> 1, "foo" -> "baz")
obj("qix" ~> 1).to[Example] shouldBe a[Failure[_]]
obj("qix" ~> 1, "foo" ~> "baz")
.to[Example]
.success
.value shouldEqual Example(1, "baz")
Expand Down
2 changes: 1 addition & 1 deletion ninny/test/src-2/nrktkt/ninny/example/Example.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ object Example extends App {
println()

val profileUpdateJson =
obj("email" -> JsonNull, "bio" -> "Just a zombie looking for his Jane")
obj("email" ~> JsonNull, "bio" ~> "Just a zombie looking for his Jane")

val profileUpdate = profileUpdateJson.to[UpdateProfile].get
println("Parsed profile update")
Expand Down
Loading

0 comments on commit d80e896

Please sign in to comment.