-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fs2-aws-ciris with decode support for kinesis initial position (#375
) * Add fs2-aws-ciris with decode support for Either[InitialPositionInStream, Date] * bump many versions
- Loading branch information
1 parent
a12252d
commit 9d6e295
Showing
4 changed files
with
147 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package fs2.aws | ||
|
||
import java.util.Date | ||
|
||
import cats.implicits._ | ||
import _root_.ciris.ConfigDecoder | ||
import software.amazon.kinesis.common.InitialPositionInStream | ||
|
||
import scala.util.matching.Regex | ||
|
||
package object ciris { | ||
|
||
/** | ||
* Ciris decoder support for Either[InitialPositionInStream, Date], useful when building [[fs2.aws.kinesis.KinesisConsumerSettings]] | ||
* | ||
* Usage: | ||
* `env("FOOBAR").as[Either[InitialPositionInStream, Date]]` | ||
* | ||
* <ul> | ||
* <li>`"TRIM_HORIZON"` becomes `Left(InitialPositionInStream.TRIM_HORIZON)` (consume from beginning)</li> | ||
* <li>`"LATEST"` becomes `(Left[InitialPositionInStream.LATEST)` (consume from latest)</li> | ||
* <li>`"TS_123456"` becomes `Right(Date(123456))` (consume from timestamp)</li> | ||
* </ul> | ||
* | ||
*/ | ||
implicit def kinesisInitialPositionDecoder[T]( | ||
implicit decoder: ConfigDecoder[T, String] | ||
): ConfigDecoder[T, Either[InitialPositionInStream, Date]] = { | ||
|
||
val DateRegex: Regex = """^TS_([0-9]+)$""".r | ||
|
||
decoder.mapOption(typeName = "InitialPositionInStream") { | ||
case "TRIM_HORIZON" => Some(Left(InitialPositionInStream.TRIM_HORIZON)) | ||
case "LATEST" => Some(Left(InitialPositionInStream.LATEST)) | ||
case DateRegex(millis) => Some(Right(new Date(millis.toLong))) | ||
case _ => None | ||
} | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
fs2-aws-ciris/src/test/scala/fs2/aws/ciris/CirisDecoderSpec.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,56 @@ | ||
package fs2.aws.ciris; | ||
|
||
import java.util.Date | ||
|
||
import cats.effect.{ ContextShift, IO } | ||
import ciris.{ ConfigException, ConfigValue } | ||
import org.scalatest.Assertion | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
import software.amazon.kinesis.common.InitialPositionInStream | ||
|
||
import scala.concurrent.ExecutionContext.Implicits.global; | ||
|
||
class CirisDecoderSpec extends AnyWordSpec with Matchers { | ||
implicit val cs: ContextShift[IO] = IO.contextShift(global) | ||
|
||
"InitialPositionDecoderSpec" should { | ||
|
||
"when decoding Either[InitialPositionInStream, Date]" can { | ||
|
||
// same package, so `import fs2.aws.ciris._` not necessary here | ||
def decode(testStr: String): Either[InitialPositionInStream, Date] = | ||
ConfigValue | ||
.default(testStr) | ||
.as[Either[InitialPositionInStream, Date]] | ||
.load[IO] | ||
.unsafeRunSync() | ||
|
||
def expectDecodeFailure(testString: String): Assertion = | ||
intercept[ConfigException] { | ||
decode(testString) | ||
}.getMessage should include( | ||
s"Unable to convert value $testString to InitialPositionInStream" | ||
) | ||
|
||
"decode supported strings as initial offsets" in { | ||
|
||
decode("LATEST") should equal(Left(InitialPositionInStream.LATEST)) | ||
decode("TRIM_HORIZON") should equal(Left(InitialPositionInStream.TRIM_HORIZON)) | ||
decode("TS_1592404273000") should equal(Right(new Date(1592404273000L))) | ||
|
||
} | ||
|
||
"fail to decode valid strings" in { | ||
|
||
expectDecodeFailure("FOOBAR") | ||
expectDecodeFailure("TS_FOO") | ||
expectDecodeFailure("TS_") | ||
expectDecodeFailure("_1592404273000") | ||
|
||
} | ||
} | ||
|
||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version=1.3.10 | ||
sbt.version=1.3.11 |