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

Function Example

dsl/attribute.go:351–392  ·  view source on GitHub ↗

Example provides an example value for a type, a parameter, a header or any attribute. Example supports two syntaxes: one syntax accepts two arguments where the first argument is a summary describing the example and the second a value provided directly or via a DSL which may also specify a long descr

(args ...any)

Source from the content-addressed store, hash-verified

349// })
350// })
351func Example(args ...any) {
352 if len(args) == 0 {
353 eval.TooFewArgError()
354 return
355 }
356 if len(args) > 2 {
357 eval.TooManyArgError()
358 return
359 }
360 var (
361 summary string
362 arg any
363 )
364 if len(args) == 1 {
365 summary = "default"
366 arg = args[0]
367 } else {
368 var ok bool
369 summary, ok = args[0].(string)
370 if !ok {
371 eval.InvalidArgError("summary (string)", args[0])
372 return
373 }
374 arg = args[1]
375 }
376 a, ok := eval.Current().(*expr.AttributeExpr)
377 if !ok {
378 eval.IncompatibleDSL()
379 return
380 }
381 ex := &expr.ExampleExpr{Summary: summary}
382 if dsl, ok := arg.(func()); ok {
383 eval.Execute(dsl, ex)
384 } else {
385 ex.Value = arg
386 }
387 if ex.Value == nil {
388 eval.ReportError("example value is missing")
389 return
390 }
391 a.UserExamples = append(a.UserExamples, ex)
392}
393
394func parseAttributeArgs(baseAttr *expr.AttributeExpr, args ...any) (expr.DataType, string, func()) {
395 var (

Callers 5

TestInvalidArgErrorFunction · 0.50
TestTooFewArgErrorFunction · 0.50
TestTooManyArgErrorFunction · 0.50
type_spec_test.goFile · 0.50

Calls 7

TooFewArgErrorFunction · 0.92
TooManyArgErrorFunction · 0.92
InvalidArgErrorFunction · 0.92
CurrentFunction · 0.92
IncompatibleDSLFunction · 0.92
ExecuteFunction · 0.92
ReportErrorFunction · 0.92

Tested by 3

TestInvalidArgErrorFunction · 0.40
TestTooFewArgErrorFunction · 0.40
TestTooManyArgErrorFunction · 0.40