()
| 366 | } |
| 367 | |
| 368 | func Example_format() { |
| 369 | testCases := []struct { |
| 370 | format string |
| 371 | args []interface{} |
| 372 | }{ |
| 373 | {"%d", []interface{}{123}}, |
| 374 | {"%v", []interface{}{[]int{123, 456}}}, |
| 375 | {"%v", []interface{}{[]RedactableString{"safe", "‹unsafe›"}}}, |
| 376 | {"%v", []interface{}{[]safe{"safe", "safe2"}}}, |
| 377 | {"%v", []interface{}{[]safestringer{{"safe"}, {"safe2"}}}}, |
| 378 | {"%v", []interface{}{makeMixedInts()}}, |
| 379 | {"%+v", []interface{}{makeMixedInts()}}, |
| 380 | {"%v", []interface{}{[]mixedInts{makeMixedInts(), makeMixedInts()}}}, |
| 381 | {"%+v", []interface{}{makeMixedSafe()}}, |
| 382 | {"%+v", []interface{}{makeMixedSafeStringer()}}, |
| 383 | {"%+v", []interface{}{makeMixedRedactableString()}}, |
| 384 | {"%+v", []interface{}{makeMixedRedactableBytes()}}, |
| 385 | } |
| 386 | |
| 387 | type formatterFn func(format string, args ...interface{}) string |
| 388 | formatters := []struct { |
| 389 | name string |
| 390 | fn formatterFn |
| 391 | }{ |
| 392 | {"fmt.sprint", fmt.Sprintf}, |
| 393 | {"redact.sprint", func(format string, args ...interface{}) string { return string(Sprintf(format, args...)) }}, |
| 394 | {"redact.printer", func(format string, args ...interface{}) string { |
| 395 | fn := func(w p) { w.Printf(format, args...) } |
| 396 | return string(Sprint(compose{fn: fn})) |
| 397 | }}, |
| 398 | } |
| 399 | |
| 400 | for _, tc := range testCases { |
| 401 | base := fmt.Sprintf("%q :: %T :: %+v", tc.format, tc.args[0], tc.args) |
| 402 | base = ptrRe.ReplaceAllString(base, "℘") |
| 403 | fmt.Println(base) |
| 404 | for _, f := range formatters { |
| 405 | result := f.fn(tc.format, tc.args...) |
| 406 | // Erase pointers. |
| 407 | result = ptrRe.ReplaceAllString(result, "℘") |
| 408 | fmt.Printf("%s:\t%s\n", f.name, result) |
| 409 | } |
| 410 | fmt.Println() |
| 411 | } |
| 412 | |
| 413 | // Output: |
| 414 | // "%d" :: int :: [123] |
| 415 | // fmt.sprint: 123 |
| 416 | // redact.sprint: ‹123› |
| 417 | // redact.printer: ‹123› |
| 418 | // |
| 419 | // "%v" :: []int :: [[123 456]] |
| 420 | // fmt.sprint: [123 456] |
| 421 | // redact.sprint: [‹123› ‹456›] |
| 422 | // redact.printer: [‹123› ‹456›] |
| 423 | // |
| 424 | // "%v" :: []markers.RedactableString :: [[safe ‹unsafe›]] |
| 425 | // fmt.sprint: [safe ‹unsafe›] |
nothing calls this directly
no test coverage detected
searching dependent graphs…