Skip to content

Commit 7025af1

Browse files
committed
handle comment str indices
1 parent 06e01f7 commit 7025af1

File tree

3 files changed

+146
-4
lines changed

3 files changed

+146
-4
lines changed

pdata/pprofile/profile.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,35 @@ import "fmt"
88
// switchDictionary updates the Profile, switching its indices from one
99
// dictionary to another.
1010
func (ms Profile) switchDictionary(src, dst ProfilesDictionary) error {
11+
for i, v := range ms.AttributeIndices().All() {
12+
if src.AttributeTable().Len() < int(v) {
13+
return fmt.Errorf("invalid attribute index %d", v)
14+
}
15+
16+
attr := src.AttributeTable().At(int(v))
17+
err := attr.switchDictionary(src, dst)
18+
if err != nil {
19+
return fmt.Errorf("couldn't switch dictionary for attribute %d: %w", i, err)
20+
}
21+
idx, err := SetAttribute(dst.AttributeTable(), attr)
22+
if err != nil {
23+
return fmt.Errorf("couldn't set attribute %d: %w", i, err)
24+
}
25+
ms.AttributeIndices().SetAt(i, idx)
26+
}
27+
28+
for i, v := range ms.CommentStrindices().All() {
29+
if src.StringTable().Len() < int(v) {
30+
return fmt.Errorf("invalid comment index %d", v)
31+
}
32+
33+
idx, err := SetString(dst.StringTable(), src.StringTable().At(int(v)))
34+
if err != nil {
35+
return fmt.Errorf("couldn't set key: %w", err)
36+
}
37+
ms.CommentStrindices().SetAt(i, idx)
38+
}
39+
1140
for i, v := range ms.Sample().All() {
1241
err := v.switchDictionary(src, dst)
1342
if err != nil {

pdata/pprofile/profile_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package pprofile
55

66
import (
7+
"errors"
78
"testing"
89

910
"github.com/stretchr/testify/assert"
@@ -34,6 +35,118 @@ func TestProfileSwitchDictionary(t *testing.T) {
3435
wantProfile: NewProfile(),
3536
wantDictionary: NewProfilesDictionary(),
3637
},
38+
{
39+
name: "with an existing attribute",
40+
profile: func() Profile {
41+
p := NewProfile()
42+
p.AttributeIndices().Append(1)
43+
return p
44+
}(),
45+
46+
src: func() ProfilesDictionary {
47+
d := NewProfilesDictionary()
48+
d.StringTable().Append("", "test")
49+
50+
d.AttributeTable().AppendEmpty()
51+
a := d.AttributeTable().AppendEmpty()
52+
a.SetKeyStrindex(1)
53+
54+
return d
55+
}(),
56+
dst: func() ProfilesDictionary {
57+
d := NewProfilesDictionary()
58+
d.StringTable().Append("", "foo")
59+
60+
d.AttributeTable().AppendEmpty()
61+
d.AttributeTable().AppendEmpty()
62+
return d
63+
}(),
64+
65+
wantProfile: func() Profile {
66+
p := NewProfile()
67+
p.AttributeIndices().Append(2)
68+
return p
69+
}(),
70+
wantDictionary: func() ProfilesDictionary {
71+
d := NewProfilesDictionary()
72+
d.StringTable().Append("", "foo", "test")
73+
74+
d.AttributeTable().AppendEmpty()
75+
d.AttributeTable().AppendEmpty()
76+
a := d.AttributeTable().AppendEmpty()
77+
a.SetKeyStrindex(2)
78+
return d
79+
}(),
80+
},
81+
{
82+
name: "with an attribute index that does not match anything",
83+
profile: func() Profile {
84+
p := NewProfile()
85+
p.AttributeIndices().Append(1)
86+
return p
87+
}(),
88+
89+
src: NewProfilesDictionary(),
90+
dst: NewProfilesDictionary(),
91+
92+
wantProfile: func() Profile {
93+
p := NewProfile()
94+
p.AttributeIndices().Append(1)
95+
return p
96+
}(),
97+
wantDictionary: NewProfilesDictionary(),
98+
wantErr: errors.New("invalid attribute index 1"),
99+
},
100+
{
101+
name: "with an existing comment",
102+
profile: func() Profile {
103+
p := NewProfile()
104+
p.CommentStrindices().Append(1)
105+
return p
106+
}(),
107+
108+
src: func() ProfilesDictionary {
109+
d := NewProfilesDictionary()
110+
d.StringTable().Append("", "test")
111+
return d
112+
}(),
113+
dst: func() ProfilesDictionary {
114+
d := NewProfilesDictionary()
115+
d.StringTable().Append("", "foo")
116+
return d
117+
}(),
118+
119+
wantProfile: func() Profile {
120+
p := NewProfile()
121+
p.CommentStrindices().Append(2)
122+
return p
123+
}(),
124+
wantDictionary: func() ProfilesDictionary {
125+
d := NewProfilesDictionary()
126+
d.StringTable().Append("", "foo", "test")
127+
128+
return d
129+
}(),
130+
},
131+
{
132+
name: "with a comment index that does not match anything",
133+
profile: func() Profile {
134+
p := NewProfile()
135+
p.CommentStrindices().Append(1)
136+
return p
137+
}(),
138+
139+
src: NewProfilesDictionary(),
140+
dst: NewProfilesDictionary(),
141+
142+
wantProfile: func() Profile {
143+
p := NewProfile()
144+
p.CommentStrindices().Append(1)
145+
return p
146+
}(),
147+
wantDictionary: NewProfilesDictionary(),
148+
wantErr: errors.New("invalid comment index 1"),
149+
},
37150
{
38151
name: "with a profile that has a sample",
39152
profile: func() Profile {

pdata/pprofile/sample_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ func TestSampleSwitchDictionary(t *testing.T) {
7979
}(),
8080
},
8181
{
82-
name: "with a link index that does not match anything",
82+
name: "with an attribute index that does not match anything",
8383
sample: func() Sample {
8484
s := NewSample()
85-
s.SetLinkIndex(1)
85+
s.AttributeIndices().Append(1)
8686
return s
8787
}(),
8888

@@ -91,11 +91,11 @@ func TestSampleSwitchDictionary(t *testing.T) {
9191

9292
wantSample: func() Sample {
9393
s := NewSample()
94-
s.SetLinkIndex(1)
94+
s.AttributeIndices().Append(1)
9595
return s
9696
}(),
9797
wantDictionary: NewProfilesDictionary(),
98-
wantErr: errors.New("invalid link index 1"),
98+
wantErr: errors.New("invalid attribute index 1"),
9999
},
100100
{
101101
name: "with an existing link",

0 commit comments

Comments
 (0)