|
17 | 17 | import com.google.api.generator.engine.ast.CommentStatement; |
18 | 18 | import com.google.api.generator.engine.ast.JavaDocComment; |
19 | 19 | import com.google.api.generator.engine.ast.TypeNode; |
| 20 | +import com.google.api.generator.gapic.model.Service; |
| 21 | +import com.google.api.generator.gapic.utils.JavaStyle; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
20 | 24 |
|
21 | 25 | class ServiceClientCommentComposer { |
| 26 | + // Tokens. |
22 | 27 | private static final String COLON = ":"; |
23 | 28 |
|
| 29 | + // Constants. |
| 30 | + private static final String SERVICE_DESCRIPTION_INTRO_STRING = |
| 31 | + "This class provides the ability to make remote calls to the backing service through method " |
| 32 | + + "calls that map to API methods. Sample code to get started:"; |
| 33 | + private static final String SERVICE_DESCRIPTION_CLOSE_STRING = |
| 34 | + "Note: close() needs to be called on the echoClient object to clean up resources such as " |
| 35 | + + "threads. In the example above, try-with-resources is used, which automatically calls " |
| 36 | + + "close()."; |
| 37 | + private static final String SERVICE_DESCRIPTION_SURFACE_SUMMARY_STRING = |
| 38 | + "The surface of this class includes several types of Java methods for each of the API's " |
| 39 | + + "methods:"; |
| 40 | + private static final String SERVICE_DESCRIPTION_SURFACE_CODA_STRING = |
| 41 | + "See the individual methods for example code."; |
| 42 | + private static final String SERVICE_DESCRIPTION_RESOURCE_NAMES_FORMATTING_STRING = |
| 43 | + "Many parameters require resource names to be formatted in a particular way. To assist with" |
| 44 | + + " these names, this class includes a format method for each type of name, and" |
| 45 | + + " additionally a parse method to extract the individual identifiers contained within" |
| 46 | + + " names that are returned."; |
| 47 | + private static final String SERVICE_DESCRIPTION_CREDENTIALS_SUMMARY_STRING = |
| 48 | + "To customize credentials:"; |
| 49 | + private static final String SERVICE_DESCRIPTION_ENDPOINT_SUMMARY_STRING = |
| 50 | + "To customize the endpoint:"; |
| 51 | + |
| 52 | + private static final List<String> SERVICE_DESCRIPTION_SURFACE_DESCRIPTION = |
| 53 | + Arrays.asList( |
| 54 | + "A \"flattened\" method. With this type of method, the fields of the request type have" |
| 55 | + + " been converted into function parameters. It may be the case that not all fields" |
| 56 | + + " are available as parameters, and not every API method will have a flattened" |
| 57 | + + " method entry point.", |
| 58 | + "A \"request object\" method. This type of method only takes one parameter, a request" |
| 59 | + + " object, which must be constructed before the call. Not every API method will" |
| 60 | + + " have a request object method.", |
| 61 | + "A \"callable\" method. This type of method takes no parameters and returns an immutable " |
| 62 | + + "API callable object, which can be used to initiate calls to the service."); |
| 63 | + |
| 64 | + // Patterns. |
24 | 65 | private static final String CREATE_METHOD_STUB_ARG_PATTERN = |
25 | 66 | "Constructs an instance of EchoClient, using the given stub for making calls. This is for" |
26 | 67 | + " advanced usage - prefer using create(%s)."; |
27 | 68 |
|
| 69 | + private static final String SERVICE_DESCRIPTION_CUSTOMIZE_SUMMARY_PATTERN = |
| 70 | + "This class can be customized by passing in a custom instance of %s to create(). For" |
| 71 | + + " example:"; |
| 72 | + |
| 73 | + private static final String SERVICE_DESCRIPTION_SUMMARY_PATTERN = "Service Description: %s"; |
| 74 | + |
| 75 | + // Comments. |
28 | 76 | static final CommentStatement CREATE_METHOD_NO_ARG_COMMENT = |
29 | 77 | toSimpleComment("Constructs an instance of EchoClient with default settings."); |
30 | 78 |
|
@@ -45,6 +93,41 @@ class ServiceClientCommentComposer { |
45 | 93 | "Returns the OperationsClient that can be used to query the status of a long-running" |
46 | 94 | + " operation returned by another API method call."); |
47 | 95 |
|
| 96 | + static List<CommentStatement> createClassHeaderComments(Service service) { |
| 97 | + JavaDocComment.Builder classHeaderJavadocBuilder = JavaDocComment.builder(); |
| 98 | + if (service.hasDescription()) { |
| 99 | + classHeaderJavadocBuilder.addComment( |
| 100 | + String.format(SERVICE_DESCRIPTION_SUMMARY_PATTERN, service.description())); |
| 101 | + } |
| 102 | + |
| 103 | + // Service introduction. |
| 104 | + classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_INTRO_STRING); |
| 105 | + // TODO(summerji): Add sample code here. |
| 106 | + |
| 107 | + // API surface description. |
| 108 | + classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_CLOSE_STRING); |
| 109 | + classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_SURFACE_SUMMARY_STRING); |
| 110 | + classHeaderJavadocBuilder.addOrderedList(SERVICE_DESCRIPTION_SURFACE_DESCRIPTION); |
| 111 | + classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_SURFACE_CODA_STRING); |
| 112 | + |
| 113 | + // Formatting resource names. |
| 114 | + classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_RESOURCE_NAMES_FORMATTING_STRING); |
| 115 | + |
| 116 | + // Customization examples. |
| 117 | + classHeaderJavadocBuilder.addParagraph( |
| 118 | + String.format( |
| 119 | + SERVICE_DESCRIPTION_CUSTOMIZE_SUMMARY_PATTERN, |
| 120 | + String.format("%sSettings", JavaStyle.toUpperCamelCase(service.name())))); |
| 121 | + classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_CREDENTIALS_SUMMARY_STRING); |
| 122 | + // TODO(summerji): Add credentials' customization sample code here. |
| 123 | + classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_ENDPOINT_SUMMARY_STRING); |
| 124 | + // TODO(summerji): Add endpoint customization sample code here. |
| 125 | + |
| 126 | + return Arrays.asList( |
| 127 | + CommentComposer.AUTO_GENERATED_CLASS_COMMENT, |
| 128 | + CommentStatement.withComment(classHeaderJavadocBuilder.build())); |
| 129 | + } |
| 130 | + |
48 | 131 | static CommentStatement createCreateMethodStubArgComment(TypeNode settingsType) { |
49 | 132 | return toSimpleComment( |
50 | 133 | String.format(CREATE_METHOD_STUB_ARG_PATTERN, settingsType.reference().name())); |
|