Skip to content

Commit 053bb00

Browse files
authored
Merge pull request #26 from SentryMan/more-generation
Generation part 2
2 parents d3418c3 + 77df820 commit 053bb00

File tree

18 files changed

+343
-106
lines changed

18 files changed

+343
-106
lines changed

blackbox-test/pom.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@
3434
<dependency>
3535
<groupId>io.avaje</groupId>
3636
<artifactId>avaje-validator</artifactId>
37-
<version>0.1-SNAPSHOT</version>
37+
<version>${project.version}</version>
3838
</dependency>
3939

40-
<!-- <dependency>-->
41-
<!-- <groupId>io.avaje</groupId>-->
42-
<!-- <artifactId>avaje-validator-generator</artifactId>-->
43-
<!-- <version>0.1-SNAPSHOT</version>-->
44-
<!-- <scope>provided</scope>-->
45-
<!-- </dependency>-->
40+
<dependency>
41+
<groupId>io.avaje</groupId>
42+
<artifactId>avaje-validator-generator</artifactId>
43+
<version>${project.version}</version>
44+
<scope>provided</scope>
45+
</dependency>
4646

4747
<!-- test dependencies -->
4848

validator-constraints/pom.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
<artifactId>avaje-validator-parent</artifactId>
99
<version>0.1-SNAPSHOT</version>
1010
</parent>
11-
12-
<groupId>org.example</groupId>
1311
<artifactId>validator-constraints</artifactId>
1412

