-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: create doc examples from ExamplesTrait in Commands
- Loading branch information
Showing
2 changed files
with
157 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...oftware/amazon/smithy/typescript/codegen/documentation/DocumentationExampleGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.typescript.codegen.documentation; | ||
|
||
import java.util.stream.Collectors; | ||
import software.amazon.smithy.model.node.ArrayNode; | ||
import software.amazon.smithy.model.node.BooleanNode; | ||
import software.amazon.smithy.model.node.Node; | ||
import software.amazon.smithy.model.node.NullNode; | ||
import software.amazon.smithy.model.node.NumberNode; | ||
import software.amazon.smithy.model.node.ObjectNode; | ||
import software.amazon.smithy.model.node.StringNode; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
@SmithyInternalApi | ||
public final class DocumentationExampleGenerator { | ||
private DocumentationExampleGenerator() {} | ||
|
||
/** | ||
* @return the ObjectNode from the curated example written as a JavaScript object literal. | ||
*/ | ||
public static String inputToJavaScriptObject(ObjectNode node) { | ||
if (node == null) { | ||
return "{ /* empty */ }"; | ||
} | ||
return write(node, 0); | ||
} | ||
|
||
public static String outputToJavaScriptObject(ObjectNode node) { | ||
if (node == null) { | ||
return "{ /* metadata only */ }"; | ||
} | ||
return write(node, 0); | ||
} | ||
|
||
private static String write(Node node, int indent) { | ||
StringBuilder buffer = new StringBuilder(); | ||
String indentation = " ".repeat(indent); | ||
|
||
switch (node.getType()) { | ||
case OBJECT -> { | ||
ObjectNode objectNode = node.expectObjectNode(); | ||
if (objectNode.getMembers().isEmpty()) { | ||
return indentation + "{ /* empty */ }"; | ||
} | ||
String membersJoined = objectNode.getMembers() | ||
.entrySet() | ||
.stream() | ||
.map(entry -> indentation | ||
+ " " | ||
+ entry.getKey().getValue() | ||
+ ": " | ||
+ write(entry.getValue(), indent + 2)) | ||
.collect(Collectors.joining(",\n")); | ||
|
||
return buffer | ||
.append("{\n") | ||
.append(membersJoined).append("\n") | ||
.append(indentation).append("}") | ||
.toString(); | ||
} | ||
case ARRAY -> { | ||
ArrayNode arrayNode = node.expectArrayNode(); | ||
if (arrayNode.getElements().isEmpty()) { | ||
return indentation + "[]"; | ||
} | ||
String membersJoined = arrayNode.getElements() | ||
.stream() | ||
.map(elementNode -> indentation | ||
+ " " | ||
+ write(elementNode, indent + 2)) | ||
.collect(Collectors.joining(",\n")); | ||
|
||
return buffer | ||
.append("[\n") | ||
.append(membersJoined).append("\n") | ||
.append(indentation).append("]") | ||
.toString(); | ||
} | ||
case STRING -> { | ||
StringNode stringNode = node.expectStringNode(); | ||
return "\"" + stringNode.getValue() + "\""; | ||
} | ||
case NUMBER -> { | ||
NumberNode numberNode = node.expectNumberNode(); | ||
return numberNode.getValue().toString(); | ||
} | ||
case BOOLEAN -> { | ||
BooleanNode booleanNode = node.expectBooleanNode(); | ||
return booleanNode.toString(); | ||
} | ||
case NULL -> { | ||
NullNode nullNode = node.expectNullNode(); | ||
return nullNode.toString(); | ||
} | ||
default -> throw new IllegalStateException("Unexpected value: " + node.getType()); | ||
} | ||
} | ||
} |