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

uPickle integration #54

Merged
merged 3 commits into from
Dec 21, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ jobs:

- name: Make target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v'))
run: mkdir -p modules/core/.native/target modules/core/.js/target modules/circe/.js/target modules/circe/.jvm/target modules/circe/.native/target modules/core/.jvm/target project/target
run: mkdir -p modules/upickle/.js/target modules/core/.native/target modules/core/.js/target modules/circe/.js/target modules/upickle/.jvm/target modules/circe/.jvm/target modules/circe/.native/target modules/core/.jvm/target modules/upickle/.native/target project/target

- name: Compress target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v'))
run: tar cf targets.tar modules/core/.native/target modules/core/.js/target modules/circe/.js/target modules/circe/.jvm/target modules/circe/.native/target modules/core/.jvm/target project/target
run: tar cf targets.tar modules/upickle/.js/target modules/core/.native/target modules/core/.js/target modules/circe/.js/target modules/upickle/.jvm/target modules/circe/.jvm/target modules/circe/.native/target modules/core/.jvm/target modules/upickle/.native/target project/target

- name: Upload target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v'))
Expand Down
15 changes: 14 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ lazy val circe = crossProject(JVMPlatform, JSPlatform, NativePlatform)
),
)

lazy val upickle = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.withoutSuffixFor(JVMPlatform)
.dependsOn(core % "compile->compile;test->test")
.asModule
.settings(commonSettings)
.settings(
description := "uPickle / uJson integration for record4s",
libraryDependencies ++= Seq(
"com.lihaoyi" %%% "upickle" % "3.1.3",
),
)

