Search in sources :

Example 1 with CommentStatement

use of com.google.api.generator.engine.ast.CommentStatement in project gapic-generator-java by googleapis.

the class AbstractServiceStubSettingsClassComposer method generate.

@Override
public GapicClass generate(GapicContext context, Service service) {
    // TODO(miraleung): Robustify this against a null serviceConfig.
    GapicServiceConfig serviceConfig = context.serviceConfig();
    Map<String, Message> messageTypes = context.messages();
    String pakkage = String.format("%s.stub", service.pakkage());
    TypeStore typeStore = createDynamicTypes(service, pakkage);
    List<Sample> samples = new ArrayList<>();
    Set<String> deprecatedSettingVarNames = new HashSet<>();
    Map<String, VariableExpr> methodSettingsMemberVarExprs = createMethodSettingsClassMemberVarExprs(service, serviceConfig, typeStore, /* isNestedClass= */
    false, deprecatedSettingVarNames);
    String className = ClassNames.getServiceStubSettingsClassName(service);
    List<CommentStatement> classHeaderComments = createClassHeaderComments(service, typeStore.get(className), samples);
    ClassDefinition classDef = ClassDefinition.builder().setPackageString(pakkage).setHeaderCommentStatements(classHeaderComments).setAnnotations(createClassAnnotations(service)).setScope(ScopeNode.PUBLIC).setName(className).setExtendsType(createExtendsType(service, typeStore)).setStatements(createClassStatements(service, serviceConfig, methodSettingsMemberVarExprs, messageTypes, typeStore)).setMethods(createClassMethods(service, methodSettingsMemberVarExprs, deprecatedSettingVarNames, typeStore)).setNestedClasses(Arrays.asList(createNestedBuilderClass(service, serviceConfig, typeStore))).build();
    return GapicClass.create(GapicClass.Kind.STUB, classDef, SampleComposerUtil.handleDuplicateSamples(samples));
}
Also used : Message(com.google.api.generator.gapic.model.Message) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) Sample(com.google.api.generator.gapic.model.Sample) ArrayList(java.util.ArrayList) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) HashSet(java.util.HashSet)

Example 2 with CommentStatement

use of com.google.api.generator.engine.ast.CommentStatement in project gapic-generator-java by googleapis.

the class AbstractServiceSettingsClassComposer method generate.

@Override
public GapicClass generate(GapicContext context, Service service) {
    String pakkage = service.pakkage();
    TypeStore typeStore = createDynamicTypes(service);
    String className = ClassNames.getServiceSettingsClassName(service);
    GapicClass.Kind kind = Kind.MAIN;
    List<Sample> samples = new ArrayList<>();
    List<CommentStatement> classHeaderComments = createClassHeaderComments(service, typeStore.get(className), samples);
    ClassDefinition classDef = ClassDefinition.builder().setPackageString(pakkage).setHeaderCommentStatements(classHeaderComments).setAnnotations(createClassAnnotations(service)).setScope(ScopeNode.PUBLIC).setName(className).setExtendsType(TypeNode.withReference(FIXED_TYPESTORE.get("ClientSettings").reference().copyAndSetGenerics(Arrays.asList(typeStore.get(ClassNames.getServiceSettingsClassName(service)).reference())))).setMethods(createClassMethods(service, typeStore)).setNestedClasses(Arrays.asList(createNestedBuilderClass(service, typeStore))).build();
    return GapicClass.create(kind, classDef, SampleComposerUtil.handleDuplicateSamples(samples));
}
Also used : CommentStatement(com.google.api.generator.engine.ast.CommentStatement) Sample(com.google.api.generator.gapic.model.Sample) GapicClass(com.google.api.generator.gapic.model.GapicClass) ArrayList(java.util.ArrayList) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) Kind(com.google.api.generator.gapic.model.GapicClass.Kind) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition)

Example 3 with CommentStatement

use of com.google.api.generator.engine.ast.CommentStatement in project gapic-generator-java by googleapis.

the class ServiceClientCommentComposer method createRpcMethodHeaderComment.

