Skip to content

Commit

Permalink
config: MarshalText on value receiver
Browse files Browse the repository at this point in the history
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 <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Jan 24, 2022
1 parent a8c7ebe commit d215220
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions config/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
15 changes: 15 additions & 0 deletions config/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

0 comments on commit d215220

Please sign in to comment.