fix #123 allow to specify extra annotations on repositories/methods
[yosql.git] / yosql-codegen / src / main / java / wtf / metio / yosql / codegen / blocks / DefaultJavadoc.java
blob25892b1ed4218cd32cef972532713e9a6c42dce7
1 /*
2 * This file is part of yosql. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at https://creativecommons.org/publicdomain/zero/1.0/. No part of yosql,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
5 * in the LICENSE file.
6 */
8 package wtf.metio.yosql.codegen.blocks;
10 import ch.qos.cal10n.IMessageConveyor;
11 import com.squareup.javapoet.CodeBlock;
12 import de.xn__ho_hia.javapoet.TypeGuesser;
13 import wtf.metio.yosql.internals.jdk.Strings;
14 import wtf.metio.yosql.models.configuration.ResultRowConverter;
15 import wtf.metio.yosql.models.immutables.FilesConfiguration;
16 import wtf.metio.yosql.models.immutables.SqlConfiguration;
17 import wtf.metio.yosql.models.immutables.SqlStatement;
19 import java.util.List;
20 import java.util.Optional;
22 import static java.util.function.Predicate.not;
24 public final class DefaultJavadoc implements Javadoc {
26 private final FilesConfiguration files;
27 private final IMessageConveyor messages;
29 public DefaultJavadoc(final FilesConfiguration files, final IMessageConveyor messages) {
30 this.files = files;
31 this.messages = messages;
34 @Override
35 public CodeBlock repositoryJavadoc(final List<SqlStatement> statements) {
36 final var builder = CodeBlock.builder()
37 .add(messages.getMessage(Javadocs.USED_FILES))
38 .add(messages.getMessage(Javadocs.LIST_START));
39 final var input = files.inputBaseDirectory();
40 statements.stream()
41 .map(SqlStatement::getSourcePath)
42 .distinct()
43 .map(input::relativize)
44 .forEach(path -> builder.add(messages.getMessage(Javadocs.LIST_ITEM), path));
45 builder.add(messages.getMessage(Javadocs.LIST_END));
46 return builder.build();
49 @Override
50 public CodeBlock fieldJavaDoc(final SqlStatement statement) {
51 final var input = files.inputBaseDirectory();
52 final var builder = CodeBlock.builder()
53 .add(messages.getMessage(Javadocs.USED_FILE))
54 .add(messages.getMessage(Javadocs.LIST_START))
55 .add(messages.getMessage(Javadocs.LIST_ITEM), input.relativize(statement.getSourcePath()));
56 builder.add(messages.getMessage(Javadocs.LIST_END));
57 return builder.build();
60 @Override
61 public CodeBlock methodJavadoc(final List<SqlStatement> statements, final String configuration) {
62 final var builder = CodeBlock.builder();
63 statements.stream()
64 .map(SqlStatement::getConfiguration)
65 .flatMap(config -> config.description().stream())
66 .filter(not(Strings::isBlank))
67 .forEach(description -> builder.add(messages.getMessage(Javadocs.DESCRIPTION), description));
68 if (statements.size() > 1) {
69 builder.add(messages.getMessage(Javadocs.EXECUTED_STATEMENTS));
70 } else {
71 builder.add(messages.getMessage(Javadocs.EXECUTED_STATEMENT));
73 for (final var statement : statements) {
74 statement.getConfiguration().vendor()
75 .filter(not(Strings::isBlank))
76 .ifPresentOrElse(vendor ->
77 builder.add(messages.getMessage(Javadocs.VENDOR), vendor),
78 () -> {
79 if (statements.size() > 1) {
80 builder.add(messages.getMessage(Javadocs.FALLBACK));
82 });
83 builder.add(messages.getMessage(Javadocs.STATEMENT), statement.getRawStatement());
85 builder.add(messages.getMessage(Javadocs.USED_FILES_METHOD))
86 .add(messages.getMessage(Javadocs.LIST_START));
87 final var input = files.inputBaseDirectory();
88 statements.stream()
89 .map(SqlStatement::getSourcePath)
90 .distinct()
91 .map(input::relativize)
92 .forEach(path -> builder.add(messages.getMessage(Javadocs.LIST_ITEM), path));
93 builder.add(messages.getMessage(Javadocs.LIST_END));
94 builder.add(messages.getMessage(Javadocs.DISABLE_WITH), configuration);
95 statements.stream()
96 .map(SqlStatement::getConfiguration)
97 .map(SqlConfiguration::resultRowConverter)
98 .flatMap(Optional::stream)
99 .map(ResultRowConverter::resultType)
100 .flatMap(Optional::stream)
101 .filter(type -> !type.startsWith("java"))
102 .map(type -> type.substring(0, type.contains("<") ? type.indexOf("<") : type.length()))
103 .distinct()
104 .map(TypeGuesser::guessTypeName)
105 .filter(type -> !type.isPrimitive())
106 .filter(type -> !type.isBoxedPrimitive())
107 .forEach(type -> builder.add(messages.getMessage(Javadocs.SEE), type));
108 return builder.build();