Skip to content

Commit

Permalink
Do not implement json.Marshaler and json.Unmarshaler (#32)
Browse files Browse the repository at this point in the history
Implementing encoding.TextMarshaler and encoding.TextUnmarshaler already
satisfies all use cases the Text type.

Tests and docs were adjusted accordingly.
  • Loading branch information
rsjethani authored May 30, 2024
1 parent adc2085 commit 6891a61
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 84 deletions.
25 changes: 1 addition & 24 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,6 @@ func ExampleCustomRedact() {
}

func ExampleText_MarshalText() {
sec := secret.New("secret!")
bytes, err := sec.MarshalText()
if err != nil {
panic(err)
}

fmt.Println(string(bytes))
// Output: *****
}

func ExampleText_UnmarshalText() {
sec := secret.Text{}

err := sec.UnmarshalText([]byte(`$ecre!`))
if err != nil {
panic(err)
}

fmt.Println(sec, sec.Value())
// Output: ***** $ecre!
}

func ExampleText_MarshalJSON() {
login := struct {
User string
Password secret.Text
Expand All @@ -78,7 +55,7 @@ func ExampleText_MarshalJSON() {
// Output: {"User":"John","Password":"*****"}
}

func ExampleText_UnmarshalJSON() {
func ExampleText_UnmarshalText() {
login := struct {
User string
Password secret.Text
Expand Down
38 changes: 4 additions & 34 deletions secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
// etc. but provide access to the secret when requested explicitly.
package secret

import (
"encoding/json"
"fmt"
)

// Text provides a way to safely store your secret value and a corresponding redact hint. This
// redact hint what is used in operations like printing and serializing. The default
// value of Text is usable.
Expand Down Expand Up @@ -58,14 +53,14 @@ func (s Text) Value() string {
return *s.v
}

// MarshalText implements [encoding.TextMarshaler]. It marshals redact string into bytes rather than the actual
// secret value.
// MarshalText implements [encoding.TextMarshaler]. It marshals redact string into bytes rather than
// the actual secret value.
func (s Text) MarshalText() ([]byte, error) {
return []byte(*s.r), nil
}

// UnmarshalText implements [encoding.TextUnmarshaler]. It unmarshals b into receiver's new secret value.
// If redact string is present then it is reused otherwise [DefaultRedact] is used.
// UnmarshalText implements [encoding.TextUnmarshaler]. It unmarshals b into receiver's new secret
// value. If redact string is present then it is reused.
func (s *Text) UnmarshalText(b []byte) error {
v := string(b)

Expand All @@ -78,31 +73,6 @@ func (s *Text) UnmarshalText(b []byte) error {
return nil
}

// MarshalJSON allows Text to be serialized into a JSON string. Only the redact hint is part of the
// the JSON string.
func (s Text) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, *s.r)), nil
}

// UnmarshalJSON allows a JSON string to be deserialized into a Text value. DefaultRedact is set
// as the redact hint.
func (s *Text) UnmarshalJSON(b []byte) error {
// Get the new secret value from unmarshalled data.
var n string
if err := json.Unmarshal(b, &n); err != nil {
return err
}

// If the original redact is not nil then use it otherwise fallback to default.
if s.r != nil {
*s = New(n, CustomRedact(*s.r))
} else {
*s = New(n)
}

return nil
}

// Equals checks whether s2 has same secret string or not.
func (s *Text) Equals(s2 Text) bool {
return *s.v == *s2.v
Expand Down
26 changes: 0 additions & 26 deletions secret_test.go

This file was deleted.

0 comments on commit 6891a61

Please sign in to comment.