Permalink
Browse files

Remove `$obj` from precise json.

We no longer support keys that coincidentally share a name with a
precise key.
  • Loading branch information...
alissapajer committed Sep 12, 2018
1 parent aab5307 commit 8b97cf4fe393f00b5c614d19c335dff7b0bf02e3
View
@@ -626,19 +626,16 @@ Type | Readable | Precise | Notes
null | `null` | *same* |
boolean | `true`, `false` | *same* |
string | `"abc"` | *same* |
int | `1` | *same* |
decimal | `2.1` | *same* |
object | `{ "a": 1 }` | *same* |
object | `{ "$foo": 2 }` | `{ "$obj": { "$foo": 2 } }` | Requires a type-specifier if any key starts with `$`.
number | `1`, `2.1` | *same* |
object | `{ "a": 1 }` | *same* | Keys that coincidentally equal a precise temporal key (e.g. "$localtime") are not supported.
array | `[1, 2, 3]` | *same* |
localdatetime | `"2015-01-31T10:30:00"` | `{ "$localdatetime": "2015-01-31T10:30" }` |
localdate | `"2015-01-31"` | `{ "$localdate": "2015-01-31" }` |
localtime | `"10:30:00.000"` | `{ "$localtime": "10:30" }` |
offsetdatetime | `"2015-01-31T10:30:00Z"` | `{ "$offsetdatetime": "2015-01-31T10:30Z" }` |
offsetdate | `"2015-01-31Z"` | `{ "$offsetdate": "2015-01-31Z" }` |
offsettime | `"10:30:00.000Z"` | `{ "$offsettime": "10:30Z" }` |
interval | `"PT12H34M"` | `{ "$interval": "P7DT12H34M" }` |
binary | `"TE1OTw=="` | `{ "$binary": "TE1OTw==" }` | BASE64-encoded.
localdatetime | `"2015-01-31T10:30:00"` | `{ "$localdatetime": "2015-01-31T10:30" }` |
localdate | `"2015-01-31"` | `{ "$localdate": "2015-01-31" }` |
localtime | `"10:30:00.000"` | `{ "$localtime": "10:30" }` |
offsetdatetime | `"2015-01-31T10:30:00Z"` | `{ "$offsetdatetime": "2015-01-31T10:30Z" }` |
offsetdate | `"2015-01-31Z"` | `{ "$offsetdate": "2015-01-31Z" }` |
offsettime | `"10:30:00.000Z"` | `{ "$offsettime": "10:30Z" }` |
interval | `"PT12H34M"` | `{ "$interval": "P7DT12H34M" }` |
### CSV
@@ -19,7 +19,6 @@ package quasar.frontend.data
import slamdata.Predef._
import quasar.common.data.Data
import quasar.fp._
import quasar.fp.ski._
import argonaut._, Argonaut._
import scalaz._, Scalaz._
@@ -73,7 +72,6 @@ object DataCodec {
val OffsetDateKey = "$offsetdate"
val OffsetTimeKey = "$offsettime"
val IntervalKey = "$interval"
val ObjKey = "$obj"
}
object VerboseDateTimeFormatters {
@@ -116,15 +114,15 @@ object DataCodec {
def encode(data: Data): Option[Json] = {
import Data._
import VerboseDateTimeFormatters._
data match {
case d@(Null | Bool(_) | Int(_) | Dec(_) | Str(_)) => Readable.encode(d)
// For Object, if we find one of the above keys, which means we serialized something particular
// to the precise encoding, wrap this object in another object with a single field with the name ObjKey
case Obj(value) =>
val obj = Json.obj(value.toList.map { case (k, v) => encode(v).map(k -> _) }.unite: _*)
value.keys.find(_.startsWith("$")).fold(obj)(κ(Json.obj(ObjKey -> obj))).some
Json.obj(value.toList.map { case (k, v) => encode(v).map(k -> _) }.unite: _*).some
case Arr(value) => Json.array(value.map(encode).unite: _*).some
case Arr(value) =>
Json.array(value.map(encode).unite: _*).some
case OffsetDateTime(value) =>
Json.obj(OffsetDateTimeKey -> jString(OffsetDateTimeFormatter.format(value))).some
@@ -173,8 +171,7 @@ object DataCodec {
case (`LocalTimeKey`, value) :: Nil => unpack(value.string, "string value for $localtime")(parseLocalTime(_).leftMap(err => ParseError(err.message)))
case (`LocalDateKey`, value) :: Nil => unpack(value.string, "string value for $localdate")(parseLocalDate(_).leftMap(err => ParseError(err.message)))
case (`IntervalKey`, value) :: Nil => unpack(value.string, "string value for $interval")(parseInterval(_).leftMap(err => ParseError(err.message)))
case (`ObjKey`, value) :: Nil => unpack(value.obj, "object value for $obj")(decodeObj)
case _ => obj.fields.find(_.startsWith("$")).fold(decodeObj(obj))(κ(-\/(UnescapedKeyError(json))))
case _ => decodeObj(obj)
}
})
}
@@ -98,17 +98,11 @@ class DataCodecSpecs extends quasar.Qspec {
"""{ "a": 1, "b": 2, "c": 3, "d": 4, "e": 5 }""")
}
"encode obj with leading '$'s" in {
"encode nonsense obj with leading '$'s (precise)" in {
DataCodec.render(Data.Obj(ListMap(
"$a" -> Data.Int(1),
LocalDateKey -> Data.LocalDateTime(LocalDateTime.parse("2015-01-31T10:30"))))) must beSome(
s"""{ "$ObjKey": { "$$a": 1, "$LocalDateKey": { "$LocalDateTimeKey": "2015-01-31T10:30:00.000000000" } } }""")
}
"encode obj with $obj" in {
DataCodec.render(Data.Obj(ListMap(ObjKey -> Data.Obj(ListMap(ObjKey -> Data.Int(1)))))) must
beSome(s"""{ "$ObjKey": { "$ObjKey": { "$ObjKey": { "$ObjKey": 1 } } } }""")
s"""{ "$$a": 1, "$LocalDateKey": { "$LocalDateTimeKey": "2015-01-31T10:30:00.000000000" } }""")
}
"encode array" in {
@@ -190,14 +184,13 @@ class DataCodecSpecs extends quasar.Qspec {
results must beSome(expected)
}
// Some invalid inputs:
"fail with unescaped leading '$'" in {
"succeed with unescaped leading '$'" in {
DataCodec.parse("""{ "$a": 1 }""") must
be_-\/(UnescapedKeyError(jSingleObject("$a", jNumber(1))))
be_\/-(Data.Obj(ListMap(("$a", Data.Int(1)))))
}
// Some invalid inputs:
"fail with invalid offset date time value" in {
DataCodec.parse(s"""{ "$OffsetDateTimeKey": 123456 }""") must be_-\/
}
@@ -269,11 +262,11 @@ class DataCodecSpecs extends quasar.Qspec {
beSome("""{ "a": 1, "b": 2, "c": 3, "d": 4, "e": 5 }""")
}
"encode obj with leading '$'s" in {
"encode nonsense obj with precise key paired with non-precise key (readable)" in {
DataCodec.render(Data.Obj(ListMap(
"$a" -> Data.Int(1),
LocalDateTimeKey -> Data.LocalDateTime(LocalDateTime.parse("2015-01-31T10:30"))))) must
beSome(s"""{ "$$a": 1, "$LocalDateTimeKey": "2015-01-31T10:30" }""")
LocalDateKey -> Data.LocalDateTime(LocalDateTime.parse("2015-01-31T10:30"))))) must
beSome(s"""{ "$$a": 1, "$LocalDateKey": "2015-01-31T10:30" }""")
}
"encode array" in {
@@ -1766,9 +1766,6 @@ abstract class Slice { source =>
import DataCodec.PreciseKeys._
def inner(node: SchemaNode): SchemaNode = node match {
case obj @ SchemaNode.Obj(nodes) if obj.keys.exists(_.contains("$")) =>
SchemaNode.Obj(Map(ObjKey -> SchemaNode.Obj(nodes.mapValues(inner))))
case SchemaNode.Obj(nodes) =>
SchemaNode.Obj(nodes.mapValues(inner))

0 comments on commit 8b97cf4

Please sign in to comment.