Skip to content

Commit

Permalink
fix: Add missing nano part when encoding timestamp to document
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostbuster91 committed Aug 31, 2024
1 parent d4edb32 commit d2d21f4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
24 changes: 24 additions & 0 deletions modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,30 @@ class DocumentSpec() extends FunSuite {
)
}

test("Document encoder - timestamp epoch seconds with nanos") {
val timestampWithNanos = Timestamp(1716459630L, 5000000)
val result = Document.Encoder
.withExplicitDefaultsEncoding(false)
.fromSchema(TimestampOperationInput.schema)
.encode(
TimestampOperationInput(
timestampWithNanos,
timestampWithNanos,
timestampWithNanos
)
)
expect.same(
Document.obj(
"httpDate" -> Document.fromString("Thu, 23 May 2024 10:20:30.005 GMT"),
"dateTime" -> Document.fromString("2024-05-23T10:20:30.005Z"),
"epochSeconds" -> Document.fromBigDecimal(
BigDecimal("1716459630.500000")
)
),
result
)
}

test("Document decoder - timestamp defaults") {
val doc = Document.obj()
val result = Document.Decoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ class DocumentEncoderSchemaVisitor(
hints
.get(TimestampFormat)
.getOrElse(TimestampFormat.EPOCH_SECONDS) match {
case DATE_TIME => ts => DString(ts.format(DATE_TIME))
case HTTP_DATE => ts => DString(ts.format(HTTP_DATE))
case EPOCH_SECONDS => ts => DNumber(BigDecimal(ts.epochSecond))
case DATE_TIME => ts => DString(ts.format(DATE_TIME))
case HTTP_DATE => ts => DString(ts.format(HTTP_DATE))
case EPOCH_SECONDS =>
ts => DNumber(BigDecimal(s"${ts.epochSecond}.${ts.nano}"))
}
case PDocument => from(identity)
case PFloat => from(float => DNumber(BigDecimal(float.toDouble)))
Expand Down

0 comments on commit d2d21f4

Please sign in to comment.