Fix: handle nil variableLabels in Desc.String() method and add tests for nil label values (#1687)
Fixes #1684 Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
This commit is contained in:
parent
2356bb3e0b
commit
1880f1bc33
|
@ -189,12 +189,15 @@ func (d *Desc) String() string {
|
||||||
fmt.Sprintf("%s=%q", lp.GetName(), lp.GetValue()),
|
fmt.Sprintf("%s=%q", lp.GetName(), lp.GetValue()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
vlStrings := make([]string, 0, len(d.variableLabels.names))
|
vlStrings := []string{}
|
||||||
for _, vl := range d.variableLabels.names {
|
if d.variableLabels != nil {
|
||||||
if fn, ok := d.variableLabels.labelConstraints[vl]; ok && fn != nil {
|
vlStrings = make([]string, 0, len(d.variableLabels.names))
|
||||||
vlStrings = append(vlStrings, fmt.Sprintf("c(%s)", vl))
|
for _, vl := range d.variableLabels.names {
|
||||||
} else {
|
if fn, ok := d.variableLabels.labelConstraints[vl]; ok && fn != nil {
|
||||||
vlStrings = append(vlStrings, vl)
|
vlStrings = append(vlStrings, fmt.Sprintf("c(%s)", vl))
|
||||||
|
} else {
|
||||||
|
vlStrings = append(vlStrings, vl)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
|
|
|
@ -28,3 +28,36 @@ func TestNewDescInvalidLabelValues(t *testing.T) {
|
||||||
t.Errorf("NewDesc: expected error because: %s", desc.err)
|
t.Errorf("NewDesc: expected error because: %s", desc.err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewDescNilLabelValues(t *testing.T) {
|
||||||
|
desc := NewDesc(
|
||||||
|
"sample_label",
|
||||||
|
"sample label",
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
if desc.err != nil {
|
||||||
|
t.Errorf("NewDesc: unexpected error: %s", desc.err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewDescWithNilLabelValues_String(t *testing.T) {
|
||||||
|
desc := NewDesc(
|
||||||
|
"sample_label",
|
||||||
|
"sample label",
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
if desc.String() != `Desc{fqName: "sample_label", help: "sample label", constLabels: {}, variableLabels: {}}` {
|
||||||
|
t.Errorf("String: unexpected output: %s", desc.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewInvalidDesc_String(t *testing.T) {
|
||||||
|
desc := NewInvalidDesc(
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
if desc.String() != `Desc{fqName: "", help: "", constLabels: {}, variableLabels: {}}` {
|
||||||
|
t.Errorf("String: unexpected output: %s", desc.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue