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

Function byLength

expr/example.go:156–182  ·  view source on GitHub ↗

byLength generates a random size array of examples based on what's given.

(a *AttributeExpr, r *ExampleGenerator)

Source from the content-addressed store, hash-verified

154
155// byLength generates a random size array of examples based on what's given.
156func byLength(a *AttributeExpr, r *ExampleGenerator) any {
157 count := NewLength(a, r)
158 // Use unaliased type to handle alias types (e.g., Type("Alias", String))
159 dt := unalias(a.Type)
160 switch dt.Kind() {
161 case StringKind:
162 return r.Characters(count)
163 case BytesKind:
164 return []byte(r.Characters(count))
165 case MapKind:
166 raw := make(map[any]any)
167 m := dt.(*Map)
168 for range count {
169 raw[m.KeyType.Example(r)] = m.ElemType.Example(r)
170 }
171 return m.MakeMap(raw)
172 case ArrayKind:
173 raw := make([]any, count)
174 ar := dt.(*Array)
175 for i := range count {
176 raw[i] = ar.ElemType.Example(r)
177 }
178 return ar.MakeSlice(raw)
179 default:
180 panic("invalid type for length validation: " + a.Type.Name())
181 }
182}
183
184// byEnum returns a random selected enum value.
185func byEnum(a *AttributeExpr, r *ExampleGenerator) any {

Callers 1

ExampleMethod · 0.85

Calls 8

NewLengthFunction · 0.85
MakeMapMethod · 0.80
MakeSliceMethod · 0.80
unaliasFunction · 0.70
KindMethod · 0.65
CharactersMethod · 0.65
ExampleMethod · 0.65
NameMethod · 0.65

Tested by

no test coverage detected