Skip to content

Commit 4526b39

Browse files
authored
Convert equality check from method to a function (#34)
The equality check is a readonly operation on Text values hence it is more of a package function candidate rather than a method on the type itself.
1 parent 2a49149 commit 4526b39

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

example_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ func ExampleText_UnmarshalText() {
7575
// $ecre!
7676
}
7777

78-
func ExampleText_Equals() {
79-
s1 := secret.New("hello")
80-
s2 := secret.New("hello")
81-
s3 := secret.New("hi")
82-
fmt.Println(s1.Equals(s2))
83-
fmt.Println(s1.Equals(s3))
78+
func ExampleEqual() {
79+
tx1 := secret.New("hello")
80+
tx2 := secret.New("hello", secret.RedactHint(secret.Redacted))
81+
tx3 := secret.New("world")
82+
fmt.Println(secret.Equal(tx1, tx2))
83+
fmt.Println(secret.Equal(tx1, tx3))
8484

8585
// Output:
8686
// true

secret.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,20 @@ package secret
77
// Text provides a way to safely store your secret value and a corresponding redact hint. This
88
// redact hint is what is used in operations like printing and serializing.
99
type Text struct {
10-
// v is the actual secret values.
11-
v *string
12-
// r is the redact hint to be used in place of secret.
13-
r *string
10+
secret *string
11+
redact *string
1412
}
1513

1614
// New returns [Text] for the secret with [FiveStar] as the default redact hint. Provide options
1715
// like [RedactHint] to modify default behavior.
1816
func New(secret string, options ...func(*Text)) Text {
1917
tx := Text{
20-
v: new(string),
21-
r: new(string),
18+
secret: new(string),
19+
redact: new(string),
2220
}
2321

24-
*tx.v = secret
25-
*tx.r = FiveStar
22+
*tx.secret = secret
23+
*tx.redact = FiveStar
2624

2725
for _, o := range options {
2826
o(&tx)
@@ -42,25 +40,25 @@ const (
4240
// the common redact hints provided with this package like [FiveX] or provide your own string.
4341
func RedactHint(r string) func(*Text) {
4442
return func(t *Text) {
45-
*t.r = r
43+
*t.redact = r
4644
}
4745
}
4846

4947
// String implements the [fmt.Stringer] interface and returns only the redact hint. This prevents the
5048
// secret value from being printed to std*, logs etc.
5149
func (tx Text) String() string {
52-
if tx.r == nil {
50+
if tx.redact == nil {
5351
return FiveStar
5452
}
55-
return *tx.r
53+
return *tx.redact
5654
}
5755

5856
// Value gives you access to the actual secret value stored inside Text.
5957
func (tx Text) Value() string {
60-
if tx.v == nil {
58+
if tx.secret == nil {
6159
return ""
6260
}
63-
return *tx.v
61+
return *tx.secret
6462
}
6563

6664
// MarshalText implements [encoding.TextMarshaler]. It marshals redact string into bytes rather than
@@ -75,15 +73,15 @@ func (tx *Text) UnmarshalText(b []byte) error {
7573
s := string(b)
7674

7775
// If the original redact is not nil then use it otherwise fallback to default.
78-
if tx.r != nil {
79-
*tx = New(s, RedactHint(*tx.r))
76+
if tx.redact != nil {
77+
*tx = New(s, RedactHint(*tx.redact))
8078
} else {
8179
*tx = New(s)
8280
}
8381
return nil
8482
}
8583

86-
// Equals checks whether s2 has same secret string or not.
87-
func (tx *Text) Equals(s2 Text) bool {
88-
return *tx.v == *s2.v
84+
// Equal returns true if both arguments have the same secret. The redact strings are not considered.
85+
func Equal(tx1, tx2 Text) bool {
86+
return *tx1.secret == *tx2.secret
8987
}

0 commit comments

Comments
 (0)