From d2152205f56f1029c7face5d4f2b486f28677147 Mon Sep 17 00:00:00 2001 From: Hank Donnay Date: Mon, 24 Jan 2022 09:48:39 -0600 Subject: [PATCH] config: MarshalText on value receiver This change will allow the yaml.v3 encoder to see the method as it's walking the struct. I added a json test to ensure this behavior. Signed-off-by: Hank Donnay --- config/auth.go | 8 ++++---- config/auth_test.go | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/config/auth.go b/config/auth.go index 0229fd776e..a654d5f6b0 100644 --- a/config/auth.go +++ b/config/auth.go @@ -10,15 +10,15 @@ import ( type Base64 []byte var ( - _ encoding.TextMarshaler = (*Base64)(nil) + _ encoding.TextMarshaler = (Base64)(nil) _ encoding.TextUnmarshaler = (*Base64)(nil) ) // MarshalText implements encoding.TextMarshaler. -func (b *Base64) MarshalText() ([]byte, error) { - sz := base64.StdEncoding.EncodedLen(len(*b)) +func (b Base64) MarshalText() ([]byte, error) { + sz := base64.StdEncoding.EncodedLen(len(b)) out := make([]byte, sz) - base64.StdEncoding.Encode(out, *b) + base64.StdEncoding.Encode(out, b) return out, nil } diff --git a/config/auth_test.go b/config/auth_test.go index f16445782a..0b95e4def4 100644 --- a/config/auth_test.go +++ b/config/auth_test.go @@ -88,3 +88,18 @@ func TestAuthUnmarshal(t *testing.T) { } }) } + +func TestAuthMarshal(t *testing.T) { + want := `{"key":"ZGVhZGJlZWZkZWFkYmVlZg==","iss":["iss"]}` + in := config.AuthPSK{ + Key: []byte("deadbeefdeadbeef"), + Issuer: []string{"iss"}, + } + gotb, err := json.Marshal(in) + if err != nil { + t.Error(err) + } + if got := string(gotb); !cmp.Equal(got, want) { + t.Error(cmp.Diff(got, want)) + } +}