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

Bring back the WithRaw trait for libraries that need the original object of a read value #122

Merged
merged 1 commit into from
Mar 25, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Details
+ The `@react` macro now produces nicer APIs for external components that have default values for all props parameters. [PR #119](https://github.com/shadaj/slinky/pull/119)
+ Add more variations for `ExternalComponent` that support providing a statically-typed interface for the component instance: `ExternalComponentWithRefType`, `ExternalComponentWithAttributesWithRefType`, `ExternalComponentNoPropsWithRefType`, `ExternalComponentNoPropsWithAttributesWithRefType` [PR #119](https://github.com/shadaj/slinky/pull/119)
+ Bring back the `WithRaw` trait, which makes it possible to access the original object of a read value [PR #122](https://github.com/shadaj/slinky/pull/122)
+ Fix exceptions when declaring custom tags and attributes in a component class [PR #118](https://github.com/shadaj/slinky/pull/118)
+ Fix exceptions when reading the null-prototype in Node.js [PR #121](https://github.com/shadaj/slinky/pull/121)

Expand Down
9 changes: 8 additions & 1 deletion readWrite/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ sourceGenerators in Compile += Def.task {

out.println(
s"""package slinky.readwrite
|
|import scala.collection.generic.CanBuildFrom
|import scala.concurrent.Future
|import scala.language.experimental.macros
Expand All @@ -174,11 +175,17 @@ sourceGenerators in Compile += Def.task {
|
|trait Reader[P] {
| def read(o: js.Object): P = {
| if (js.typeOf(o) == "object" && o != null && !js.isUndefined(o.asInstanceOf[js.Dynamic].__)) {
| val ret = if (js.typeOf(o) == "object" && o != null && !js.isUndefined(o.asInstanceOf[js.Dynamic].__)) {
| o.asInstanceOf[js.Dynamic].__.asInstanceOf[P]
| } else {
| forceRead(o)
| }
|
| if (ret.isInstanceOf[WithRaw]) {
| ret.asInstanceOf[js.Dynamic].__slinky_raw = o
| }
|
| ret
| }
|
| protected def forceRead(o: js.Object): P
Expand Down
7 changes: 7 additions & 0 deletions readWrite/src/main/scala/slinky/readwrite/WithRaw.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package slinky.readwrite

import scala.scalajs.js

trait WithRaw {
def raw: js.Object = this.asInstanceOf[js.Dynamic].__slinky_raw.asInstanceOf[js.Object]
}
9 changes: 8 additions & 1 deletion tests/src/test/scala/slinky/core/ReaderWriterTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package slinky.core

import slinky.readwrite.{Reader, Writer}
import slinky.readwrite.{Reader, WithRaw, Writer}
import org.scalatest.FunSuite

import scala.scalajs.js
Expand Down Expand Up @@ -92,6 +92,13 @@ class ReaderWriterTest extends FunSuite {
assert(!written.asInstanceOf[js.Object].hasOwnProperty("boolean"))
}

test("Read/write - case class with raw") {
case class CaseClassWithRaw(int: Int, boolean: Boolean) extends WithRaw
val inObj = js.Dynamic.literal(int = 1, boolean = true)
val read = implicitly[Reader[CaseClassWithRaw]].read(inObj)
assert(read == CaseClassWithRaw(1, true) && read.raw == inObj)
}

test("Read/write - sealed trait with case objects") {
sealed trait MySealedTrait
case class SubTypeA(int: Int) extends MySealedTrait
Expand Down