fix #123 allow to specify extra annotations on repositories/methods
[yosql.git] / yosql-codegen / src / main / java / wtf / metio / yosql / codegen / files / DefaultMethodNameValidator.java
blob294ddc16f1b28f68c5515e8d110d9700a4d94758
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 ch.qos.cal10n.IMessageConveyor;
11 import wtf.metio.yosql.codegen.lifecycle.ValidationErrors;
12 import wtf.metio.yosql.codegen.orchestration.ExecutionErrors;
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 java.nio.file.Path;
18 import java.util.List;
20 public final class DefaultMethodNameValidator implements MethodNameValidator {
22 private final RepositoriesConfiguration repositories;
23 private final ExecutionErrors errors;
24 private final IMessageConveyor messages;
26 public DefaultMethodNameValidator(
27 final RepositoriesConfiguration repositories,
28 final ExecutionErrors errors,
29 final IMessageConveyor messages) {
30 this.repositories = repositories;
31 this.errors = errors;
32 this.messages = messages;
35 @Override
36 public void validateNames(final SqlConfiguration configuration, final Path source) {
37 if (repositories.validateMethodNamePrefixes()) {
38 configuration.type()
39 .map(this::allowedPrefixes)
40 .flatMap(prefixes -> configuration.name()
41 .filter(name -> notStartsWith(name, prefixes)))
42 .ifPresent(name -> invalidPrefix(source, name));
46 private List<String> allowedPrefixes(final SqlStatementType type) {
47 return switch (type) {
48 case READING -> repositories.allowedReadPrefixes();
49 case WRITING -> repositories.allowedWritePrefixes();
50 case CALLING -> repositories.allowedCallPrefixes();
54 private static boolean notStartsWith(final String fileName, final List<String> prefixes) {
55 return prefixes == null || prefixes.stream().noneMatch(fileName::startsWith);
58 private void invalidPrefix(final Path source, final String name) {
59 errors.illegalArgument(messages.getMessage(ValidationErrors.INVALID_PREFIX, source, name));