fix #123 allow to specify extra annotations on repositories/methods
[yosql.git] / yosql-codegen / src / main / java / wtf / metio / yosql / codegen / files / DefaultMethodNameConfigurer.java
blob4f7e3ebd7c25009de7b4dba4d059d5e848d90a83
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.files;
10 import org.slf4j.cal10n.LocLogger;
11 import wtf.metio.yosql.codegen.lifecycle.SqlConfigurationLifecycle;
12 import wtf.metio.yosql.internals.jdk.Strings;
13 import wtf.metio.yosql.models.configuration.SqlStatementType;
14 import wtf.metio.yosql.models.immutables.RepositoriesConfiguration;
15 import wtf.metio.yosql.models.immutables.SqlConfiguration;
17 import javax.lang.model.SourceVersion;
18 import java.util.Optional;
20 import static java.util.function.Predicate.not;
22 /**
23 * Default implementation of a {@link MethodNameConfigurer}.
25 public final class DefaultMethodNameConfigurer implements MethodNameConfigurer {
27 private final LocLogger logger;
28 private final RepositoriesConfiguration repositories;
30 /**
31 * @param logger The logger to use.
32 * @param repositories The repository configuration to use.
34 public DefaultMethodNameConfigurer(final LocLogger logger, final RepositoriesConfiguration repositories) {
35 this.logger = logger;
36 this.repositories = repositories;
39 @Override
40 public SqlConfiguration configureNames(
41 final SqlConfiguration configuration,
42 final String fileName,
43 final int statementInFile) {
44 var adapted = configuration;
45 adapted = baseName(adapted, fileName, statementInFile);
46 adapted = affixes(adapted);
47 return adapted;
50 // visible for testing
51 SqlConfiguration baseName(final SqlConfiguration configuration, final String fileName, final int statementInFile) {
52 return SqlConfiguration.copyOf(configuration)
53 .withName(configuration.name()
54 .filter(SourceVersion::isName)
55 .or(() -> configuration.type()
56 .map(type -> validName(type, fileName))
57 .map(name -> calculateName(name, statementInFile)))
58 .or(() -> Optional.of(fileName)
59 .filter(SourceVersion::isName)
60 .map(name -> calculateName(name, statementInFile))));
63 private String validName(final SqlStatementType type, final String fileName) {
64 return SourceVersion.isName(fileName) ? fileName : generateName(type);
67 private static String calculateName(final String name, final int statementInFile) {
68 return statementInFile > 1 ? name + statementInFile : name;
71 private String generateName(final SqlStatementType type) {
72 final var prefix = switch (type) {
73 case READING -> repositories.allowedReadPrefixes().get(0);
74 case WRITING -> repositories.allowedWritePrefixes().get(0);
75 case CALLING -> repositories.allowedCallPrefixes().get(0);
77 return prefix + "NameWasChanged";
80 // visible for testing
81 SqlConfiguration affixes(final SqlConfiguration configuration) {
82 var adapted = configuration;
83 adapted = batchNamePrefix(adapted);
84 adapted = batchNameSuffix(adapted);
85 adapted = standardNamePrefix(adapted);
86 adapted = standardNameSuffix(adapted);
87 return adapted;
90 // visible for testing
91 SqlConfiguration batchNamePrefix(final SqlConfiguration configuration) {
92 return configuration.batchPrefix()
93 .filter(not(Strings::isBlank))
94 .map(prefix -> configuration)
95 .orElseGet(() -> {
96 logger.debug(SqlConfigurationLifecycle.BATCH_PREFIX_NAME_CHANGED, repositories.batchPrefix());
97 return SqlConfiguration.copyOf(configuration).withBatchPrefix(repositories.batchPrefix());
98 });
101 // visible for testing
102 SqlConfiguration batchNameSuffix(final SqlConfiguration configuration) {
103 return configuration.batchSuffix()
104 .filter(not(Strings::isBlank))
105 .map(suffix -> configuration)
106 .orElseGet(() -> {
107 logger.debug(SqlConfigurationLifecycle.BATCH_SUFFIX_NAME_CHANGED, repositories.batchSuffix());
108 return SqlConfiguration.copyOf(configuration).withBatchSuffix(repositories.batchSuffix());
112 // visible for testing
113 SqlConfiguration standardNamePrefix(final SqlConfiguration configuration) {
114 return configuration.standardPrefix()
115 .filter(not(Strings::isBlank))
116 .map(prefix -> configuration)
117 .orElseGet(() -> {
118 logger.debug(SqlConfigurationLifecycle.STANDARD_PREFIX_NAME_CHANGED, repositories.standardPrefix());
119 return SqlConfiguration.copyOf(configuration).withStandardPrefix(repositories.standardPrefix());
123 // visible for testing
124 SqlConfiguration standardNameSuffix(final SqlConfiguration configuration) {
125 return configuration.standardSuffix()
126 .filter(not(Strings::isBlank))
127 .map(suffix -> configuration)
128 .orElseGet(() -> {
129 logger.debug(SqlConfigurationLifecycle.STANDARD_SUFFIX_NAME_CHANGED, repositories.standardSuffix());
130 return SqlConfiguration.copyOf(configuration).withStandardSuffix(repositories.standardSuffix());