|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.docgen.core.generators; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import java.util.Locale; |
| 10 | +import java.util.concurrent.atomic.AtomicReference; |
| 11 | +import java.util.function.Consumer; |
| 12 | +import software.amazon.smithy.docgen.core.DocGenerationContext; |
| 13 | +import software.amazon.smithy.docgen.core.DocSymbolProvider; |
| 14 | +import software.amazon.smithy.docgen.core.sections.BoundOperationSection; |
| 15 | +import software.amazon.smithy.docgen.core.sections.BoundOperationsSection; |
| 16 | +import software.amazon.smithy.docgen.core.sections.BoundResourceSection; |
| 17 | +import software.amazon.smithy.docgen.core.sections.BoundResourcesSection; |
| 18 | +import software.amazon.smithy.docgen.core.sections.ProtocolSection; |
| 19 | +import software.amazon.smithy.docgen.core.sections.ProtocolsSection; |
| 20 | +import software.amazon.smithy.docgen.core.writers.DocWriter; |
| 21 | +import software.amazon.smithy.docgen.core.writers.DocWriter.ListType; |
| 22 | +import software.amazon.smithy.model.knowledge.ServiceIndex; |
| 23 | +import software.amazon.smithy.model.shapes.EntityShape; |
| 24 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 25 | +import software.amazon.smithy.model.shapes.ResourceShape; |
| 26 | +import software.amazon.smithy.model.shapes.Shape; |
| 27 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 28 | +import software.amazon.smithy.utils.CodeInterceptor; |
| 29 | +import software.amazon.smithy.utils.CodeSection; |
| 30 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 31 | +import software.amazon.smithy.utils.StringUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * Provides common generation methods for services and resources. |
| 35 | + */ |
| 36 | +@SmithyInternalApi |
| 37 | +final class GeneratorUtils { |
| 38 | + private GeneratorUtils() {} |
| 39 | + |
| 40 | + static void generateOperationListing( |
| 41 | + DocGenerationContext context, |
| 42 | + DocWriter writer, |
| 43 | + EntityShape shape, |
| 44 | + List<OperationShape> operations |
| 45 | + ) { |
| 46 | + writer.pushState(new BoundOperationsSection(context, shape, operations)); |
| 47 | + |
| 48 | + if (operations.isEmpty()) { |
| 49 | + writer.popState(); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + var parentLinkId = context.symbolProvider().toSymbol(shape) |
| 54 | + .expectProperty(DocSymbolProvider.LINK_ID_PROPERTY, String.class); |
| 55 | + writer.openHeading("Operations", parentLinkId + "-operations"); |
| 56 | + writer.openList(ListType.UNORDERED); |
| 57 | + |
| 58 | + for (var operation : operations) { |
| 59 | + writer.pushState(new BoundOperationSection(context, shape, operation)); |
| 60 | + writeListingElement(context, writer, operation); |
| 61 | + writer.popState(); |
| 62 | + } |
| 63 | + |
| 64 | + writer.closeList(ListType.UNORDERED); |
| 65 | + writer.closeHeading(); |
| 66 | + writer.popState(); |
| 67 | + } |
| 68 | + |
| 69 | + static void generateResourceListing( |
| 70 | + DocGenerationContext context, |
| 71 | + DocWriter writer, |
| 72 | + EntityShape shape, |
| 73 | + List<ResourceShape> resources |
| 74 | + ) { |
| 75 | + writer.pushState(new BoundResourcesSection(context, shape, resources)); |
| 76 | + |
| 77 | + if (resources.isEmpty()) { |
| 78 | + writer.popState(); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + var parentLinkId = context.symbolProvider().toSymbol(shape) |
| 83 | + .expectProperty(DocSymbolProvider.LINK_ID_PROPERTY, String.class); |
| 84 | + var heading = shape.isServiceShape() ? "Resources" : "Sub-Resources"; |
| 85 | + writer.openHeading(heading, parentLinkId + "-" + heading.toLowerCase(Locale.ENGLISH)); |
| 86 | + writer.openList(ListType.UNORDERED); |
| 87 | + |
| 88 | + for (var resource : resources) { |
| 89 | + writer.pushState(new BoundResourceSection(context, shape, resource)); |
| 90 | + writeListingElement(context, writer, resource); |
| 91 | + writer.popState(); |
| 92 | + } |
| 93 | + |
| 94 | + writer.closeList(ListType.UNORDERED); |
| 95 | + writer.closeHeading(); |
| 96 | + writer.popState(); |
| 97 | + } |
| 98 | + |
| 99 | + private static void writeListingElement(DocGenerationContext context, DocWriter writer, Shape shape) { |
| 100 | + writer.openListItem(ListType.UNORDERED); |
| 101 | + var symbol = context.symbolProvider().toSymbol(shape); |
| 102 | + writer.writeInline("$R: ", symbol).writeShapeDocs(shape, context.model()); |
| 103 | + writer.closeListItem(ListType.UNORDERED); |
| 104 | + } |
| 105 | + |
| 106 | + static void writeProtocolsSection(DocGenerationContext context, DocWriter writer, Shape shape) { |
| 107 | + var protocols = ServiceIndex.of(context.model()).getProtocols(context.settings().service()).keySet(); |
| 108 | + if (protocols.isEmpty()) { |
| 109 | + return; |
| 110 | + } |
| 111 | + writer.pushState(new ProtocolsSection(context, shape)); |
| 112 | + |
| 113 | + AtomicReference<String> tabGroupContents = new AtomicReference<>(); |
| 114 | + var tabGroup = capture(writer, tabGroupWriter -> { |
| 115 | + tabGroupWriter.openTabGroup(); |
| 116 | + tabGroupContents.set(capture(tabGroupWriter, w -> { |
| 117 | + for (var protocol : protocols) { |
| 118 | + writeProtocolSection(context, w, shape, protocol); |
| 119 | + } |
| 120 | + })); |
| 121 | + tabGroupWriter.closeTabGroup(); |
| 122 | + }); |
| 123 | + |
| 124 | + if (StringUtils.isBlank(tabGroupContents.get())) { |
| 125 | + // The extra newline is needed because the section intercepting logic actually adds one |
| 126 | + // by virtue of calling write instead of writeInline |
| 127 | + writer.unwrite("$L\n", tabGroup); |
| 128 | + } |
| 129 | + |
| 130 | + writer.popState(); |
| 131 | + } |
| 132 | + |
| 133 | + private static void writeProtocolSection( |
| 134 | + DocGenerationContext context, |
| 135 | + DocWriter writer, |
| 136 | + Shape shape, |
| 137 | + ShapeId protocol |
| 138 | + ) { |
| 139 | + var protocolSymbol = context.symbolProvider().toSymbol(context.model().expectShape(protocol)); |
| 140 | + |
| 141 | + AtomicReference<String> tabContents = new AtomicReference<>(); |
| 142 | + var tab = capture(writer, tabWriter -> { |
| 143 | + tabWriter.openTab(protocolSymbol.getName()); |
| 144 | + tabContents.set(capture(tabWriter, w2 -> tabWriter.injectSection( |
| 145 | + new ProtocolSection(context, shape, protocol)))); |
| 146 | + tabWriter.closeTab(); |
| 147 | + }); |
| 148 | + |
| 149 | + if (StringUtils.isBlank(tabContents.get())) { |
| 150 | + // The extra newline is needed because the section intercepting logic actually adds one |
| 151 | + // by virtue of calling write instead of writeInline |
| 152 | + writer.unwrite("$L\n", tab); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Captures and returns what is written by the given consumer. |
| 158 | + * |
| 159 | + * @param writer The writer to capture from. |
| 160 | + * @param consumer A consumer that writes text to be captured. |
| 161 | + * @return Returns what was written by the consumer. |
| 162 | + */ |
| 163 | + private static String capture(DocWriter writer, Consumer<DocWriter> consumer) { |
| 164 | + var recorder = new RecordingInterceptor(); |
| 165 | + writer.pushState(new CapturingSection()).onSection(recorder); |
| 166 | + consumer.accept(writer); |
| 167 | + writer.popState(); |
| 168 | + return recorder.getContents(); |
| 169 | + } |
| 170 | + |
| 171 | + private record CapturingSection() implements CodeSection {} |
| 172 | + |
| 173 | + /** |
| 174 | + * Records what was written to the section previously and writes it back. |
| 175 | + */ |
| 176 | + private static final class RecordingInterceptor implements CodeInterceptor<CapturingSection, DocWriter> { |
| 177 | + private String contents = null; |
| 178 | + |
| 179 | + public String getContents() { |
| 180 | + return contents; |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public Class<CapturingSection> sectionType() { |
| 185 | + return CapturingSection.class; |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public void write(DocWriter writer, String previousText, CapturingSection section) { |
| 190 | + contents = previousText; |
| 191 | + writer.writeWithNoFormatting(previousText); |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments