github.com/goldeneggg/goa@v1.3.1/design/example.go (about) 1 package design 2 3 import ( 4 "fmt" 5 "math" 6 "regexp" 7 "time" 8 9 regen "github.com/zach-klippenstein/goregen" 10 ) 11 12 // exampleGenerator generates a random example based on the given validations on the definition. 13 type exampleGenerator struct { 14 a *AttributeDefinition 15 r *RandomGenerator 16 } 17 18 // newExampleGenerator returns an example generator that uses the given random generator. 19 func newExampleGenerator(a *AttributeDefinition, r *RandomGenerator) *exampleGenerator { 20 return &exampleGenerator{a, r} 21 } 22 23 // Maximum number of tries for generating example. 24 const maxAttempts = 500 25 26 // Generate generates a random value based on the given validations. 27 func (eg *exampleGenerator) Generate(seen []string) interface{} { 28 // Randomize array length first, since that's from higher level 29 if eg.hasLengthValidation() { 30 return eg.generateValidatedLengthExample(seen) 31 } 32 // Enum should dominate, because the potential "examples" are fixed 33 if eg.hasEnumValidation() { 34 return eg.generateValidatedEnumExample() 35 } 36 // loop until a satisified example is generated 37 hasFormat, hasPattern, hasMinMax := eg.hasFormatValidation(), eg.hasPatternValidation(), eg.hasMinMaxValidation() 38 attempts := 0 39 for attempts < maxAttempts { 40 attempts++ 41 var example interface{} 42 // Format comes first, since it initiates the example 43 if hasFormat { 44 example = eg.generateFormatExample() 45 } 46 // now validate with the rest of matchers; if not satisified, redo 47 if hasPattern { 48 if example == nil { 49 example = eg.generateValidatedPatternExample() 50 } else if !eg.checkPatternValidation(example) { 51 continue 52 } 53 } 54 if hasMinMax { 55 if example == nil { 56 example = eg.generateValidatedMinMaxValueExample() 57 } else if !eg.checkMinMaxValueValidation(example) { 58 continue 59 } 60 } 61 if example == nil { 62 example = eg.a.Type.GenerateExample(eg.r, seen) 63 } 64 return example 65 } 66 return eg.a.Type.GenerateExample(eg.r, seen) 67 } 68 69 func (eg *exampleGenerator) ExampleLength() int { 70 if eg.hasLengthValidation() { 71 minlength, maxlength := math.Inf(1), math.Inf(-1) 72 if eg.a.Validation.MinLength != nil { 73 minlength = float64(*eg.a.Validation.MinLength) 74 } 75 if eg.a.Validation.MaxLength != nil { 76 maxlength = float64(*eg.a.Validation.MaxLength) 77 } 78 count := 0 79 if math.IsInf(minlength, 1) { 80 count = int(maxlength) - (eg.r.Int() % 3) 81 } else if math.IsInf(maxlength, -1) { 82 count = int(minlength) + (eg.r.Int() % 3) 83 } else if minlength < maxlength { 84 diff := int(maxlength - minlength) 85 if diff > maxExampleLength { 86 diff = maxExampleLength 87 } 88 count = int(minlength) + (eg.r.Int() % diff) 89 } else if minlength == maxlength { 90 count = int(minlength) 91 } else { 92 panic("Validation: MinLength > MaxLength") 93 } 94 if count > maxExampleLength { 95 count = maxExampleLength 96 } 97 if count <= 0 && maxlength != 0 { 98 count = 1 99 } 100 return count 101 } 102 return eg.r.Int()%3 + 1 103 } 104 105 func (eg *exampleGenerator) hasLengthValidation() bool { 106 if eg.a.Validation == nil { 107 return false 108 } 109 return eg.a.Validation.MinLength != nil || eg.a.Validation.MaxLength != nil 110 } 111 112 const maxExampleLength = 10 113 114 // generateValidatedLengthExample generates a random size array of examples based on what's given. 115 func (eg *exampleGenerator) generateValidatedLengthExample(seen []string) interface{} { 116 count := eg.ExampleLength() 117 if !eg.a.Type.IsArray() { 118 return eg.r.faker.Characters(count) 119 } 120 res := make([]interface{}, count) 121 for i := 0; i < count; i++ { 122 res[i] = eg.a.Type.ToArray().ElemType.GenerateExample(eg.r, seen) 123 } 124 return res 125 } 126 127 func (eg *exampleGenerator) hasEnumValidation() bool { 128 return eg.a.Validation != nil && len(eg.a.Validation.Values) > 0 129 } 130 131 // generateValidatedEnumExample returns a random selected enum value. 132 func (eg *exampleGenerator) generateValidatedEnumExample() interface{} { 133 if !eg.hasEnumValidation() { 134 return nil 135 } 136 values := eg.a.Validation.Values 137 count := len(values) 138 i := eg.r.Int() % count 139 return values[i] 140 } 141 142 func (eg *exampleGenerator) hasFormatValidation() bool { 143 return eg.a.Validation != nil && eg.a.Validation.Format != "" 144 } 145 146 // generateFormatExample returns a random example based on the format the user asks. 147 func (eg *exampleGenerator) generateFormatExample() interface{} { 148 if !eg.hasFormatValidation() { 149 return nil 150 } 151 format := eg.a.Validation.Format 152 if res, ok := map[string]interface{}{ 153 "email": eg.r.faker.Email(), 154 "hostname": eg.r.faker.DomainName() + "." + eg.r.faker.DomainSuffix(), 155 "date-time": time.Unix(int64(eg.r.Int())%1454957045, 0).Format(time.RFC3339), // to obtain a "fixed" rand 156 "ipv4": eg.r.faker.IPv4Address().String(), 157 "ipv6": eg.r.faker.IPv6Address().String(), 158 "ip": eg.r.faker.IPv4Address().String(), 159 "uri": eg.r.faker.URL(), 160 "mac": func() string { 161 res, err := regen.Generate(`([0-9A-F]{2}-){5}[0-9A-F]{2}`) 162 if err != nil { 163 return "12-34-56-78-9A-BC" 164 } 165 return res 166 }(), 167 "cidr": "192.168.100.14/24", 168 "regexp": eg.r.faker.Characters(3) + ".*", 169 "rfc1123": time.Unix(int64(eg.r.Int())%1454957045, 0).Format(time.RFC1123), // to obtain a "fixed" rand 170 }[format]; ok { 171 return res 172 } 173 panic("Validation: unknown format '" + format + "'") // bug 174 } 175 176 func (eg *exampleGenerator) hasPatternValidation() bool { 177 return eg.a.Validation != nil && eg.a.Validation.Pattern != "" 178 } 179 180 func (eg *exampleGenerator) checkPatternValidation(example interface{}) bool { 181 if !eg.hasPatternValidation() { 182 return true 183 } 184 pattern := eg.a.Validation.Pattern 185 re, err := regexp.Compile(pattern) 186 if err != nil { 187 panic("Validation: invalid pattern '" + pattern + "'") 188 } 189 if !re.MatchString(fmt.Sprint(example)) { 190 return false 191 } 192 return true 193 } 194 195 // generateValidatedPatternExample generates a random value that satisifies the pattern. Note: if 196 // multiple patterns are given, only one of them is used. currently, it doesn't support multiple. 197 func (eg *exampleGenerator) generateValidatedPatternExample() interface{} { 198 if !eg.hasPatternValidation() { 199 return false 200 } 201 pattern := eg.a.Validation.Pattern 202 example, err := regen.Generate(pattern) 203 if err != nil { 204 return eg.r.faker.Name() 205 } 206 return example 207 } 208 209 func (eg *exampleGenerator) hasMinMaxValidation() bool { 210 if eg.a.Validation == nil { 211 return false 212 } 213 return eg.a.Validation.Minimum != nil || eg.a.Validation.Maximum != nil 214 } 215 216 func (eg *exampleGenerator) checkMinMaxValueValidation(example interface{}) bool { 217 if !eg.hasMinMaxValidation() { 218 return true 219 } 220 valid := true 221 if min := eg.a.Validation.Minimum; min != nil { 222 if v, ok := example.(int); ok && float64(v) < *min { 223 valid = false 224 } else if v, ok := example.(float64); ok && v < *min { 225 valid = false 226 } 227 } 228 if !valid { 229 return false 230 } 231 if max := eg.a.Validation.Maximum; max != nil { 232 if v, ok := example.(int); ok && float64(v) > *max { 233 return false 234 } else if v, ok := example.(float64); ok && v > *max { 235 return false 236 } 237 } 238 return true 239 } 240 241 func (eg *exampleGenerator) generateValidatedMinMaxValueExample() interface{} { 242 if !eg.hasMinMaxValidation() { 243 return nil 244 } 245 min, max := math.Inf(1), math.Inf(-1) 246 if eg.a.Validation.Minimum != nil { 247 min = *eg.a.Validation.Minimum 248 } 249 if eg.a.Validation.Maximum != nil { 250 max = *eg.a.Validation.Maximum 251 } 252 if math.IsInf(min, 1) { 253 if eg.a.Type.Kind() == IntegerKind { 254 if max == 0 { 255 return int(max) - eg.r.Int()%3 256 } 257 return eg.r.Int() % int(max) 258 } 259 return eg.r.Float64() * max 260 } else if math.IsInf(max, -1) { 261 if eg.a.Type.Kind() == IntegerKind { 262 if min == 0 { 263 return int(min) + eg.r.Int()%3 264 } 265 return int(min) + eg.r.Int()%int(min) 266 } 267 return min + eg.r.Float64()*min 268 } else if min < max { 269 if eg.a.Type.Kind() == IntegerKind { 270 return int(min) + eg.r.Int()%int(max-min) 271 } 272 return min + eg.r.Float64()*(max-min) 273 } else if min == max { 274 if eg.a.Type.Kind() == IntegerKind { 275 return int(min) 276 } 277 return min 278 } 279 panic("Validation: Min > Max") 280 }