lazy val benchmark_3 = (project in file("modules/benchmark_3"))
.dependsOn(core.jvm)
.settings(commonSettings)
Expand Down Expand Up @@ -171,7 +184,7 @@ ThisBuild / githubWorkflowAddedJobs ++= Seq(
ThisBuild / tlSitePublishBranch := None // update on tag releases
lazy val docs = project
.in(file("site"))
.dependsOn(core.jvm, circe.jvm)
.dependsOn(core.jvm, circe.jvm, upickle.jvm)
.enablePlugins(TypelevelSitePlugin)
.settings(
scalacOptions --= Seq(
Expand Down
38 changes: 38 additions & 0 deletions docs/integrations/upickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Integration with uPickle
========================

Records can be converted to/from JSON by using [uPickle][].

[uPickle]: https://com-lihaoyi.github.io/upickle/

Installation
------------

You need an additional module to use records integrated with [uPickle][].

```scala
libraryDependencies ++= Seq(
"com.github.tarao" %% "record4s-upickle" % "@VERSION@"
),
```

From / To JSON
--------------

Importing `Record.readWriter` enables [uPickle][] to decode/encode records in the ordinary way.

```scala mdoc:mline
import com.github.tarao.record4s.%
import com.github.tarao.record4s.upickle.Record.readWriter
import upickle.default.{read, write}

type Person = % { val name: String; val age: Int }

val json = """{"name":"tarao","age":3}"""

// Read from JSON
val r = read[Person](json)

// Write it back
write(r)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2023 record4s authors
*
* 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 com.github.tarao
package record4s
package upickle

import _root_.upickle.default.{ReadWriter, readwriter}
import upickle.Record.{readDict, writeDict}

object ArrayRecord {
inline given readWriter[T <: Tuple](using
r: RecordLike[record4s.ArrayRecord[T]],
): ReadWriter[record4s.ArrayRecord[T]] = {
type Types = r.ElemTypes
type Labels = r.ElemLabels

readwriter[ujson.Value].bimap[ArrayRecord[T]](
record => ujson.Obj(writeDict[Types, Labels](r.iterableOf(record).toMap)),
json => {
val dict = json.obj
val iterable = readDict[Types, Labels, Vector[(String, Any)]](
dict,
Vector.newBuilder[(String, Any)],
).result()
record4s.ArrayRecord.newArrayRecord[ArrayRecord[T]](iterable)
},
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2023 record4s authors
*
* 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 com.github.tarao
package record4s
package upickle

import _root_.upickle.core.LinkedHashMap
import _root_.upickle.default.{
ReadWriter,
Reader,
Writer,
read,
readwriter,
writeJs,
}

import scala.collection.mutable.Builder
import scala.compiletime.{constValue, erasedValue, summonInline}
import scala.util.NotGiven

object Record {
private[upickle] inline def writeDict[Types, Labels](
record: Map[String, Any],
res: LinkedHashMap[String, ujson.Value] = LinkedHashMap(),
): LinkedHashMap[String, ujson.Value] =
inline (erasedValue[Types], erasedValue[Labels]) match {
case _: (EmptyTuple, EmptyTuple) =>
res

case _: (tpe *: types, label *: labels) =>
val labelStr = constValue[label & String]
val value =
inline erasedValue[tpe] match {
case _: ujson.Value =>
record(labelStr).asInstanceOf[ujson.Value]
case _ =>
val writer = summonInline[Writer[tpe]]
val elem = record(labelStr).asInstanceOf[tpe]
writeJs[tpe](elem)(using writer)
}
writeDict[types, labels](record, res += (labelStr -> value))
}

private[upickle] inline def readDict[Types, Labels, C](
dict: LinkedHashMap[String, ujson.Value],
res: Builder[(String, Any), C],
): Builder[(String, Any), C] =
inline (erasedValue[Types], erasedValue[Labels]) match {
case _: (EmptyTuple, EmptyTuple) =>
res

case _: (tpe *: types, label *: labels) =>
val labelStr = constValue[label & String]
val jsonElem = dict.getOrElse(labelStr, ujson.Null)
val reader = summonInline[Reader[tpe]]
val elem = read[tpe](jsonElem)(using reader)
res += (labelStr -> elem)
readDict[types, labels, C](dict, res)
}

inline given readWriter[R <: %](using
r: RecordLike[R],
nonTuple: NotGiven[R <:< Tuple],
): ReadWriter[R] = {
type Types = r.ElemTypes
type Labels = r.ElemLabels

readwriter[ujson.Value].bimap[R](
record => ujson.Obj(writeDict[Types, Labels](r.iterableOf(record).toMap)),
json => {
val dict = json.obj
val iterable = readDict[Types, Labels, Map[String, Any]](
dict,
Map.newBuilder[String, Any],
).result()
record4s.Record.newMapRecord[R](iterable)
},
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2023 record4s authors
*
* 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 com.github.tarao
package record4s
package upickle

class ArrayRecordSpec extends helper.UnitSpec {
describe("ArrayRecord.readWriter") {
import upickle.ArrayRecord.readWriter

describe("write") {
import _root_.upickle.default.{write, writeJs}

it("should write a record") {
val r = record4s.ArrayRecord(name = "tarao", age = 3)
write(r) shouldBe """{"name":"tarao","age":3}"""
writeJs(r) shouldBe ujson.Obj(
"name" -> ujson.Str("tarao"),
"age" -> ujson.Num(3),
)
}

it("should write a nested record") {
val r = record4s.ArrayRecord(
name = "tarao",
age = 3,
email = record4s.ArrayRecord(user = "tarao", domain = "example.com"),
)
write(
r,
) shouldBe """{"name":"tarao","age":3,"email":{"user":"tarao","domain":"example.com"}}"""
writeJs(r) shouldBe ujson.Obj(
"name" -> ujson.Str("tarao"),
"age" -> ujson.Num(3),
"email" -> ujson.Obj(
"user" -> ujson.Str("tarao"),
"domain" -> ujson.Str("example.com"),
),
)
}
}

describe("read") {
import _root_.upickle.default.read

it("should read a record") {
val json = """{"name":"tarao","age":3}"""
val r =
read[record4s.ArrayRecord[(("name", String), ("age", Int))]](json)
r shouldStaticallyBe a[
record4s.ArrayRecord[(("name", String), ("age", Int))],
]
r.name shouldBe "tarao"
r.age shouldBe 3
}

it("should read a nested record") {
val json =
"""{"name":"tarao","age":3,"email":{"user":"tarao","domain":"example.com"}}"""
val r = read[record4s.ArrayRecord[
(
("name", String),
("age", Int),
(
"email",
record4s.ArrayRecord[(("user", String), ("domain", String))],
),
),
]](json)
r shouldStaticallyBe a[record4s.ArrayRecord[
(
("name", String),
("age", Int),
(
"email",
record4s.ArrayRecord[(("user", String), ("domain", String))],
),
),
]]
r.name shouldBe "tarao"
r.age shouldBe 3
r.email.user shouldBe "tarao"
r.email.domain shouldBe "example.com"
}
}
}
}
Loading