forked from mirror/client_golang
42 lines
898 B
Go
42 lines
898 B
Go
![]() |
// Copyright (c) 2013, Matt T. Proud
|
||
|
// All rights reserved.
|
||
|
//
|
||
|
// Use of this source code is governed by a BSD-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package utility
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"sort"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
delimiter = "|"
|
||
|
)
|
||
|
|
||
|
// LabelsToSignature provides a way of building a unique signature
|
||
|
// (i.e., fingerprint) for a given label set sequence.
|
||
|
func LabelsToSignature(labels map[string]string) string {
|
||
|
// TODO(matt): This is a wart, and we'll want to validate that collisions
|
||
|
// do not occur in less-than-diligent environments.
|
||
|
cardinality := len(labels)
|
||
|
keys := make([]string, 0, cardinality)
|
||
|
|
||
|
for label := range labels {
|
||
|
keys = append(keys, label)
|
||
|
}
|
||
|
|
||
|
sort.Strings(keys)
|
||
|
|
||
|
buffer := bytes.Buffer{}
|
||
|
|
||
|
for _, label := range keys {
|
||
|
buffer.WriteString(label)
|
||
|
buffer.WriteString(delimiter)
|
||
|
buffer.WriteString(labels[label])
|
||
|
}
|
||
|
|
||
|
return buffer.String()
|
||
|
}
|