public static List<CommentStatement> createRpcMethodHeaderComment(Method method, List<MethodArgument> methodArguments, Optional<String> sampleCodeOpt) {
    JavaDocComment.Builder methodJavadocBuilder = JavaDocComment.builder();
    if (method.hasDescription()) {
        methodJavadocBuilder = processProtobufComment(method.description(), methodJavadocBuilder, null);
    }
    if (sampleCodeOpt.isPresent()) {
        methodJavadocBuilder.addParagraph(METHOD_DESCRIPTION_SAMPLE_CODE_SUMMARY_STRING);
        methodJavadocBuilder.addSampleCode(sampleCodeOpt.get());
    }
    if (methodArguments.isEmpty()) {
        methodJavadocBuilder.addParam("request", "The request object containing all of the parameters for the API call.");
    } else {
        for (MethodArgument argument : methodArguments) {
            // TODO(miraleung): Remove the newline replacement when we support CommonMark.
            String description = argument.field().hasDescription() ? argument.field().description() : EMPTY_STRING;
            methodJavadocBuilder.addParam(JavaStyle.toLowerCamelCase(argument.name()), description);
        }
    }
    methodJavadocBuilder.setThrows(API_EXCEPTION_TYPE_NAME, EXCEPTION_CONDITION);
    if (method.isDeprecated()) {
        methodJavadocBuilder.setDeprecated(CommentComposer.DEPRECATED_METHOD_STRING);
    }
    List<CommentStatement> comments = new ArrayList<>();
    comments.add(CommentComposer.AUTO_GENERATED_METHOD_COMMENT);
    if (!methodJavadocBuilder.emptyComments()) {
        comments.add(CommentStatement.withComment(methodJavadocBuilder.build()));
    }
    return comments;
}
Also used : CommentStatement(com.google.api.generator.engine.ast.CommentStatement) JavaDocComment(com.google.api.generator.engine.ast.JavaDocComment) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) ArrayList(java.util.ArrayList)

Example 4 with CommentStatement

use of com.google.api.generator.engine.ast.CommentStatement in project gapic-generator-java by googleapis.

the class ClientLibraryPackageInfoComposer method createPackageInfoJavadoc.

