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

[configopaque] Implement fmt.GoStringer interface for String #9271

Merged
merged 3 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion .chloggen/configopaque_stringer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ change_type: breaking
component: configopaque

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: configopaque.String implements `fmt.Stringer`, outputting [REDACTED] when used as `fmt.Sprintf("%s", opaquestring)`
note: configopaque.String implements `fmt.Stringer` and `fmt.GoStringer`, outputting [REDACTED] when formatted with the %s, %q or %#v verbs`

# One or more tracking issues or pull requests related to the change
issues: [9213]
Expand Down
16 changes: 13 additions & 3 deletions config/configopaque/opaque.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import (
"fmt"
)

// String alias that is marshaled in an opaque way.
// String alias that is marshaled and printed in an opaque way.
type String string

var _ fmt.Stringer = String("")

const maskedString = "[REDACTED]"

var _ encoding.TextMarshaler = String("")
Expand All @@ -22,6 +20,18 @@ func (s String) MarshalText() ([]byte, error) {
return []byte(maskedString), nil
}

var _ fmt.Stringer = String("")

// String formats the string as `[REDACTED]`.
// This is used for the %s and %q verbs.
func (s String) String() string {
return maskedString
}

var _ fmt.GoStringer = String("")

// GoString formats the string as `[REDACTED]`.
// This is used for the %#v verb.
func (s String) GoString() string {
return maskedString
}
44 changes: 41 additions & 3 deletions config/configopaque/opaque_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,65 @@
package configopaque // import "go.opentelemetry.io/collector/config/configopaque"

import (
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)

func TestStringMarshalText(t *testing.T) {
examples := []String{"opaque", "s", "veryveryveryveryveryveryveryveryveryverylong"}
for _, example := range examples {
opaque, err := example.MarshalText()
require.NoError(t, err)
assert.Equal(t, "[REDACTED]", string(opaque))
assert.Equal(t, maskedString, string(opaque))
}
}

type TestStruct struct {
Opaque String `json:"opaque" yaml:"opaque"`
Plain string `json:"plain" yaml:"plain"`
}

var example = TestStruct{
Opaque: "opaque",
Plain: "plain",
}

func TestStringJSON(t *testing.T) {
bytes, err := json.Marshal(example)
require.NoError(t, err)
assert.Equal(t, `{"opaque":"[REDACTED]","plain":"plain"}`, string(bytes))
}

func TestStringYAML(t *testing.T) {
bytes, err := yaml.Marshal(example)
require.NoError(t, err)
assert.Equal(t, "opaque: '[REDACTED]'\nplain: plain\n", string(bytes))
}

func TestStringFmt(t *testing.T) {
examples := []String{"opaque", "s", "veryveryveryveryveryveryveryveryveryverylong"}
verbTests := []struct {
verb string
want string
}{
{verb: "%s", want: maskedString},
{verb: "%q", want: fmt.Sprintf("%q", maskedString)},
{verb: "%v", want: maskedString},
{verb: "%#v", want: maskedString},
{verb: "%+v", want: maskedString},
{verb: "%x", want: fmt.Sprintf("%x", maskedString)},
}
for _, example := range examples {
// nolint S1025
assert.Equal(t, "[REDACTED]", fmt.Sprintf("%s", example))
for _, tt := range verbTests {
t.Run(fmt.Sprintf("%s/%s", string(example), tt.verb), func(t *testing.T) {
assert.Equal(t, tt.want, fmt.Sprintf(tt.verb, example))
})
}
// converting to a string allows to output as an unredacted string still:
// nolint S1025
assert.Equal(t, string(example), fmt.Sprintf("%s", string(example)))
mx-psi marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading