From 181a7bd53f18c27cc0c32e8921c2b8e84ccd6f63 Mon Sep 17 00:00:00 2001 From: Matt Hicks Date: Mon, 27 Nov 2023 18:06:57 -0600 Subject: [PATCH] Better JSON representation of GeoJSON and updated Fabric --- .../scala/com/outr/arango/geo/GeoJSON.scala | 41 +++++++++++++------ .../outr/arango/json/JsonDocumentModel.scala | 3 +- project/Dependencies.scala | 2 +- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/core/src/main/scala/com/outr/arango/geo/GeoJSON.scala b/core/src/main/scala/com/outr/arango/geo/GeoJSON.scala index 6893b63e..2b801e3e 100644 --- a/core/src/main/scala/com/outr/arango/geo/GeoJSON.scala +++ b/core/src/main/scala/com/outr/arango/geo/GeoJSON.scala @@ -21,13 +21,22 @@ object GeoJSON { } private[geo] def pointsFromCoords(json: Json): List[GeoPoint] = json.asArr.value.toList.map(pointFromCoords) private[geo] def multiPointsFromCoords(json: Json): List[List[GeoPoint]] = json.asArr.value.toList.map(pointsFromCoords) - private[geo] def createRW[T <: GeoJSON](toCoordinates: T => Json, fromCoordinates: Json => T): RW[T] = RW.from( - r = t => obj( - "coordinates" -> toCoordinates(t) - ), - w = j => fromCoordinates(j("coordinates")), - d = DefType.Json - ) + private[geo] def createRW[T <: GeoJSON](toCoordinates: T => Json, + fromCoordinates: Json => T, + dimensions: Int): RW[T] = { + def d(dim: Int): DefType = if (dim == 0) { + DefType.Dec + } else { + DefType.Arr(d(dim - 1)) + } + RW.from( + r = t => obj( + "coordinates" -> toCoordinates(t) + ), + w = j => fromCoordinates(j("coordinates")), + d = DefType.Obj(None, "coordinates" -> d(dimensions)) + ) + } implicit lazy val rw: RW[GeoJSON] = RW.poly[GeoJSON](getType = _.getClass.getSimpleName.replace("$", ""))( "GeoPoint" -> GeoPoint.rw, @@ -44,7 +53,8 @@ case class GeoPoint(latitude: Double, longitude: Double) extends GeoJSON object GeoPoint { implicit val rw: RW[GeoPoint] = createRW[GeoPoint]( point => pointArray(point), - pointFromCoords + pointFromCoords, + dimensions = 1 ).withPostRead(addType("GeoPoint")) } @@ -53,7 +63,8 @@ case class GeoMultiPoint(points: List[GeoPoint]) extends GeoJSON object GeoMultiPoint { implicit val rw: RW[GeoMultiPoint] = createRW[GeoMultiPoint]( mp => mp.points.map(pointArray).json, - json => GeoMultiPoint(pointsFromCoords(json)) + json => GeoMultiPoint(pointsFromCoords(json)), + dimensions = 2 ).withPostRead(addType("GeoMultiPoint")) } @@ -62,7 +73,8 @@ case class GeoLineString(points: List[GeoPoint]) extends GeoJSON object GeoLineString { implicit val rw: RW[GeoLineString] = createRW[GeoLineString]( ls => ls.points.map(pointArray).json, - json => GeoLineString(pointsFromCoords(json)) + json => GeoLineString(pointsFromCoords(json)), + dimensions = 2 ).withPostRead(addType("GeoLineString")) } @@ -71,7 +83,8 @@ case class GeoMultiLineString(lines: List[List[GeoPoint]]) extends GeoJSON object GeoMultiLineString { implicit val rw: RW[GeoMultiLineString] = createRW[GeoMultiLineString]( mls => mls.lines.map(_.map(pointArray)).json, - json => GeoMultiLineString(multiPointsFromCoords(json)) + json => GeoMultiLineString(multiPointsFromCoords(json)), + dimensions = 3 ).withPostRead(addType("GeoMultiLineString")) } @@ -80,7 +93,8 @@ case class GeoPolygon(points: List[GeoPoint]) extends GeoJSON object GeoPolygon { implicit val rw: RW[GeoPolygon] = createRW[GeoPolygon]( p => p.points.map(pointArray).json, - json => GeoPolygon(pointsFromCoords(json)) + json => GeoPolygon(pointsFromCoords(json)), + dimensions = 3 ).withPostRead(addType("GeoPolygon")) } @@ -89,6 +103,7 @@ case class GeoMultiPolygon(polygons: List[List[GeoPoint]]) extends GeoJSON object GeoMultiPolygon { implicit val rw: RW[GeoMultiPolygon] = createRW[GeoMultiPolygon]( mp => mp.polygons.map(_.map(pointArray)).json, - json => GeoMultiPolygon(multiPointsFromCoords(json)) + json => GeoMultiPolygon(multiPointsFromCoords(json)), + dimensions = 4 ).withPostRead(addType("GeoMultiPolygon")) } \ No newline at end of file diff --git a/core/src/main/scala/com/outr/arango/json/JsonDocumentModel.scala b/core/src/main/scala/com/outr/arango/json/JsonDocumentModel.scala index 0be84082..fe454d1e 100644 --- a/core/src/main/scala/com/outr/arango/json/JsonDocumentModel.scala +++ b/core/src/main/scala/com/outr/arango/json/JsonDocumentModel.scala @@ -12,7 +12,8 @@ trait JsonDocumentModel extends DocumentModel[JsonDocument] { r = jd => jd.json.merge(obj("_id" -> jd._id)), w = j => JsonDocument(j, j("_id").as[Id[JsonDocument]]), d = DefType.Obj( - "json" -> DefType.Obj(), + None, + "json" -> DefType.Obj(None), "_id" -> Id.rw.definition ) ) diff --git a/project/Dependencies.scala b/project/Dependencies.scala index b4ef9b89..12338975 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -2,7 +2,7 @@ import sbt.* object Dependencies { object version { - val fabric: String = "1.12.6" + val fabric: String = "1.12.8" val profig: String = "3.4.11"