Skip to content

Commit

Permalink
Merge pull request #26 from disneystreaming/add-js-converters
Browse files Browse the repository at this point in the history
add js.Any => Document converter
  • Loading branch information
Jakub Kozłowski authored Jan 5, 2022
2 parents 622f1aa + 451e053 commit dad7bf3
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
53 changes: 53 additions & 0 deletions modules/core/src-js/smithy4s/JsConverters.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2021 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* 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 smithy4s

import scala.scalajs.js
import scala.util.control.NonFatal
import smithy4s._

object JsConverters {

private[this] def convertAnyToDocumentUnsafe(input: Any): Document =
input match {
case s: String => Document.fromString(s)
case n: Double => Document.fromDouble(n)
case true => Document.fromBoolean(true)
case false => Document.fromBoolean(false)
case null => Document.DNull
case a: js.Array[_] =>
Document.DArray(a.map(convertAnyToDocumentUnsafe(_: Any)).toVector)
case o: js.Object =>
Document.DObject(
o.asInstanceOf[js.Dictionary[_]]
.view
.mapValues(convertAnyToDocumentUnsafe)
.toMap
)
case other if js.isUndefined(other) => Document.DNull
}

/**
* Convert [[scala.scalajs.js.Any]] to [[Document]].
*/
def convertJsToDocument(input: js.Any): Either[Throwable, Document] =
try Right(convertAnyToDocumentUnsafe(input))
catch {
case NonFatal(exception) => Left(exception)
}

}
62 changes: 62 additions & 0 deletions modules/core/test/src-js/smithy4s/JsConvertersSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package smithy4s

import weaver._
import scala.scalajs.js

object JsConvertersSpec extends FunSuite {

test("object - strings") {
val input = js.Dictionary("one" -> "1")
val result = JsConverters.convertJsToDocument(input)
val expected =
Right(Document.DObject(Map("one" -> Document.fromString("1"))))
expect(result == expected)
}

test("array - booleans") {
val input = js.Array(true, false)
val result = JsConverters.convertJsToDocument(input)
val expected =
Right(Document.DArray(Vector(true, false).map(Document.fromBoolean)))
expect(result == expected)
}

test("integer") {
val input = 1
val result = JsConverters.convertJsToDocument(input)
val expected = Right(Document.fromInt(1))
expect(result == expected)
}

test("null") {
val input = null
val result = JsConverters.convertJsToDocument(input)
val expected =
Right(Document.DNull)
expect(result == expected)
}

test("undefined") {
val input = js.undefined
val result = JsConverters.convertJsToDocument(input)
val expected =
Right(Document.DNull)
expect(result == expected)
}

test("double") {
val input = 1.1
val result = JsConverters.convertJsToDocument(input)
val expected =
Right(Document.fromDouble(1.1))
expect(result == expected)
}

test("float") {
val input = 1.1f
val result = JsConverters.convertJsToDocument(input)
val expected = Right(Document.fromDouble(input.toDouble))
expect(result == expected)
}

}

0 comments on commit dad7bf3

Please sign in to comment.