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)
| 349 | // }) |
| 350 | // }) |
| 351 | func 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 | |
| 394 | func parseAttributeArgs(baseAttr *expr.AttributeExpr, args ...any) (expr.DataType, string, func()) { |
| 395 | var ( |