-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
189 lines (165 loc) · 4.09 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package gendoc
import (
"fmt"
"go/ast"
"path"
"reflect"
"strconv"
"strings"
"github.com/hyperledger/fabric/common/metrics"
"golang.org/x/tools/go/packages"
)
// Options scans the provided list of packages for options structs used when
// creating metrics and returns instances that are recreated from the source
// tree.
func Options(pkgs []*packages.Package) ([]interface{}, error) {
var options []interface{}
for _, p := range pkgs {
for _, f := range p.Syntax {
opts, err := FileOptions(f)
if err != nil {
return nil, err
}
options = append(options, opts...)
}
}
return options, nil
}
// FileOptions walks the specified ast.File for options structs used when
// creating metrics and returns instances that are recreated from the source.
func FileOptions(f *ast.File) ([]interface{}, error) {
var imports = walkImports(f)
var options []interface{}
var errors []error
// If the file contains gendoc:ignore, ignore the file
for _, c := range f.Comments {
if strings.Index(c.Text(), "gendoc:ignore") != -1 {
return nil, nil
}
}
// Iterate over declarations
for i := range f.Decls {
ast.Inspect(f.Decls[i], func(x ast.Node) bool {
node, ok := x.(*ast.ValueSpec)
if !ok {
return true
}
for _, v := range node.Values {
value, ok := v.(*ast.CompositeLit)
if !ok {
continue
}
literalType, ok := value.Type.(*ast.SelectorExpr)
if !ok {
continue
}
ident, ok := literalType.X.(*ast.Ident)
if !ok {
continue
}
if imports[ident.Name] != "github.com/hyperledger/fabric/common/metrics" {
continue
}
option, err := createOption(literalType)
if err != nil {
errors = append(errors, err)
break
}
option, err = populateOption(value, option)
if err != nil {
errors = append(errors, err)
break
}
options = append(options, option)
}
return false
})
}
if len(errors) != 0 {
return nil, errors[0]
}
return options, nil
}
func walkImports(f *ast.File) map[string]string {
imports := map[string]string{}
for i := range f.Imports {
ast.Inspect(f.Imports[i], func(x ast.Node) bool {
switch node := x.(type) {
case *ast.ImportSpec:
importPath, err := strconv.Unquote(node.Path.Value)
if err != nil {
panic(err)
}
importName := path.Base(importPath)
if node.Name != nil {
importName = node.Name.Name
}
imports[importName] = importPath
return false
default:
return true
}
})
}
return imports
}
func createOption(lit *ast.SelectorExpr) (interface{}, error) {
optionName := lit.Sel.Name
switch optionName {
case "CounterOpts":
return &metrics.CounterOpts{}, nil
case "GaugeOpts":
return &metrics.GaugeOpts{}, nil
case "HistogramOpts":
return &metrics.HistogramOpts{}, nil
default:
return nil, fmt.Errorf("unknown object type: %s", optionName)
}
}
func populateOption(lit *ast.CompositeLit, target interface{}) (interface{}, error) {
val := reflect.ValueOf(target).Elem()
for _, elem := range lit.Elts {
if kv, ok := elem.(*ast.KeyValueExpr); ok {
name := kv.Key.(*ast.Ident).Name
field := val.FieldByName(name)
switch name {
// ignored
case "Buckets":
// slice of strings
case "LabelNames":
labelNames, err := stringSlice(kv.Value.(*ast.CompositeLit))
if err != nil {
return nil, err
}
labelNamesValue := reflect.ValueOf(labelNames)
field.Set(labelNamesValue)
// simple strings
case "Namespace", "Subsystem", "Name", "Help", "StatsdFormat":
basicVal := kv.Value.(*ast.BasicLit)
val, err := strconv.Unquote(basicVal.Value)
if err != nil {
return nil, err
}
field.SetString(val)
default:
return nil, fmt.Errorf("unknown field name: %s", name)
}
}
}
return val.Interface(), nil
}
func stringSlice(lit *ast.CompositeLit) ([]string, error) {
var slice []string
for _, elem := range lit.Elts {
val, err := strconv.Unquote(elem.(*ast.BasicLit).Value)
if err != nil {
return nil, err
}
slice = append(slice, val)
}
return slice, nil
}