Browse Source

httptypes: add MemberCreateRequest.MarshalJSON

Brian Waldon 11 years ago
parent
commit
011a67c878

+ 14 - 0
etcdserver/etcdhttp/httptypes/member.go

@@ -33,6 +33,20 @@ type MemberCreateRequest struct {
 	PeerURLs types.URLs
 	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 {
 func (m *MemberCreateRequest) UnmarshalJSON(data []byte) error {
 	s := struct {
 	s := struct {
 		PeerURLs []string `json:"peerURLs"`
 		PeerURLs []string `json:"peerURLs"`

+ 19 - 0
etcdserver/etcdhttp/httptypes/member_test.go

@@ -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)
+	}
+}