httptypes: add MemberCreateRequest.MarshalJSON

release-2.0
Brian Waldon 2014-10-29 16:01:05 -07:00
parent e457d52f5c
commit 011a67c878
2 changed files with 33 additions and 0 deletions

View File

@ -33,6 +33,20 @@ type MemberCreateRequest struct {
PeerURLs types.URLs
}
func (m *MemberCreateRequest) MarshalJSON() ([]byte, error) {
s := struct {
PeerURLs []string `json:"peerURLs"`
}{
PeerURLs: make([]string, len(m.PeerURLs)),
}
for i, u := range m.PeerURLs {
s.PeerURLs[i] = u.String()
}
return json.Marshal(&s)
}
func (m *MemberCreateRequest) UnmarshalJSON(data []byte) error {
s := struct {
PeerURLs []string `json:"peerURLs"`

View File

@ -199,3 +199,22 @@ func TestMemberCreateRequestUnmarshalFail(t *testing.T) {
}
}
}
func TestMemberCreateRequestMarshal(t *testing.T) {
req := MemberCreateRequest{
PeerURLs: types.URLs([]url.URL{
url.URL{Scheme: "http", Host: "127.0.0.1:8081"},
url.URL{Scheme: "https", Host: "127.0.0.1:8080"},
}),
}
want := []byte(`{"peerURLs":["http://127.0.0.1:8081","https://127.0.0.1:8080"]}`)
got, err := json.Marshal(&req)
if err != nil {
t.Fatalf("Marshal returned unexpected err=%v", err)
}
if !reflect.DeepEqual(want, got) {
t.Fatalf("Failed to marshal MemberCreateRequest: want=%s, got=%s", want, got)
}
}