Skip to content

Commit

Permalink
Add date-time timestamp handling on top of epoch-seconds timestamp ha…
Browse files Browse the repository at this point in the history
…ndling, to ShapeValueGenerator. Enhance codegen test.
  • Loading branch information
Sichan Yoo committed Nov 20, 2024
1 parent 02e79dc commit 4bad21e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import software.amazon.smithy.model.shapes.MapShape
import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.model.shapes.ShapeType
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.shapes.TimestampShape
import software.amazon.smithy.model.shapes.UnionShape
import software.amazon.smithy.model.traits.EnumTrait
import software.amazon.smithy.model.traits.StreamingTrait
import software.amazon.smithy.swift.codegen.model.hasTrait
import software.amazon.smithy.swift.codegen.model.toMemberNames
import software.amazon.smithy.swift.codegen.swiftmodules.SmithyStreamsTypes
import software.amazon.smithy.swift.codegen.swiftmodules.SmithyTimestampsTypes
import software.amazon.smithy.swift.codegen.swiftmodules.SmithyTypes
import software.amazon.smithy.swift.codegen.utils.toLowerCamelCase
import software.amazon.smithy.utils.StringUtils.lowerCase
Expand Down Expand Up @@ -243,6 +245,14 @@ class ShapeValueGenerator(

override fun stringNode(node: StringNode) {
when (currShape) {
is TimestampShape -> {
val value = node.expectStringNode().value
writer.writeInline(
"\$N(format: .dateTime).date(from: \$S)",
SmithyTimestampsTypes.TimestampFormatter,
value
)
}
is DoubleShape, is FloatShape -> {
val symbol = generator.symbolProvider.toSymbol(currShape)
val value = when (node.value.toString()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,20 @@ Set<Swift.String>(arrayLiteral:
val member2 = MemberShape.builder().id("foo.bar#MyStruct\$boolMember").target("smithy.api#Boolean").build()
val member3 = MemberShape.builder().id("foo.bar#MyStruct\$intMember").target("smithy.api#Integer").build()

val nestedMember1 = MemberShape.builder().id("foo.bar#Nested\$tsMember").target("smithy.api#Timestamp").build()
val nestedMember1 = MemberShape.builder().id("foo.bar#Nested\$epochSecondsMember").target("smithy.api#Timestamp").build()
val nested = StructureShape.builder()
.id("foo.bar#Nested")
.addMember(nestedMember1)
.build()

val nestedMember2 = MemberShape.builder().id("foo.bar#Nested2\$dateTimeMember").target("smithy.api#Timestamp").build()
val nested2 = StructureShape.builder()
.id("foo.bar#Nested2")
.addMember(nestedMember2)
.build()

val member4 = MemberShape.builder().id("foo.bar#MyStruct\$structMember").target("foo.bar#Nested").build()
val member9 = MemberShape.builder().id("foo.bar#MyStruct\$structMember2").target("foo.bar#Nested2").build()

val enumTrait = EnumTrait.builder()
.addEnum(EnumDefinition.builder().value("fooey").name("FOO").build())
Expand All @@ -195,10 +202,12 @@ Set<Swift.String>(arrayLiteral:
.addMember(member6)
.addMember(member7)
.addMember(member8)
.addMember(member9)
.build()
val model = Model.assembler()
.addShapes(struct, member1, member2, member3)
.addShapes(member4, nested, nestedMember1)
.addShapes(member9, nested2, nestedMember2)
.addShapes(member5, enumShape)
.addShapes(member6, member7, member8)
.assemble()
Expand All @@ -216,7 +225,13 @@ Set<Swift.String>(arrayLiteral:
.withMember(
"structMember",
Node.objectNodeBuilder()
.withMember("tsMember", 11223344)
.withMember("epochSecondsMember", 11223344)
.build()
)
.withMember(
"structMember2",
Node.objectNodeBuilder()
.withMember("dateTimeMember", "1970-01-01T00:00:00Z")
.build()
)
.withMember("enumMember", "fooey")
Expand All @@ -238,7 +253,10 @@ MyStruct(
nullMember: nil,
stringMember: "v1",
structMember: Nested(
tsMember: Date(timeIntervalSince1970: 11223344)
epochSecondsMember: Date(timeIntervalSince1970: 11223344)
),
structMember2: Nested2(
dateTimeMember: SmithyTimestamps.TimestampFormatter(format: .dateTime).date(from: "1970-01-01T00:00:00Z")
)
)
""".trimIndent()
Expand Down

0 comments on commit 4bad21e

Please sign in to comment.