Skip to content

Commit e0a8385

Browse files
committed
Regex doco and builder
1 parent e370d2a commit e0a8385

File tree

7 files changed

+114
-52
lines changed

7 files changed

+114
-52
lines changed

readme.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,28 @@ And example query might look like:
162162
```
163163

164164

165+
## Regex Scalars
166+
167+
The RegexScalar has a builder where you provide one or more regex patterns that control the acceptable values
168+
for a new scalar.
169+
170+
You name the scalar and it provides an implementation.
171+
172+
For example, imagine a `phoneNumber` scalar like this :
173+
174+
```java
175+
176+
RegexScalar phoneNumberScalar = ExtendedScalars.newRegexScalar("phoneNumber")
177+
.addPattern(Pattern.compile("\\([0-9]*\\)[0-9]*"))
178+
.build()
179+
180+
```
181+
182+
165183
## Other Scalars
166184

167185
* `Url`
168186
* An url scalar that accepts string values like `https://www.w3.org/Addressing/URL/url-spec.txt` and produces
169187
`java.net.URL` objects at runtime
188+
189+

src/main/java/graphql/scalars/ExtendedScalars.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import graphql.scalars.numeric.PositiveIntScalar;
1515
import graphql.scalars.object.JsonScalar;
1616
import graphql.scalars.object.ObjectScalar;
17+
import graphql.scalars.regex.RegexScalar;
1718
import graphql.scalars.url.UrlScalar;
1819
import graphql.schema.GraphQLScalarType;
1920

@@ -158,4 +159,18 @@ public class ExtendedScalars {
158159
* @see graphql.Scalars#GraphQLFloat
159160
*/
160161
public static GraphQLScalarType NonNegativeFloat = new NonNegativeFloatScalar();
162+
163+
164+
/**
165+
* A builder of a scalar that uses one or more regular expression {@link java.util.regex.Pattern}s to control
166+
* the acceptable values for that scalar.
167+
* <p>
168+
* The scalar converts any passed in objects to Strings first and them matches it against the provided
169+
* scalars to ensure its an acceptable value.
170+
*
171+
* @return a builder of a regex scalar
172+
*/
173+
public static RegexScalar.Builder newRegexScalar(String name) {
174+
return new RegexScalar.Builder().name(name);
175+
}
161176
}

