Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pomadchin committed Dec 24, 2020
1 parent 7a275a2 commit ed8fda8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.azavea.stac4s.api.client

import com.azavea.stac4s.Bbox
import com.azavea.stac4s.api.client.utils.ClientCodecs
import com.azavea.stac4s.geometry.Geometry
import com.azavea.stac4s.types.TemporalExtent

import cats.instances.either._
import cats.syntax.apply._
import cats.syntax.either._
import eu.timepit.refined.types.numeric.NonNegInt
import io.circe._
import io.circe.generic.semiauto._
import io.circe.refined._

import java.time.Instant

case class SearchFilters(
bbox: Option[Bbox] = None,
datetime: Option[TemporalExtent] = None,
Expand All @@ -25,55 +21,7 @@ case class SearchFilters(
next: Option[PaginationToken] = None
)

object SearchFilters {

// TemporalExtent STAC API compatible serialization
// Ported from https://github.com/azavea/franklin/
private def stringToInstant(s: String): Either[Throwable, Instant] =
Either.catchNonFatal(Instant.parse(s))

private def temporalExtentToString(te: TemporalExtent): String =
te.value match {
case Some(start) :: Some(end) :: _ if start != end => s"${start.toString}/${end.toString}"
case Some(start) :: Some(end) :: _ if start == end => s"${start.toString}"
case Some(start) :: None :: _ => s"${start.toString}/.."
case None :: Some(end) :: _ => s"../${end.toString}"
}

private def temporalExtentFromString(str: String): Either[String, TemporalExtent] = {
str.split("/").toList match {
case ".." :: endString :: _ =>
val parsedEnd = stringToInstant(endString)
parsedEnd match {
case Left(_) => s"Could not decode instant: $str".asLeft
case Right(end: Instant) => TemporalExtent(None, end).asRight
}
case startString :: ".." :: _ =>
val parsedStart = stringToInstant(startString)
parsedStart match {
case Left(_) => s"Could not decode instant: $str".asLeft
case Right(start: Instant) => TemporalExtent(start, None).asRight
}
case startString :: endString :: _ =>
val parsedStart = stringToInstant(startString)
val parsedEnd = stringToInstant(endString)
(parsedStart, parsedEnd).tupled match {
case Left(_) => s"Could not decode instant: $str".asLeft
case Right((start: Instant, end: Instant)) => TemporalExtent(start, end).asRight
}
case _ =>
Either.catchNonFatal(Instant.parse(str)) match {
case Left(_) => s"Could not decode instant: $str".asLeft
case Right(t: Instant) => TemporalExtent(t, t).asRight
}
}
}

implicit val encoderTemporalExtent: Encoder[TemporalExtent] =
Encoder.encodeString.contramap[TemporalExtent](temporalExtentToString)

implicit val decoderTemporalExtent: Decoder[TemporalExtent] =
Decoder.decodeString.emap(temporalExtentFromString)
object SearchFilters extends ClientCodecs {

implicit val searchFilterDecoder: Decoder[SearchFilters] = { c =>
for {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.azavea.stac4s.api.client

import com.azavea.stac4s.Bbox
import com.azavea.stac4s.api.client.utils.ClientCodecs
import com.azavea.stac4s.types.TemporalExtent

import cats.instances.either._
import cats.syntax.apply._
import cats.syntax.either._
import eu.timepit.refined.types.numeric.NonNegInt
import geotrellis.vector.{io => _, _}
import io.circe._
import io.circe.generic.semiauto._
import io.circe.refined._

import java.time.Instant

case class SearchFilters(
bbox: Option[Bbox] = None,
datetime: Option[TemporalExtent] = None,
Expand All @@ -25,55 +21,7 @@ case class SearchFilters(
next: Option[PaginationToken] = None
)

object SearchFilters {

// TemporalExtent STAC API compatible serialization
// Ported from https://github.com/azavea/franklin/
private def stringToInstant(s: String): Either[Throwable, Instant] =
Either.catchNonFatal(Instant.parse(s))

private def temporalExtentToString(te: TemporalExtent): String =
te.value match {
case Some(start) :: Some(end) :: _ if start != end => s"${start.toString}/${end.toString}"
case Some(start) :: Some(end) :: _ if start == end => s"${start.toString}"
case Some(start) :: None :: _ => s"${start.toString}/.."
case None :: Some(end) :: _ => s"../${end.toString}"
}

private def temporalExtentFromString(str: String): Either[String, TemporalExtent] = {
str.split("/").toList match {
case ".." :: endString :: _ =>
val parsedEnd = stringToInstant(endString)
parsedEnd match {
case Left(_) => s"Could not decode instant: $str".asLeft
case Right(end: Instant) => TemporalExtent(None, end).asRight
}
case startString :: ".." :: _ =>
val parsedStart = stringToInstant(startString)
parsedStart match {
case Left(_) => s"Could not decode instant: $str".asLeft
case Right(start: Instant) => TemporalExtent(start, None).asRight
}
case startString :: endString :: _ =>
val parsedStart = stringToInstant(startString)
val parsedEnd = stringToInstant(endString)
(parsedStart, parsedEnd).tupled match {
case Left(_) => s"Could not decode instant: $str".asLeft
case Right((start: Instant, end: Instant)) => TemporalExtent(start, end).asRight
}
case _ =>
Either.catchNonFatal(Instant.parse(str)) match {
case Left(_) => s"Could not decode instant: $str".asLeft
case Right(t: Instant) => TemporalExtent(t, t).asRight
}
}
}

implicit val encoderTemporalExtent: Encoder[TemporalExtent] =
Encoder.encodeString.contramap[TemporalExtent](temporalExtentToString)

implicit val decoderTemporalExtent: Decoder[TemporalExtent] =
Decoder.decodeString.emap(temporalExtentFromString)
object SearchFilters extends ClientCodecs {

implicit val searchFilterDecoder: Decoder[SearchFilters] = { c =>
for {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.azavea.stac4s.api.client.utils

import com.azavea.stac4s.types.TemporalExtent

import cats.syntax.apply._
import cats.syntax.either._
import io.circe.{Decoder, Encoder}

import java.time.Instant

trait ClientCodecs {

// TemporalExtent STAC API compatible serialization
// Ported from https://github.com/azavea/franklin/
private def stringToInstant(s: String): Either[Throwable, Instant] =
Either.catchNonFatal(Instant.parse(s))

private def temporalExtentToString(te: TemporalExtent): String =
te.value match {
case Some(start) :: Some(end) :: _ if start != end => s"${start.toString}/${end.toString}"
case Some(start) :: Some(end) :: _ if start == end => s"${start.toString}"
case Some(start) :: None :: _ => s"${start.toString}/.."
case None :: Some(end) :: _ => s"../${end.toString}"
}

private def temporalExtentFromString(str: String): Either[String, TemporalExtent] = {
str.split("/").toList match {
case ".." :: endString :: _ =>
val parsedEnd = stringToInstant(endString)
parsedEnd match {
case Left(_) => s"Could not decode instant: $str".asLeft
case Right(end: Instant) => TemporalExtent(None, end).asRight
}
case startString :: ".." :: _ =>
val parsedStart = stringToInstant(startString)
parsedStart match {
case Left(_) => s"Could not decode instant: $str".asLeft
case Right(start: Instant) => TemporalExtent(start, None).asRight
}
case startString :: endString :: _ =>
val parsedStart = stringToInstant(startString)
val parsedEnd = stringToInstant(endString)
(parsedStart, parsedEnd).tupled match {
case Left(_) => s"Could not decode instant: $str".asLeft
case Right((start: Instant, end: Instant)) => TemporalExtent(start, end).asRight
}
case _ =>
Either.catchNonFatal(Instant.parse(str)) match {
case Left(_) => s"Could not decode instant: $str".asLeft
case Right(t: Instant) => TemporalExtent(t, t).asRight
}
}
}

implicit lazy val encoderTemporalExtent: Encoder[TemporalExtent] =
Encoder.encodeString.contramap[TemporalExtent](temporalExtentToString)

implicit lazy val decoderTemporalExtent: Decoder[TemporalExtent] =
Decoder.decodeString.emap(temporalExtentFromString)

}

0 comments on commit ed8fda8

Please sign in to comment.