MCPcopy Create free account
hub / github.com/goadesign/goa / Example

Method Example

expr/example.go:20–91  ·  view source on GitHub ↗

Example returns the example set on the attribute at design time. If there isn't such a value then Example computes a random value for the attribute using the given random value producer.

(r *ExampleGenerator)

Source from the content-addressed store, hash-verified

18// isn't such a value then Example computes a random value for the attribute
19// using the given random value producer.
20func (a *AttributeExpr) Example(r *ExampleGenerator) any {
21 if ex := a.ExtractUserExamples(); len(ex) > 0 {
22 // Return the last item in the slice so that examples can be overridden
23 // in the DSL. Overridden examples are always appended to the UserExamples
24 // slice.
25 return ex[len(ex)-1].Value
26 }
27
28 if r.Randomizer == nil {
29 return nil
30 }
31
32 value, ok := a.Meta.Last("openapi:generate")
33 if !ok {
34 value, ok = a.Meta.Last("swagger:generate")
35 }
36 if ok && value == "false" {
37 return nil
38 }
39
40 value, ok = a.Meta.Last("openapi:example")
41 if !ok {
42 value, ok = a.Meta.Last("swagger:example")
43 }
44 if ok && value == "false" {
45 return nil
46 }
47
48 // randomize array length first, since that's from higher level
49 if hasLengthValidation(a) {
50 return byLength(a, r)
51 }
52 // enum should dominate, because the potential "examples" are fixed
53 if hasEnumValidation(a) {
54 return byEnum(a, r)
55 }
56 // loop until a satisfying example is generated
57 var (
58 hasFormat = hasFormatValidation(a)
59 hasPattern = hasPatternValidation(a)
60 hasMinMax = hasMinMaxValidation(a)
61 attempts = 0
62 )
63 for attempts < maxAttempts {
64 attempts++
65 var example any
66 // Format comes first, since it initiates the example
67 if hasFormat {
68 example = byFormat(a, r)
69 }
70 // now validate with rest of matchers; redo if not satisified
71 if hasPattern {
72 if example == nil {
73 example = byPattern(a, r)
74 } else if !checkPattern(a, example) {
75 continue
76 }
77 }

Callers 6

TestByPatternFunction · 0.95
TestByFormatUUIDFunction · 0.95
extractHeadersMethod · 0.95
extractCookiesMethod · 0.95

Calls 15

ExtractUserExamplesMethod · 0.95
hasLengthValidationFunction · 0.85
byLengthFunction · 0.85
hasEnumValidationFunction · 0.85
byEnumFunction · 0.85
hasFormatValidationFunction · 0.85
hasPatternValidationFunction · 0.85
hasMinMaxValidationFunction · 0.85
byFormatFunction · 0.85
byPatternFunction · 0.85
checkPatternFunction · 0.85
byMinMaxFunction · 0.85

Tested by 4

TestByPatternFunction · 0.76
TestByFormatUUIDFunction · 0.76