1513
</project>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package io.avaje.validation.constraints;
2+
3+
import static java.lang.annotation.ElementType.FIELD;
4+
import static java.lang.annotation.ElementType.METHOD;
5+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
6+
7+
import java.lang.annotation.Documented;
8+
import java.lang.annotation.Repeatable;
9+
import java.lang.annotation.Retention;
10+
import java.lang.annotation.Target;
11+
12+
import io.avaje.validation.constraints.Pattern.List;
13+
14+
/**
15+
* The annotated {@code CharSequence} must match the specified regular expression. The regular
16+
* expression follows the Java regular expression conventions see {@link java.util.regex.Pattern}.
17+
*
18+
* <p>Accepts {@code CharSequence}. {@code null} elements are considered valid.
19+
*
20+
*/
21+
@Target({METHOD, FIELD})
22+
@Retention(RUNTIME)
23+
@Repeatable(List.class)
24+
@Documented
25+
public @interface Pattern {
26+
27+
/** @return the regular expression to match */
28+
String regexp();
29+
30+
/** @return array of {@code Flag}s considered when resolving the regular expression */
31+
Flag[] flags() default {};
32+
33+
/** @return the error message template */
34+
String message() default "{avaje.validation.constraints.Pattern.message}";
35+
36+
/** @return the groups the constraint belongs to */
37+
Class<?>[] groups() default {};
38+
39+
/** Possible Regexp flags. */
40+
public enum Flag {
41+
42+
/**
43+
* Enables Unix lines mode.
44+
*
45+
* @see java.util.regex.Pattern#UNIX_LINES
46+
*/
47+
UNIX_LINES(java.util.regex.Pattern.UNIX_LINES),
48+
49+
/**
50+
* Enables case-insensitive matching.
51+
*
52+
* @see java.util.regex.Pattern#CASE_INSENSITIVE
53+
*/
54+
CASE_INSENSITIVE(java.util.regex.Pattern.CASE_INSENSITIVE),
55+
56+
/**
57+
* Permits whitespace and comments in pattern.
58+
*
59+
* @see java.util.regex.Pattern#COMMENTS
60+
*/
61+
COMMENTS(java.util.regex.Pattern.COMMENTS),
62+
63+
/**
64+
* Enables multiline mode.
65+
*
66+
* @see java.util.regex.Pattern#MULTILINE
67+
*/
68+
MULTILINE(java.util.regex.Pattern.MULTILINE),
69+
70+
/**
71+
* Enables dotall mode.
72+
*
73+
* @see java.util.regex.Pattern#DOTALL
74+
*/
75+
DOTALL(java.util.regex.Pattern.DOTALL),
76+
77+
/**
78+
* Enables Unicode-aware case folding.
79+
*
80+
* @see java.util.regex.Pattern#UNICODE_CASE
81+
*/
82+
UNICODE_CASE(java.util.regex.Pattern.UNICODE_CASE),
83+
84+
/**
85+
* Enables canonical equivalence.
86+
*
87+
* @see java.util.regex.Pattern#CANON_EQ
88+
*/
89+
CANON_EQ(java.util.regex.Pattern.CANON_EQ);
90+
91+
// JDK flag value
92+
private final int value;
93+
94+
Flag(int value) {
95+
this.value = value;
96+
}
97+
98+
/** @return flag value as defined in {@link java.util.regex.Pattern} */
99+
public int getValue() {
100+
return value;
101+
}
102+
}
103+
104+
/**
105+
* Defines several {@link Pattern} annotations on the same element.
106+
*
107+
* @see Pattern
108+
*/
109+
@Target({METHOD, FIELD})
110+
@Retention(RUNTIME)
111+
@Documented
112+
@interface List {
113+
114+
Pattern[] value();
115+
}
116+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module io.avaje.validation.contraints {
2+
3+
exports io.avaje.validation.constraints;
4+
5+
}

validator-generator/pom.xml

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
35
<modelVersion>4.0.0</modelVersion>
46
<parent>
57
<groupId>io.avaje</groupId>
@@ -11,7 +13,7 @@
1113
<name>validator generator</name>
1214
<description>annotation processor generating json adapters</description>
1315
<properties>
14-
<avaje.prisms.version>1.8</avaje.prisms.version>
16+
<avaje.prisms.version>1.9</avaje.prisms.version>
1517
</properties>
1618

1719
<dependencies>
@@ -21,6 +23,11 @@
2123
<artifactId>avaje-validator</artifactId>
2224
<version>${project.version}</version>
2325
</dependency>
26+
<dependency>
27+
<groupId>io.avaje</groupId>
28+
<artifactId>validator-constraints</artifactId>
29+
<version>${project.version}</version>
30+
</dependency>
2431

2532
<dependency>
2633
<groupId>io.avaje</groupId>
@@ -29,21 +36,35 @@
2936
<optional>true</optional>
3037
<scope>provided</scope>
3138
</dependency>
32-
33-
<dependency>
34-
<groupId>jakarta.validation</groupId>
35-
<artifactId>jakarta.validation-api</artifactId>
36-
<version>3.0.2</version>
39+
<dependency>
40+
<groupId>io.avaje</groupId>
41+
<artifactId>avaje-validator</artifactId>
42+
<version>${project.version}</version>
3743
<scope>provided</scope>
3844
<optional>true</optional>
39-
</dependency>
40-
<dependency>
41-
<groupId>javax.validation</groupId>
42-
<artifactId>validation-api</artifactId>
43-
<version>2.0.1.Final</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>jakarta.validation</groupId>
48+
<artifactId>jakarta.validation-api</artifactId>
49+
<version>3.0.2</version>
4450
<scope>provided</scope>
4551
<optional>true</optional>
46-
</dependency>
52+
</dependency>
53+
<dependency>
54+
<groupId>javax.validation</groupId>
55+
<artifactId>validation-api</artifactId>
56+
<version>2.0.1.Final</version>
57+
<scope>provided</scope>
58+
<optional>true</optional>
59+
</dependency>
60+
<dependency>
61+
<groupId>io.avaje</groupId>
62+
<artifactId>avaje-http-api</artifactId>
63+
<version>1.39</version>
64+
<optional>true</optional>
65+
<scope>provided</scope>
66+
</dependency>
67+
4768

4869
<!-- test dependencies -->
4970
<dependency>
@@ -52,7 +73,7 @@
5273
<version>1.1</version>
5374
<scope>test</scope>
5475
</dependency>
55-
<dependency>
76+
<dependency>
5677
<groupId>io.avaje</groupId>
5778
<artifactId>avaje-lang</artifactId>
5879
<version>1.1</version>

validator-generator/src/main/java/io/avaje/validation/generator/AnnotationUtil.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.avaje.validation.generator;
22

3+
import static java.util.stream.Collectors.joining;
4+
35
import java.util.List;
46

57
import javax.lang.model.element.AnnotationMirror;
@@ -12,7 +14,12 @@ private AnnotationUtil() {}
1214
public static String getAnnotationAttributMap(AnnotationMirror annotationMirror) {
1315
final StringBuilder sb = new StringBuilder("Map.of(");
1416
boolean first = true;
17+
final var patternOp = PatternPrism.isInstance(annotationMirror);
1518

19+
if (patternOp.isPresent()) {
20+
patternOp.ifPresent(p -> pattern(sb, p));
21+
return sb.toString();
22+
}
1623
for (final var entry : annotationMirror.getElementValues().entrySet()) {
1724
if (!first) {
1825
sb.append(", ");
@@ -25,6 +32,21 @@ public static String getAnnotationAttributMap(AnnotationMirror annotationMirror)
2532
return sb.toString();
2633
}
2734

35+
private static void pattern(StringBuilder sb, PatternPrism prism) {
36+
if (prism.regexp() != null) {
37+
sb.append("\"regexp\",\"" + prism.regexp() + "\"");
38+
}
39+
40+
if (prism.message() != null) {
41+
sb.append(", \"message\",\"" + prism.message() + "\"");
42+
}
43+
if (!prism.flags().isEmpty()) {
44+
sb.append(", \"flags\",List.of(" + prism.flags().stream().collect(joining(", ")) + ")");
45+
}
46+
47+
sb.append(")");
48+
}
49+
2850
private static void writeVal(final StringBuilder sb, final AnnotationValue annotationValue) {
2951
final var value = annotationValue.getValue();
3052
// handle array values

0 commit comments

Comments
 (0)