-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
65 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
modules/client/shared/src/main/scala/com/azavea/stac4s/api/client/utils/ClientCodecs.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |