Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate better docs when fields are required #2996

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[[smithy-rs]]
message = "It's now possible to nest runtime components with the `RuntimePlugin` trait. A `current_components` argument was added to the `runtime_components` method so that components configured from previous runtime plugins can be referenced in the current runtime plugin. Ordering of runtime plugins was also introduced via a new `RuntimePlugin::order` method."
references = ["smithy-rs#2909"]
meta = { "breaking" = true, "tada" = false, "bug" = false, "target" = "client"}
meta = { "breaking" = true, "tada" = false, "bug" = false, "target" = "client" }
author = "jdisanti"

[[aws-sdk-rust]]
Expand Down Expand Up @@ -177,3 +177,9 @@ message = "Source defaults from the default trait instead of implicitly based on
references = ["smithy-rs#2985"]
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "client" }
author = "rcoh"

[[smithy-rs]]
message = "Produce better docs when items are marked @required"
references = ["smithy-rs#2996"]
meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client" }
author = "rcoh"
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ private fun generateOperationShapeDocs(
else -> "(undocumented)"
}

"[`$builderInputDoc`]($builderInputLink) / [`$builderSetterDoc`]($builderSetterLink): $docs"
"[`$builderInputDoc`]($builderInputLink) / [`$builderSetterDoc`]($builderSetterLink):<br>required: **${memberShape.isRequired}**<br>$docs<br>"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ class BuilderGenerator(
false -> implBlockWriter.format(outputSymbol)
}
implBlockWriter.docs("Consumes the builder and constructs a #D.", outputSymbol)
val trulyRequiredMembers = members.filter { trulyRequired(it) }
if (trulyRequiredMembers.isNotEmpty()) {
implBlockWriter.docs("This method will fail if any of the following fields are not set:")
trulyRequiredMembers.forEach {
val memberName = symbolProvider.toMemberName(it)
implBlockWriter.docs("- [`$memberName`](#T::$memberName)", symbolProvider.symbolForBuilder(shape))
}
}
implBlockWriter.rustBlockTemplate("pub fn build(self) -> $returnType", *preludeScope) {
conditionalBlockTemplate("#{Ok}(", ")", conditional = fallibleBuilder, *preludeScope) {
// If a wrapper is specified, use the `::new` associated function to construct the wrapper
Expand Down Expand Up @@ -216,6 +224,9 @@ class BuilderGenerator(
val input = coreType.asArgument("input")

writer.documentShape(member, model)
if (member.isRequired) {
writer.docs("This field is required.")
}
writer.deprecatedShape(member)
writer.rustBlock("pub fn $memberName(mut self, ${input.argument}) -> Self") {
rustTemplate("self.$memberName = #{Some}(${input.value});", *preludeScope)
Expand Down Expand Up @@ -369,6 +380,10 @@ class BuilderGenerator(
}
}

private fun trulyRequired(member: MemberShape) = symbolProvider.toSymbol(member).let {
!it.isOptional() && !it.canUseDefault()
}

/**
* The core builder of the inner type. If the structure requires a fallible builder, this may use `?` to return
* errors.
Expand Down
Loading