private static CommentStatement createPackageInfoJavadoc(GapicContext context) {
    JavaDocComment.Builder javaDocCommentBuilder = JavaDocComment.builder();
    if (context.hasServiceYamlProto() && !Strings.isNullOrEmpty(context.serviceYamlProto().getTitle())) {
        javaDocCommentBuilder = javaDocCommentBuilder.addComment(String.format(PACKAGE_INFO_TITLE_PATTERN, context.serviceYamlProto().getTitle()));
    }
    javaDocCommentBuilder = javaDocCommentBuilder.addParagraph(PACKAGE_INFO_DESCRIPTION);
    for (Service service : context.services()) {
        String javaClientName = ClassNames.getServiceClientClassName(service);
        javaDocCommentBuilder = javaDocCommentBuilder.addParagraph(String.format("%s %s %s", DIVIDER, javaClientName, DIVIDER));
        // TODO(miraleung): Replace this with a comment converter when we support CommonMark.
        if (service.hasDescription()) {
            String[] descriptionParagraphs = service.description().split("\\n\\n");
            for (int i = 0; i < descriptionParagraphs.length; i++) {
                boolean startsWithItemizedList = descriptionParagraphs[i].startsWith(" * ");
                // Split by listed items, then join newlines.
                List<String> listItems = Stream.of(descriptionParagraphs[i].split("\\n \\*")).map(s -> s.replace("\n", "")).collect(Collectors.toList());
                if (startsWithItemizedList) {
                    // Remove the first asterisk.
                    listItems.set(0, listItems.get(0).substring(2));
                }
                if (!startsWithItemizedList) {
                    if (i == 0) {
                        javaDocCommentBuilder = javaDocCommentBuilder.addParagraph(String.format(SERVICE_DESCRIPTION_HEADER_PATTERN, listItems.get(0)));
                    } else {
                        javaDocCommentBuilder = javaDocCommentBuilder.addParagraph(listItems.get(0));
                    }
                }
                if (listItems.size() > 1 || startsWithItemizedList) {
                    javaDocCommentBuilder = javaDocCommentBuilder.addUnorderedList(listItems.subList(startsWithItemizedList ? 0 : 1, listItems.size()));
                }
            }
        }
        javaDocCommentBuilder = javaDocCommentBuilder.addParagraph(String.format(SAMPLE_CODE_HEADER_PATTERN, javaClientName));
        TypeNode clientType = TypeNode.withReference(VaporReference.builder().setPakkage(service.pakkage()).setName(ClassNames.getServiceClientClassName(service)).build());
        Sample packageInfoSampleCode = ServiceClientHeaderSampleComposer.composeClassHeaderSample(service, clientType, context.resourceNames(), context.messages());
        javaDocCommentBuilder.addSampleCode(SampleCodeWriter.writeInlineSample(packageInfoSampleCode.body()));
    }
    return CommentStatement.withComment(javaDocCommentBuilder.build());
}
Also used : ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) TypeNode(com.google.api.generator.engine.ast.TypeNode) ServiceClientHeaderSampleComposer(com.google.api.generator.gapic.composer.samplecode.ServiceClientHeaderSampleComposer) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) JavaDocComment(com.google.api.generator.engine.ast.JavaDocComment) Sample(com.google.api.generator.gapic.model.Sample) Collectors(java.util.stream.Collectors) Strings(com.google.common.base.Strings) ClassNames(com.google.api.generator.gapic.composer.utils.ClassNames) Service(com.google.api.generator.gapic.model.Service) List(java.util.List) Generated(javax.annotation.Generated) Stream(java.util.stream.Stream) SampleCodeWriter(com.google.api.generator.gapic.composer.samplecode.SampleCodeWriter) GapicPackageInfo(com.google.api.generator.gapic.model.GapicPackageInfo) PackageInfoDefinition(com.google.api.generator.engine.ast.PackageInfoDefinition) Preconditions(com.google.common.base.Preconditions) GapicContext(com.google.api.generator.gapic.model.GapicContext) JavaDocComment(com.google.api.generator.engine.ast.JavaDocComment) Sample(com.google.api.generator.gapic.model.Sample) Service(com.google.api.generator.gapic.model.Service) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 5 with CommentStatement

use of com.google.api.generator.engine.ast.CommentStatement in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeLineCommentStatement_basic.

@Test
public void writeLineCommentStatement_basic() {
    String content = "this is a test comment";
    LineComment lineComment = LineComment.builder().setComment(content).build();
    CommentStatement commentStatement = CommentStatement.withComment(lineComment);
    String expected = "// this is a test comment\n";
    commentStatement.accept(writerVisitor);
    assertEquals(expected, writerVisitor.write());
}
Also used : CommentStatement(com.google.api.generator.engine.ast.CommentStatement) LineComment(com.google.api.generator.engine.ast.LineComment) Test(org.junit.Test)

Aggregations

CommentStatement (com.google.api.generator.engine.ast.CommentStatement)9 Sample (com.google.api.generator.gapic.model.Sample)4 ArrayList (java.util.ArrayList)4 ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)3 JavaDocComment (com.google.api.generator.engine.ast.JavaDocComment)3 Test (org.junit.Test)3 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)2 LineComment (com.google.api.generator.engine.ast.LineComment)2 TypeNode (com.google.api.generator.engine.ast.TypeNode)2 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)2 TypeStore (com.google.api.generator.gapic.composer.store.TypeStore)2 Message (com.google.api.generator.gapic.model.Message)2 Preconditions (com.google.common.base.Preconditions)2 Strings (com.google.common.base.Strings)2 ApiFuture (com.google.api.core.ApiFuture)1 OperationFuture (com.google.api.gax.longrunning.OperationFuture)1 ApiStreamObserver (com.google.api.gax.rpc.ApiStreamObserver)1 BidiStream (com.google.api.gax.rpc.BidiStream)1 ServerStream (com.google.api.gax.rpc.ServerStream)1 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)1