-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: bwplotka <[email protected]>
- Loading branch information
Showing
12 changed files
with
238 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package fastdto | ||
|
||
import ( | ||
dto "github.com/prometheus/client_model/go" | ||
) | ||
|
||
type LabelPair struct { | ||
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"` | ||
Value string `protobuf:"bytes,2,opt,name=value" json:"value,omitempty"` | ||
} | ||
|
||
func (p LabelPair) GetName() string { | ||
return p.Name | ||
} | ||
|
||
func (p LabelPair) GetValue() string { | ||
return p.Value | ||
} | ||
|
||
// LabelPairSorter implements sort.Interface. It is used to sort a slice of | ||
// LabelPairs | ||
type LabelPairSorter []LabelPair | ||
|
||
func (s LabelPairSorter) Len() int { | ||
return len(s) | ||
} | ||
|
||
func (s LabelPairSorter) Swap(i, j int) { | ||
s[i], s[j] = s[j], s[i] | ||
} | ||
|
||
func (s LabelPairSorter) Less(i, j int) bool { | ||
return s[i].Name < s[j].Name | ||
} | ||
|
||
func ToDTOLabelPair(in []LabelPair) []*dto.LabelPair { | ||
ret := make([]*dto.LabelPair, len(in)) | ||
for i := range in { | ||
ret[i] = &dto.LabelPair{ | ||
Name: &(in[i].Name), | ||
Value: &(in[i].Value), | ||
} | ||
} | ||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package fastdto | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
dto "github.com/prometheus/client_model/go" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
func BenchmarkToDTOLabelPairs(b *testing.B) { | ||
test := []LabelPair{ | ||
{"foo", "bar"}, | ||
{"foo2", "bar2"}, | ||
{"foo3", "bar3"}, | ||
} | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
_ = ToDTOLabelPair(test) | ||
} | ||
} | ||
|
||
func TestLabelPairSorter(t *testing.T) { | ||
test := []LabelPair{ | ||
{"foo3", "bar3"}, | ||
{"foo", "bar"}, | ||
{"foo2", "bar2"}, | ||
} | ||
sort.Sort(LabelPairSorter(test)) | ||
|
||
expected := []LabelPair{ | ||
{"foo", "bar"}, | ||
{"foo2", "bar2"}, | ||
{"foo3", "bar3"}, | ||
} | ||
if diff := cmp.Diff(test, expected); diff != "" { | ||
t.Fatal(diff) | ||
} | ||
} | ||
|
||
func TestToDTOLabelPair(t *testing.T) { | ||
test := []LabelPair{ | ||
{"foo", "bar"}, | ||
{"foo2", "bar2"}, | ||
{"foo3", "bar3"}, | ||
} | ||
expected := []*dto.LabelPair{ | ||
{Name: proto.String("foo"), Value: proto.String("bar")}, | ||
{Name: proto.String("foo2"), Value: proto.String("bar2")}, | ||
{Name: proto.String("foo3"), Value: proto.String("bar3")}, | ||
} | ||
if diff := cmp.Diff(ToDTOLabelPair(test), expected, cmpopts.IgnoreUnexported(dto.LabelPair{})); diff != "" { | ||
t.Fatal(diff) | ||
} | ||
} |
Oops, something went wrong.