Skip to content

Commit

Permalink
recursively call filldefaultValue
Browse files Browse the repository at this point in the history
Signed-off-by: Weihang Lo <weihanglo@users.noreply.github.com>
  • Loading branch information
weihanglo committed Sep 8, 2022
1 parent 2b058d0 commit 4212818
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ class Instantiator(
memberShape.isRequired && !data.members.containsKey(Node.from(name))
}
.forEach { (_, memberShape) ->
val targetShape = model.expectShape(memberShape.target)
renderMemberHelper(memberShape, fillDefaultValue(targetShape))
renderMemberHelper(memberShape, fillDefaultValue(memberShape))
}
}

Expand All @@ -317,17 +316,16 @@ class Instantiator(
* Warning: this method does not take into account any constraint traits attached to the shape.
*/
private fun fillDefaultValue(shape: Shape): Node = when (shape) {
is MemberShape -> fillDefaultValue(model.expectShape(shape.target))

// Aggregate shapes.
is StructureShape -> Node.objectNode()
is UnionShape -> Node.objectNode()
is CollectionShape -> Node.arrayNode()
is MapShape -> Node.objectNode()

is MemberShape -> throw CodegenException("Unable to handle member shape `$shape`. Please provide target shape instead")

// Simple Shapes
is TimestampShape -> Node.from(0) // Number node for timestamp

is BlobShape -> Node.from("") // String node for bytes
is StringShape -> Node.from("")
is NumberShape -> Node.from(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,38 @@ class InstantiatorTest {
structure MyStructRequired {
@required
foo: String,
str: String,
@required
bar: PrimitiveInteger,
primitiveInt: PrimitiveInteger,
@required
baz: Integer,
int: Integer,
@required
ts: Timestamp,
@required
byteValue: Byte
byte: Byte
@required
union: MyUnion,
union: NestedUnion,
@required
structure: NestedStruct,
@required
list: MyList,
@required
map: NestedMap,
@required
doc: Document
}
union NestedUnion {
struct: NestedStruct,
int: Integer
}
structure NestedStruct {
@required
str: String,
@required
num: Integer
}
""".asSmithyModel().let { RecursiveShapeBoxer.transform(it) }

private val symbolProvider = testSymbolProvider(model)
Expand Down Expand Up @@ -256,22 +275,46 @@ class InstantiatorTest {
@Test
fun `generate struct with missing required members`() {
val structure = model.lookup<StructureShape>("com.test#MyStructRequired")
val union = model.lookup<UnionShape>("com.test#MyUnion")
val sut = Instantiator(symbolProvider, model, runtimeConfig, CodegenTarget.CLIENT)
val inner = model.lookup<StructureShape>("com.test#Inner")
val nestedStruct = model.lookup<StructureShape>("com.test#NestedStruct")
val union = model.lookup<UnionShape>("com.test#NestedUnion")
val sut = Instantiator(symbolProvider, model, runtimeConfig, CodegenTarget.SERVER)
val data = Node.parse("{}")
val writer = RustWriter.forModule("model")
structure.renderWithModelBuilder(model, symbolProvider, writer)
inner.renderWithModelBuilder(model, symbolProvider, writer)
nestedStruct.renderWithModelBuilder(model, symbolProvider, writer)
UnionGenerator(model, symbolProvider, writer, union).render()
writer.test {
writer.withBlock("let result = ", ";") {
sut.render(this, structure, data, Instantiator.defaultContext().copy(defaultsForRequiredFields = true))
}
writer.write("assert_eq!(result.foo.unwrap(), \"\");")
writer.write("assert_eq!(result.bar, 0);")
writer.write("assert_eq!(result.baz.unwrap(), 0);")
writer.write("assert_eq!(result.ts.unwrap(), aws_smithy_types::DateTime::from_secs(0));")
writer.write("assert_eq!(result.byte_value.unwrap(), 0);")
writer.write("assert_eq!(result.union.unwrap(), MyUnion::StringVariant(String::new()));")
writer.write(
"""
use std::collections::HashMap;
use aws_smithy_types::{DateTime, Document};
let expected = MyStructRequired {
str: Some("".into()),
primitive_int: 0,
int: Some(0),
ts: Some(DateTime::from_secs(0)),
byte: Some(0),
union: Some(NestedUnion::Struct(NestedStruct {
str: Some("".into()),
num: Some(0),
})),
structure: Some(NestedStruct {
str: Some("".into()),
num: Some(0),
}),
list: Some(vec![]),
map: Some(HashMap::new()),
doc: Some(Document::Object(HashMap::new())),
};
assert_eq!(result, expected);
""",
)
}
writer.compileAndTest()
}
Expand Down

0 comments on commit 4212818

Please sign in to comment.