From 6891a614221de2dbf7ed2f9b50f590a9f99d19dd Mon Sep 17 00:00:00 2001 From: Ravi Shekhar Jethani Date: Thu, 30 May 2024 17:41:08 +0200 Subject: [PATCH] Do not implement json.Marshaler and json.Unmarshaler (#32) Implementing encoding.TextMarshaler and encoding.TextUnmarshaler already satisfies all use cases the Text type. Tests and docs were adjusted accordingly. --- example_test.go | 25 +------------------------ secret.go | 38 ++++---------------------------------- secret_test.go | 26 -------------------------- 3 files changed, 5 insertions(+), 84 deletions(-) delete mode 100644 secret_test.go diff --git a/example_test.go b/example_test.go index 7e9fb3f..0bd0ae5 100644 --- a/example_test.go +++ b/example_test.go @@ -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 @@ -78,7 +55,7 @@ func ExampleText_MarshalJSON() { // Output: {"User":"John","Password":"*****"} } -func ExampleText_UnmarshalJSON() { +func ExampleText_UnmarshalText() { login := struct { User string Password secret.Text diff --git a/secret.go b/secret.go index 10c9d28..1aceb53 100644 --- a/secret.go +++ b/secret.go @@ -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. @@ -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) @@ -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 diff --git a/secret_test.go b/secret_test.go deleted file mode 100644 index 0aef7ff..0000000 --- a/secret_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package secret - -import ( - "encoding/json" - "testing" -) - -func TestText_UnmarshalJSON_allocates_new_data_rather_than_overwriting_existing(t *testing.T) { - s1 := New("hello") - - oldRedact := s1.r - oldValue := s1.v - - err := json.Unmarshal([]byte(`"hello"`), &s1) - if err != nil { - t.Fatal(err) - } - - if s1.r == oldRedact { - t.Fatal("UnmarshalJSON did not allocate new redact string instead it overwrote exitsing") - } - - if s1.v == oldValue { - t.Fatal("UnmarshalJSON did not allocate new value string instead it overwrote exitsing") - } -}