src/main/java/graphql/scalars/object/JsonScalar.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
/**
66
* A synonym class for {@link graphql.scalars.object.ObjectScalar}
7+
*
8+
* Access this via {@link graphql.scalars.ExtendedScalars#Json}
79
*/
810
@Internal
911
public class JsonScalar extends ObjectScalar {

src/main/java/graphql/scalars/object/ObjectScalar.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727

2828
import static graphql.scalars.util.Kit.typeName;
2929

30+
/**
31+
* Access this via {@link graphql.scalars.ExtendedScalars#Object}
32+
*/
3033
@Internal
3134
public class ObjectScalar extends GraphQLScalarType {
3235

src/main/java/graphql/scalars/regex/RegexScalar.java

Lines changed: 67 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package graphql.scalars.regex;
22

33
import graphql.Assert;
4+
import graphql.PublicApi;
45
import graphql.language.StringValue;
56
import graphql.schema.Coercing;
67
import graphql.schema.CoercingParseLiteralException;
78
import graphql.schema.CoercingParseValueException;
89
import graphql.schema.CoercingSerializeException;
910
import graphql.schema.GraphQLScalarType;
1011

12+
import java.util.ArrayList;
13+
import java.util.Collections;
14+
import java.util.List;
1115
import java.util.function.Function;
1216
import java.util.regex.Matcher;
1317
import java.util.regex.Pattern;
@@ -18,67 +22,79 @@
1822
* This is really a scalar factory for creating new scalar String types that are based on a value matching
1923
* a regular expression.
2024
*/
25+
@PublicApi
2126
public class RegexScalar extends GraphQLScalarType {
2227

23-
private RegexScalar(String name, String description, Coercing coercing) {
24-
super(name, description, coercing);
25-
}
26-
2728
/**
28-
* Creates a new scalar that uses the specified {@link java.util.regex.Pattern} to control
29-
* the values that are allowed.
30-
*
31-
* @param name the name of the scalar
32-
* @param pattern the allowable values pattern
33-
*
34-
* @return a new regex scalar
29+
* A builder for {@link graphql.scalars.regex.RegexScalar}
3530
*/
36-
public static RegexScalar regexScalar(String name, Pattern pattern) {
37-
return regexScalarImpl(name, null, pattern);
38-
}
31+
public static class Builder {
32+
private String name;
33+
private String description;
34+
private List<Pattern> patterns = new ArrayList<>();
3935

40-
/**
41-
* Creates a new scalar that uses the specified {@link java.util.regex.Pattern} to control
42-
* the values that are allowed.
43-
*
44-
* @param name the name of the scalar
45-
* @param description the description of the scalar
46-
* @param pattern the allowable values pattern
47-
*
48-
* @return a new regex scalar
49-
*/
50-
public static RegexScalar regexScalar(String name, String description, Pattern pattern) {
51-
return regexScalarImpl(name, description, pattern);
52-
}
36+
/**
37+
* Sets the name of the regex scalar
38+
*
39+
* @param name the name of the regex scalar
40+
*
41+
* @return this builder
42+
*/
43+
public Builder name(String name) {
44+
this.name = name;
45+
return this;
46+
}
5347

54-
/**
55-
* Creates a new scalar that uses the specified {@link java.util.regex.Pattern}s to control
56-
* the values that are allowed.
57-
*
58-
* @param name the name of the scalar
59-
* @param patterns the allowable values patterns
60-
*
61-
* @return a new regex scalar
62-
*/
63-
public static RegexScalar regexScalar(String name, Pattern... patterns) {
64-
return regexScalarImpl(name, null, patterns);
48+
/**
49+
* Sets the name of the regex scalar
50+
*
51+
* @param description the description of the regex scalar
52+
*
53+
* @return this builder
54+
*/
55+
public Builder description(String description) {
56+
this.description = description;
57+
return this;
58+
}
59+
60+
/**
61+
* Adds a {@link java.util.regex.Pattern} that controls the acceptable value for this scalar
62+
*
63+
* @param pattern the regex pattern
64+
*
65+
* @return this builder
66+
*/
67+
public Builder addPattern(Pattern pattern) {
68+
this.patterns.add(pattern);
69+
return this;
70+
}
71+
72+
/**
73+
* Adds a {@link java.util.regex.Pattern} that controls the acceptable value for this scalar
74+
*
75+
* @param patterns one of more regex patterns
76+
*
77+
* @return this builder
78+
*/
79+
public Builder addPatterns(Pattern... patterns) {
80+
Collections.addAll(this.patterns, patterns);
81+
return this;
82+
}
83+
84+
/**
85+
* @return the built {@link graphql.scalars.regex.RegexScalar}
86+
*/
87+
public RegexScalar build() {
88+
Assert.assertNotNull(name);
89+
return regexScalarImpl(name, description, patterns);
90+
}
6591
}
6692

67-
/**
68-
* Creates a new scalar that uses the specified {@link java.util.regex.Pattern}s to control
69-
* the values that are allowed.
70-
*
71-
* @param name the name of the scalar
72-
* @param description the description of the scalar
73-
* @param patterns the allowable values patterns
74-
*
75-
* @return a new regex scalar
76-
*/
77-
public static RegexScalar regexScalar(String name, String description, Pattern... patterns) {
78-
return regexScalarImpl(name, description, patterns);
93+
private RegexScalar(String name, String description, Coercing coercing) {
94+
super(name, description, coercing);
7995
}
8096

81-
private static RegexScalar regexScalarImpl(String name, String description, Pattern... patterns) {
97+
private static RegexScalar regexScalarImpl(String name, String description, List<Pattern> patterns) {
8298
Assert.assertNotNull(patterns);
8399
return new RegexScalar(name, description, new Coercing<String, String>() {
84100
@Override

src/test/groovy/graphql/scalars/regex/RegexScalarTest.groovy

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package graphql.scalars.regex
22

33
import graphql.language.StringValue
4+
import graphql.scalars.ExtendedScalars
45
import graphql.schema.CoercingParseLiteralException
56
import graphql.schema.CoercingParseValueException
67
import graphql.schema.CoercingSerializeException
@@ -11,7 +12,9 @@ import java.util.regex.Pattern
1112

1213
class RegexScalarTest extends Specification {
1314

14-
def phoneNumberScalar = RegexScalar.regexScalar("phoneNumber", Pattern.compile("\\([0-9]*\\)[0-9]*"))
15+
RegexScalar phoneNumberScalar = ExtendedScalars.newRegexScalar("phoneNumber")
16+
.addPattern(Pattern.compile("\\([0-9]*\\)[0-9]*"))
17+
.build()
1518

1619
@Unroll
1720
def "basic regex parseValue"() {

src/test/groovy/graphql/scalars/url/UrlScalarTest.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class UrlScalarTest extends Specification {
2727
"http://www.graphql-java.com/" | new URL("http://www.graphql-java.com/")
2828
}
2929

30+
@Unroll
3031
def "test serialize bad inputs"() {
3132
when:
3233
coercing.serialize(input)
@@ -38,6 +39,7 @@ class UrlScalarTest extends Specification {
3839
"not/a/url" || CoercingSerializeException
3940
}
4041

42+
@Unroll
4143
def "test parseValue"() {
4244
when:
4345
def result = coercing.parseValue(input)
@@ -51,6 +53,7 @@ class UrlScalarTest extends Specification {
5153
"http://www.graphql-java.com/" | new URL("http://www.graphql-java.com/")
5254
}
5355

56+
@Unroll
5457
def "test parseValue bad inputs"() {
5558
when:
5659
coercing.parseValue(input)

0 commit comments

Comments
 (0)