Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Format #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ package main
import (
"encoding/json"
"fmt"
"io"
"os"

"github.com/go-playground/sensitive"
Expand All @@ -84,10 +83,10 @@ func init() {
// override default Formatter
sensitive.FormatStringFn = func(s sensitive.String, f fmt.State, c rune) {
switch c {
case 's':
_, _ = io.WriteString(f, "redacted")
default:
sensitive.Format(f, c, "redacted")
case 'v':
_, _ = io.WriteString(f, string(s)[:4]+"*******")
sensitive.Format(f, c, string(s)[:4]+"*******")
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions _examples/custom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"io"
"os"

"github.com/go-playground/sensitive"
Expand All @@ -13,10 +12,10 @@ func init() {
// override default Formatter
sensitive.FormatStringFn = func(s sensitive.String, f fmt.State, c rune) {
switch c {
case 's':
_, _ = io.WriteString(f, "redacted")
default:
sensitive.Format(f, c, "redacted")
case 'v':
_, _ = io.WriteString(f, string(s)[:4]+"*******")
sensitive.Format(f, c, string(s)[:4]+"*******")
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package sensitive

import (
"fmt"
"strconv"
"strings"
)

// Format outputs value accordingly to formatting options.
//
// It is useful in case you'll redefine some Format<type>Fn to output
// redacted value using formatting applied to original value.
//
// sensitive.FormatStringFn = func(s sensitive.String, f fmt.State, c rune) {
// sensitive.Format(f, c, "REDACTED")
// }
// sensitive.FormatBytesFn = func(s sensitive.Bytes, f fmt.State, c rune) {
// sensitive.Format(f, c, []byte{0xDE, 0xFA, 0xCE})
// }
func Format(f fmt.State, c rune, value interface{}) {
const flags = "+-# 0"
var format strings.Builder
format.Grow(8)
format.WriteRune('%')
for _, c := range flags {
if f.Flag(int(c)) {
format.WriteRune(c)
}
}
if wid, ok := f.Width(); ok {
format.WriteString(strconv.Itoa(wid))
}
if prec, ok := f.Precision(); ok {
format.WriteRune('.')
format.WriteString(strconv.Itoa(prec))
}
format.WriteRune(c)
fmt.Fprintf(f, format.String(), value)
}
38 changes: 38 additions & 0 deletions format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package sensitive

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

func TestFormat(t *testing.T) {
oldFn := FormatStringFn
defer func() {
FormatStringFn = oldFn
}()
FormatStringFn = func(s String, f fmt.State, c rune) {
Format(f, c, string(s))
}

tests := []struct {
formatting string
}{
{"%s"},
{"%q"},
{"%10s"},
{"%.3[1]q"},
{"%#-10v"},
}

for _, tc := range tests {
tc := tc
t.Run(tc.formatting, func(t *testing.T) {
assert := require.New(t)
want := fmt.Sprintf(tc.formatting, "value")
result := fmt.Sprintf(tc.formatting, String("value"))
assert.Equal(want, result)
})
}
}
2 changes: 1 addition & 1 deletion string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestStringCustomFormatFn(t *testing.T) {
FormatStringFn = oldFn
}()
FormatStringFn = func(s String, f fmt.State, c rune) {
_, _ = f.Write([]byte("blah"))
Format(f, c, "blah")
}

value := String("value")
Expand Down