Update encoding to be backwards compatible.

This commit is contained in:
Asim 2016-03-29 17:44:46 +01:00
parent e941796234
commit 61178d1b45
3 changed files with 20 additions and 6 deletions

View File

@ -108,7 +108,7 @@ func (c *consulRegistry) Register(s *Service, opts ...RegisterOption) error {
tags := encodeMetadata(node.Metadata)
tags = append(tags, encodeEndpoints(s.Endpoints)...)
tags = append(tags, encodeVersion(s.Version))
tags = append(tags, encodeVersion(s.Version)...)
var check *consul.AgentServiceCheck

View File

@ -45,6 +45,10 @@ func encodeEndpoints(en []*Endpoint) []string {
var tags []string
for _, e := range en {
if b, err := json.Marshal(e); err == nil {
// old encoding
// TODO: remove in 09/2016
tags = append(tags, "e="+string(b))
// new encoding
tags = append(tags, "e-"+encode(b))
}
}
@ -84,6 +88,10 @@ func encodeMetadata(md map[string]string) []string {
if b, err := json.Marshal(map[string]string{
k: v,
}); err == nil {
// old encoding
// TODO: remove in 09/2016
tags = append(tags, "t="+string(b))
// new encoding
tags = append(tags, "t-"+encode(b))
}
}
@ -120,8 +128,14 @@ func decodeMetadata(tags []string) map[string]string {
return md
}
func encodeVersion(v string) string {
return "v-" + encode([]byte(v))
func encodeVersion(v string) []string {
return []string{
// old encoding,
// TODO: remove in 09/2016
"v=" + v,
// new encoding,
"v-" + encode([]byte(v)),
}
}
func decodeVersion(tags []string) (string, bool) {

View File

@ -33,7 +33,7 @@ func TestEncodingEndpoints(t *testing.T) {
for _, data := range testData {
e := encodeEndpoints([]*Endpoint{data.decoded})
if len(e) != 1 || e[0] != data.encoded {
if len(e) != 2 || e[1] != data.encoded {
t.Fatalf("Expected %s got %s", data.encoded, e)
}
@ -82,11 +82,11 @@ func TestEncodingVersion(t *testing.T) {
for _, data := range testData {
e := encodeVersion(data.decoded)
if e != data.encoded {
if e[1] != data.encoded {
t.Fatalf("Expected %s got %s", data.encoded, e)
}
d, ok := decodeVersion([]string{e})
d, ok := decodeVersion(e)
if !ok {
t.Fatal("Unexpected %t for %s", ok, data.encoded)
}