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

feat(go): Add smithy-model API documentation for Go #785

Draft
wants to merge 2 commits into
base: main-1.x
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.function.Predicate;
import java.util.logging.Logger;
import software.amazon.polymorph.smithygo.codegen.knowledge.GoPointableIndex;
import software.amazon.polymorph.utils.ModelUtils;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
Expand Down Expand Up @@ -460,4 +461,22 @@ public static boolean isNumber(Shape shape) {
return false;
}
}

/**
* docFromShapeEmpty returns the documentation string from the smithy-model if it exists
*
* @param shape shape to get documentation
* @return doc string
*/
protected static String docFromShape(Shape shape) {
Optional<String> maybeDoc = ModelUtils.getDocumentationOrJavadoc(shape);
if (maybeDoc.isPresent()) {
return (
"// " + String.join("\n// ", maybeDoc.get().split("\\r?\\n"))
);
} else {
// Don't create a documentation string if the smithy model doesn't have one
return "//";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void run() {
// a name.
if (enumTrait.getValues().get(0).getName().isPresent()) {
Set<String> constants = new LinkedHashSet<>();
writer.openBlock(CodegenUtils.docFromShape(shape));
writer
.openBlock(
"const (",
Expand Down Expand Up @@ -110,12 +111,14 @@ public void run() {
.write("");
}

writer.openBlock(CodegenUtils.docFromShape(shape));
writer.openBlock(
"func ($L) Values() []$L {",
"}",
symbol.getName(),
symbol.getName(),
() -> {
writer.openBlock(CodegenUtils.docFromShape(shape));
writer.openBlock(
"return []$L{",
"}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void run() {
writer.write("type $L = int32", symbol.getName()).write("");

Set<String> constants = new LinkedHashSet<>();
writer.openBlock(CodegenUtils.docFromShape(shape));
writer
.openBlock(
"const (",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.polymorph.smithygo.codegen;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import software.amazon.polymorph.smithygo.localservice.nameresolver.DafnyNameResolver;
import software.amazon.polymorph.smithygo.localservice.nameresolver.SmithyNameResolver;
Expand All @@ -25,6 +26,7 @@
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.model.traits.StreamingTrait;
Expand Down Expand Up @@ -90,6 +92,7 @@ public void renderStructure(Runnable runnable) {
public void renderStructure(Runnable runnable, boolean isInputStructure) {
writer.addImport("fmt");
Symbol symbol = symbolProvider.toSymbol(shape);
writer.openBlock(CodegenUtils.docFromShape(shape));
writer.openBlock("type $L struct {", symbol.getName());
CodegenUtils.SortedMembers sortedMembers = new CodegenUtils.SortedMembers(
symbolProvider
Expand Down Expand Up @@ -194,6 +197,7 @@ private void renderErrorStructure() {
writer.addUseImports(SmithyGoDependency.FMT);
ErrorTrait errorTrait = shape.expectTrait(ErrorTrait.class);

writer.openBlock(CodegenUtils.docFromShape(shape));
// Write out a struct to hold the error data.
writer
.openBlock(
Expand Down Expand Up @@ -228,6 +232,7 @@ private void renderErrorStructure() {
)
.write("");

writer.openBlock(CodegenUtils.docFromShape(shape));
// write the Error method to satisfy the standard error interface
writer.openBlock(
"func (e $L) Error() string {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void generateUnion(GoWriter writer) {
.forEach(name -> {
writer.write("// " + name);
});
writer.openBlock(CodegenUtils.docFromShape(shape));
writer
.openBlock(
"type $L interface {",
Expand All @@ -76,6 +77,7 @@ public void generateUnion(GoWriter writer) {
String exportedMemberName = symbolProvider.toMemberName(member);
Shape target = model.expectShape(member.getTarget());

writer.openBlock(CodegenUtils.docFromShape(shape));
writer.openBlock(
"type $L struct {",
"}",
Expand Down Expand Up @@ -168,6 +170,7 @@ public static void generateUnknownUnion(
final Collection<UnionShape> unions,
final SymbolProvider symbolProvider
) {
// Ignoring API Documentation trait here because this is for Smithy-V2 and we don't use it right now.
writer.openBlock(
"type $L struct {",
"}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void renderValidator(
final boolean isInputStructure
) {
final Symbol symbol = symbolProvider.toSymbol(shape);
writer.openBlock(CodegenUtils.docFromShape(shape));
writer.openBlock("func (input $L) Validate() (error) {", symbol.getName());
writer.write(
renderValidatorHelper(
Expand Down
Loading