Skip to content

Commit 3ed72a0

Browse files
authored
Merge pull request #312 from wolfadex/pattern
Add support for `pattern` for strings
2 parents f92d6a1 + f413b6b commit 3ed72a0

7 files changed

Lines changed: 405 additions & 52 deletions

File tree

cli/example/elm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"elm/http": "2.0.0",
1616
"elm/json": "1.1.3",
1717
"elm/parser": "1.1.0",
18+
"elm/regex": "1.0.0",
1819
"elm/time": "1.0.0",
1920
"elm/url": "1.0.0",
2021
"elm-community/json-extra": "4.3.0",
@@ -27,7 +28,6 @@
2728
"NoRedInk/elm-random-pcg-extended": "1.0.0",
2829
"elm/file": "1.0.5",
2930
"elm/html": "1.0.0",
30-
"elm/regex": "1.0.0",
3131
"elm/virtual-dom": "1.0.3",
3232
"rtfeldman/elm-iso8601-date-strings": "1.1.4"
3333
}

cli/src/Cli.elm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,7 @@ printSuccessMessageAndWarnings ( outputPaths, { requiredPackages, warnings } ) =
12191219
|> FastSet.insert "elm/json"
12201220
|> FastSet.insert "elm/url"
12211221
|> FastSet.insert "elm/bytes"
1222+
|> FastSet.insert "elm/regex"
12221223
|> FastSet.toList
12231224

12241225
toInstall : String -> Pretty.Doc ()

