@@ -4,10 +4,9 @@ import (
4
4
"fmt"
5
5
"math"
6
6
"regexp"
7
+ "regexp/syntax"
8
+ "strings"
7
9
"time"
8
-
9
- regen "github.com/AnatolyRugalev/goregen"
10
- googleuuid "github.com/google/uuid"
11
10
)
12
11
13
12
const (
@@ -209,23 +208,17 @@ func byFormat(a *AttributeExpr, r *ExampleGenerator) any {
209
208
FormatIP : r .IPv4Address ().String (),
210
209
FormatURI : r .URL (),
211
210
FormatMAC : func () string {
212
- res , err := regen . Generate (`([0-9A-F]{2}-){5}[0-9A-F]{2}` )
211
+ res , err := syntax . Parse (`([0-9A-F]{2}-){5}[0-9A-F]{2}` , 0 )
213
212
if err != nil {
214
213
return "12-34-56-78-9A-BC"
215
214
}
216
- return res
215
+ return patgen ( res , r )
217
216
}(),
218
217
FormatCIDR : "192.168.100.14/24" ,
219
218
FormatRegexp : r .Characters (3 ) + ".*" ,
220
219
FormatRFC1123 : time .Unix (int64 (r .Int ())% 1454957045 , 0 ).UTC ().Format (time .RFC1123 ), // to obtain a "fixed" rand
221
- FormatUUID : func () string {
222
- uuid , err := googleuuid .NewUUID ()
223
- if err != nil {
224
- return "12345678-1234-1234-9232-123456789ABC"
225
- }
226
- return uuid .String ()
227
- }(),
228
- FormatJSON :
`{"name":"example","email":"[email protected] "}` ,
220
+ FormatUUID : r .UUID (),
221
+ FormatJSON :
`{"name":"example","email":"[email protected] "}` ,
229
222
}[format ]; ok {
230
223
return res
231
224
}
@@ -240,11 +233,67 @@ func byPattern(a *AttributeExpr, r *ExampleGenerator) any {
240
233
return false
241
234
}
242
235
pattern := a .Validation .Pattern
243
- gen , err := regen . NewGenerator (pattern , & regen. GeneratorArgs { MaxUnboundedRepeatCount : 6 } )
236
+ re , err := syntax . Parse (pattern , syntax . Perl )
244
237
if err != nil {
245
238
return r .Name ()
246
239
}
247
- return gen .Generate ()
240
+ return patgen (re .Simplify (), r )
241
+ }
242
+
243
+ func patgen (re * syntax.Regexp , r * ExampleGenerator ) string {
244
+ switch re .Op {
245
+ case syntax .OpAlternate :
246
+ i := r .Int () % len (re .Sub )
247
+ return patgen (re .Sub [i ], r )
248
+ case syntax .OpCapture :
249
+ return patgen (re .Sub [0 ], r )
250
+ case syntax .OpConcat :
251
+ var res strings.Builder
252
+ for _ , sub := range re .Sub {
253
+ res .WriteString (patgen (sub , r ))
254
+ }
255
+ return res .String ()
256
+ case syntax .OpLiteral :
257
+ return string (re .Rune )
258
+ case syntax .OpStar :
259
+ var res strings.Builder
260
+ count := r .Int () % 3
261
+ for i := 0 ; i < count ; i ++ {
262
+ res .WriteString (patgen (re .Sub [0 ], r ))
263
+ }
264
+ return res .String ()
265
+ case syntax .OpPlus :
266
+ var res strings.Builder
267
+ count := r .Int ()% 2 + 1
268
+ for i := 0 ; i < count ; i ++ {
269
+ res .WriteString (patgen (re .Sub [0 ], r ))
270
+ }
271
+ return res .String ()
272
+ case syntax .OpQuest :
273
+ if r .Int ()% 2 == 0 {
274
+ return patgen (re .Sub [0 ], r )
275
+ }
276
+ return ""
277
+ case syntax .OpRepeat :
278
+ var res strings.Builder
279
+ for i := 0 ; i < re .Min ; i ++ {
280
+ res .WriteString (patgen (re .Sub [0 ], r ))
281
+ }
282
+ return res .String ()
283
+ case syntax .OpCharClass :
284
+ var chars []rune
285
+ for i := 0 ; i < len (re .Rune ); i += 2 {
286
+ start , end := re .Rune [i ], re .Rune [i + 1 ]
287
+ for j := start ; j <= end ; j ++ {
288
+ chars = append (chars , j )
289
+ }
290
+ }
291
+ return string (chars [r .Int ()% len (chars )])
292
+ case syntax .OpAnyChar , syntax .OpAnyCharNotNL :
293
+ return r .Characters (1 )
294
+ default :
295
+ return ""
296
+ }
248
297
}
249
298
250
299
func byMinMax (a * AttributeExpr , r * ExampleGenerator ) any {
0 commit comments