1
1
use std:: collections:: HashMap ;
2
- use std:: time:: { SystemTime , UNIX_EPOCH } ;
3
2
4
3
use rand:: { Rng , SeedableRng } ;
5
4
use serde_json:: { Map , Value } ;
@@ -70,11 +69,7 @@ impl Visitor for RandDataGenerator<'_> {
70
69
71
70
if let Some ( regex_pattern) = element. pattern . clone ( ) {
72
71
let mut buffer: Vec < u8 > = vec ! [ ] ;
73
- let duration = SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( ) ;
74
- let u128_bytes = duration. as_nanos ( ) . to_be_bytes ( ) ;
75
- let mut u64_bytes = [ 0 ; 8 ] ;
76
- u64_bytes. copy_from_slice ( & u128_bytes[ 8 ..16 ] ) ;
77
- let seed = u64:: from_be_bytes ( u64_bytes) ;
72
+ let seed = rand:: thread_rng ( ) . gen ( ) ;
78
73
79
74
regex_generate:: Generator :: new (
80
75
& regex_pattern,
@@ -84,6 +79,7 @@ impl Visitor for RandDataGenerator<'_> {
84
79
. unwrap ( )
85
80
. generate ( & mut buffer)
86
81
. unwrap ( ) ;
82
+
87
83
let random_string = String :: from_utf8 ( buffer) . unwrap ( ) ;
88
84
89
85
return Ok ( serde_json:: Value :: String ( random_string) ) ;
@@ -111,7 +107,10 @@ impl Visitor for RandDataGenerator<'_> {
111
107
return Ok ( serde_json:: Value :: Array ( array) ) ;
112
108
}
113
109
114
- let number_of_elements = rand:: thread_rng ( ) . gen_range ( 1 ..3 ) ;
110
+ let min_items = element. min_items . unwrap_or ( 1 ) ;
111
+ let max_items = element. max_items . unwrap_or ( 3 ) ;
112
+
113
+ let number_of_elements = rand:: thread_rng ( ) . gen_range ( min_items..=max_items) ;
115
114
116
115
for _ in 0 ..number_of_elements {
117
116
let generated_value =
@@ -193,25 +192,27 @@ impl Visitor for RandDataGenerator<'_> {
193
192
} ;
194
193
195
194
// Determine optional fields by removing required fields from all fields
196
- let optional_fields: Vec < & String > =
195
+ let mut optional_fields: Vec < & String > =
197
196
all_fields. iter ( ) . filter ( |field| !required_fields. contains ( field) ) . cloned ( ) . collect ( ) ;
198
197
199
198
// if there are no optional fields then all fields have to be included
200
199
let fields_to_include = if optional_fields. is_empty ( ) {
201
200
required_fields
202
201
} else {
203
- // decide the starting index in optional fields array
204
- let start_idx = rand:: thread_rng ( ) . gen_range ( 0 ..optional_fields. len ( ) ) ;
205
- // decide the number of optional fields
206
- let optional_fields_to_include_count =
207
- rand:: thread_rng ( ) . gen_range ( 0 ..=( optional_fields. len ( ) - start_idx) ) ;
202
+ // decide the number of optional fields to remove
203
+ let mut optional_fields_left_to_remove =
204
+ rand:: thread_rng ( ) . gen_range ( 0 ..=optional_fields. len ( ) ) ;
205
+
206
+ // remove the optional fields 1 by 1
207
+ while optional_fields_left_to_remove > 0 {
208
+ optional_fields_left_to_remove -= 1 ;
209
+
210
+ let idx_to_remove = rand:: thread_rng ( ) . gen_range ( 0 ..optional_fields. len ( ) ) ;
211
+ optional_fields. swap_remove ( idx_to_remove) ;
212
+ }
208
213
209
214
// combine required and optional fields that will be part of the json object
210
- [
211
- required_fields. as_slice ( ) ,
212
- & optional_fields[ start_idx..start_idx + optional_fields_to_include_count] ,
213
- ]
214
- . concat ( )
215
+ [ required_fields. as_slice ( ) , optional_fields. as_slice ( ) ] . concat ( )
215
216
} ;
216
217
217
218
for ( key, inner_schema) in
0 commit comments