codegen/elm.codegen.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"elm/parser": "1.1.0",
1616
"wolfadex/elm-rfc3339": "2.0.0",
1717
"danfishgold/base64-bytes": "1.1.0",
18-
"cachix/elm-uuid": "2.0.0"
18+
"cachix/elm-uuid": "2.0.0",
19+
"elm/regex": "1.0.0"
1920
},
2021
"local": [
2122
"helpers"

src/Common.elm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ type Type
452452
BasicType
453453
{ format : Maybe String
454454
, const : Maybe ConstValue
455+
, pattern : Maybe String
455456
}
456457
| Null
457458
| List Type

src/Gen/Regex.elm

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
module Gen.Regex exposing
2+
( fromString, never
3+
, call_
4+
)
5+
6+
{-|
7+
8+
9+
# Generated bindings for Regex
10+
11+
@docs fromString, never
12+
@docs call_
13+
14+
-}
15+
16+
import Elm
17+
import Elm.Annotation as Type
18+
19+
20+
{-| Try to create a `Regex`. Not all strings are valid though, so you get a
21+
\`Maybe' back. This means you can safely accept input from users.
22+
23+
import Regex
24+
25+
lowerCase : Regex.Regex
26+
lowerCase =
27+
Maybe.withDefault Regex.never <|
28+
Regex.fromString "[a-z]+"
29+
30+
**Note:** There are some [shorthand character classes][short] like `\w` for
31+
word characters, `\s` for whitespace characters, and `\d` for digits. **Make
32+
sure they are properly escaped!** If you specify them directly in your code,
33+
they would look like `"\\w\\s\\d"`.
34+
35+
[short]: https://www.regular-expressions.info/shorthand.html
36+
37+
fromString: String -> Maybe Regex.Regex
38+
39+
-}
40+
fromString : String -> Elm.Expression
41+
fromString fromStringArg_ =
42+
Elm.apply
43+
(Elm.value
44+
{ importFrom = [ "Regex" ]
45+
, name = "fromString"
46+
, annotation =
47+
Just
48+
(Type.function
49+
[ Type.string ]
50+
(Type.maybe (Type.namedWith [ "Regex" ] "Regex" []))
51+
)
52+
}
53+
)
54+
[ Elm.string fromStringArg_ ]
55+
56+
57+
{-| A regular expression that never matches any string.
58+
59+
never: Regex.Regex
60+
61+
-}
62+
never : Elm.Expression
63+
never =
64+
Elm.value
65+
{ importFrom = [ "Regex" ]
66+
, name = "never"
67+
, annotation = Just (Type.namedWith [ "Regex" ] "Regex" [])
68+
}
69+
70+
71+
call_ :
72+
{ fromString : Elm.Expression -> Elm.Expression
73+
, fromStringWith : Elm.Expression -> Elm.Expression -> Elm.Expression
74+
, contains : Elm.Expression -> Elm.Expression -> Elm.Expression
75+
, split : Elm.Expression -> Elm.Expression -> Elm.Expression
76+
, find : Elm.Expression -> Elm.Expression -> Elm.Expression
77+
, replace :
78+
Elm.Expression -> Elm.Expression -> Elm.Expression -> Elm.Expression
79+
, splitAtMost :
80+
Elm.Expression -> Elm.Expression -> Elm.Expression -> Elm.Expression
81+
, findAtMost :
82+
Elm.Expression -> Elm.Expression -> Elm.Expression -> Elm.Expression
83+
, replaceAtMost :
84+
Elm.Expression
85+
-> Elm.Expression
86+
-> Elm.Expression
87+
-> Elm.Expression
88+
-> Elm.Expression
89+
}
90+
call_ =
91+
{ fromString =
92+
\fromStringArg_ ->
93+
Elm.apply
94+
(Elm.value
95+
{ importFrom = [ "Regex" ]
96+
, name = "fromString"
97+
, annotation =
98+
Just
99+
(Type.function
100+
[ Type.string ]
101+
(Type.maybe
102+
(Type.namedWith [ "Regex" ] "Regex" [])
103+
)
104+
)
105+
}
106+
)
107+
[ fromStringArg_ ]
108+
, fromStringWith =
109+
\fromStringWithArg_ fromStringWithArg_0 ->
110+
Elm.apply
111+
(Elm.value
112+
{ importFrom = [ "Regex" ]
113+
, name = "fromStringWith"
114+
, annotation =
115+
Just
116+
(Type.function
117+
[ Type.namedWith [ "Regex" ] "Options" []
118+
, Type.string
119+
]
120+
(Type.maybe
121+
(Type.namedWith [ "Regex" ] "Regex" [])
122+
)
123+
)
124+
}
125+
)
126+
[ fromStringWithArg_, fromStringWithArg_0 ]
127+
, contains =
128+
\containsArg_ containsArg_0 ->
129+
Elm.apply
130+
(Elm.value
131+
{ importFrom = [ "Regex" ]
132+
, name = "contains"
133+
, annotation =
134+
Just
135+
(Type.function
136+
[ Type.namedWith [ "Regex" ] "Regex" []
137+
, Type.string
138+
]
139+
Type.bool
140+
)
141+
}
142+
)
143+
[ containsArg_, containsArg_0 ]
144+
, split =
145+
\splitArg_ splitArg_0 ->
146+
Elm.apply
147+
(Elm.value
148+
{ importFrom = [ "Regex" ]
149+
, name = "split"
150+
, annotation =
151+
Just
152+
(Type.function
153+
[ Type.namedWith [ "Regex" ] "Regex" []
154+
, Type.string
155+
]
156+
(Type.list Type.string)
157+
)
158+
}
159+
)
160+
[ splitArg_, splitArg_0 ]
161+
, find =
162+
\findArg_ findArg_0 ->
163+
Elm.apply
164+
(Elm.value
165+
{ importFrom = [ "Regex" ]
166+
, name = "find"
167+
, annotation =
168+
Just
169+
(Type.function
170+
[ Type.namedWith [ "Regex" ] "Regex" []
171+
, Type.string
172+
]
173+
(Type.list
174+
(Type.namedWith [ "Regex" ] "Match" [])
175+
)
176+
)
177+
}
178+
)
179+
[ findArg_, findArg_0 ]
180+
, replace =
181+
\replaceArg_ replaceArg_0 replaceArg_1 ->
182+
Elm.apply
183+
(Elm.value
184+
{ importFrom = [ "Regex" ]
185+
, name = "replace"
186+
, annotation =
187+
Just
188+
(Type.function
189+
[ Type.namedWith [ "Regex" ] "Regex" []
190+
, Type.function
191+
[ Type.namedWith [ "Regex" ] "Match" [] ]
192+
Type.string
193+
, Type.string
194+
]
195+
Type.string
196+
)
197+
}
198+
)
199+
[ replaceArg_, replaceArg_0, replaceArg_1 ]
200+
, splitAtMost =
201+
\splitAtMostArg_ splitAtMostArg_0 splitAtMostArg_1 ->
202+
Elm.apply
203+
(Elm.value
204+
{ importFrom = [ "Regex" ]
205+
, name = "splitAtMost"
206+
, annotation =
207+
Just
208+
(Type.function
209+
[ Type.int
210+
, Type.namedWith [ "Regex" ] "Regex" []
211+
, Type.string
212+
]
213+
(Type.list Type.string)
214+
)
215+
}
216+
)
217+
[ splitAtMostArg_, splitAtMostArg_0, splitAtMostArg_1 ]
218+
, findAtMost =
219+
\findAtMostArg_ findAtMostArg_0 findAtMostArg_1 ->
220+
Elm.apply
221+
(Elm.value
222+
{ importFrom = [ "Regex" ]
223+
, name = "findAtMost"
224+
, annotation =
225+
Just
226+
(Type.function
227+
[ Type.int
228+
, Type.namedWith [ "Regex" ] "Regex" []
229+
, Type.string
230+
]
231+
(Type.list
232+
(Type.namedWith [ "Regex" ] "Match" [])
233+
)
234+
)
235+
}
236+
)
237+
[ findAtMostArg_, findAtMostArg_0, findAtMostArg_1 ]
238+
, replaceAtMost =
239+
\replaceAtMostArg_ replaceAtMostArg_0 replaceAtMostArg_1 replaceAtMostArg_2 ->
240+
Elm.apply
241+
(Elm.value
242+
{ importFrom = [ "Regex" ]
243+
, name = "replaceAtMost"
244+
, annotation =
245+
Just
246+
(Type.function
247+
[ Type.int
248+
, Type.namedWith [ "Regex" ] "Regex" []
249+
, Type.function
250+
[ Type.namedWith [ "Regex" ] "Match" [] ]
251+
Type.string
252+
, Type.string
253+
]
254+
Type.string
255+
)
256+
}
257+
)
258+
[ replaceAtMostArg_
259+
, replaceAtMostArg_0
260+
, replaceAtMostArg_1
261+
, replaceAtMostArg_2
262+
]
263+
}

src/OpenApi/Generate.elm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,7 @@ operationToTypesExpectAndResolver effectTypes method pathUrl operation =
27232723
Common.Basic Common.String
27242724
{ const = Nothing
27252725
, format = Nothing
2726+
, pattern = Nothing
27262727
}
27272728
|> SuccessType
27282729
, bodyTypeAnnotation = Elm.Annotation.string

0 commit comments

Comments
 (0)