1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package profile
16
17 import (
18 "fmt"
19 "strconv"
20 "strings"
21 )
22
23
24
25
26 func (p *Profile) SampleIndexByName(sampleIndex string) (int, error) {
27 if sampleIndex == "" {
28 if dst := p.DefaultSampleType; dst != "" {
29 for i, t := range sampleTypes(p) {
30 if t == dst {
31 return i, nil
32 }
33 }
34 }
35
36 return len(p.SampleType) - 1, nil
37 }
38 if i, err := strconv.Atoi(sampleIndex); err == nil {
39 if i < 0 || i >= len(p.SampleType) {
40 return 0, fmt.Errorf("sample_index %s is outside the range [0..%d]", sampleIndex, len(p.SampleType)-1)
41 }
42 return i, nil
43 }
44
45
46
47
48 noInuse := strings.TrimPrefix(sampleIndex, "inuse_")
49 for i, t := range p.SampleType {
50 if t.Type == sampleIndex || t.Type == noInuse {
51 return i, nil
52 }
53 }
54
55 return 0, fmt.Errorf("sample_index %q must be one of: %v", sampleIndex, sampleTypes(p))
56 }
57
58 func sampleTypes(p *Profile) []string {
59 types := make([]string, len(p.SampleType))
60 for i, t := range p.SampleType {
61 types[i] = t.Type
62 }
63 return types
64 }
65
View as plain text