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

Function jsonExample

codegen/cli/cli.go:434–473  ·  view source on GitHub ↗

jsonExample generates a json example

(v any)

Source from the content-addressed store, hash-verified

432
433// jsonExample generates a json example
434func jsonExample(v any) string {
435 // In JSON, keys must be a string. But goa allows map keys to be anything.
436 r := reflect.ValueOf(v)
437 if r.Kind() == reflect.Map {
438 keys := r.MapKeys()
439 if keys[0].Kind() != reflect.String {
440 a := make(map[string]any, len(keys))
441 var kstr string
442 for _, k := range keys {
443 switch t := k.Interface().(type) {
444 case bool:
445 kstr = strconv.FormatBool(t)
446 case int32:
447 kstr = strconv.FormatInt(int64(t), 10)
448 case int64:
449 kstr = strconv.FormatInt(t, 10)
450 case int:
451 kstr = strconv.Itoa(t)
452 case float32:
453 kstr = strconv.FormatFloat(float64(t), 'f', -1, 32)
454 case float64:
455 kstr = strconv.FormatFloat(t, 'f', -1, 64)
456 default:
457 kstr = k.String()
458 }
459 a[kstr] = r.MapIndex(k).Interface()
460 }
461 v = a
462 }
463 }
464 b, err := json.MarshalIndent(v, " ", " ")
465 ex := "?"
466 if err == nil {
467 ex = string(b)
468 }
469 if strings.Contains(ex, "\n") {
470 ex = "'" + strings.ReplaceAll(ex, "'", "\\'") + "'"
471 }
472 return ex
473}
474
475var (
476 boolN = codegen.GoNativeTypeName(expr.Boolean)

Callers 1

NewFlagDataFunction · 0.85

Calls 2

KindMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected