forked from ooni/probe-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
experimentbuilder.go
203 lines (180 loc) · 5.94 KB
/
experimentbuilder.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package engine
import (
"errors"
"fmt"
"reflect"
"regexp"
"strconv"
"github.com/iancoleman/strcase"
"github.com/ooni/probe-engine/model"
)
// InputPolicy describes the experiment policy with respect to input. That is
// whether it requires input, optionally accepts input, does not want input.
type InputPolicy string
const (
// InputOrQueryTestLists indicates that the experiment requires
// external input to run and that this kind of input is URLs
// from the citizenlab/test-lists repository. If this input
// not provided to the experiment, then the code that runs the
// experiment is supposed to fetch from URLs from OONI's backends.
InputOrQueryTestLists = InputPolicy("or_query_test_lists")
// InputStrictlyRequired indicates that the experiment
// requires input and we currently don't have an API for
// fetching such input. Therefore, either the user specifies
// input or the experiment will fail for the lack of input.
InputStrictlyRequired = InputPolicy("strictly_required")
// InputOptional indicates that the experiment handles input,
// if any; otherwise it fetchs input/uses a default.
InputOptional = InputPolicy("optional")
// InputNone indicates that the experiment does not want any
// input and ignores the input if provided with it.
InputNone = InputPolicy("none")
)
// ExperimentBuilder is an experiment builder.
type ExperimentBuilder struct {
build func(interface{}) *Experiment
callbacks model.ExperimentCallbacks
config interface{}
inputPolicy InputPolicy
interruptible bool
}
// Interruptible tells you whether this is an interruptible experiment. This kind
// of experiments (e.g. ndt7) may be interrupted mid way.
func (b *ExperimentBuilder) Interruptible() bool {
return b.interruptible
}
// InputPolicy returns the experiment input policy
func (b *ExperimentBuilder) InputPolicy() InputPolicy {
return b.inputPolicy
}
// OptionInfo contains info about an option
type OptionInfo struct {
Doc string
Type string
}
// Options returns info about all options
func (b *ExperimentBuilder) Options() (map[string]OptionInfo, error) {
result := make(map[string]OptionInfo)
ptrinfo := reflect.ValueOf(b.config)
if ptrinfo.Kind() != reflect.Ptr {
return nil, errors.New("config is not a pointer")
}
structinfo := ptrinfo.Elem().Type()
if structinfo.Kind() != reflect.Struct {
return nil, errors.New("config is not a struct")
}
for i := 0; i < structinfo.NumField(); i++ {
field := structinfo.Field(i)
result[field.Name] = OptionInfo{
Doc: field.Tag.Get("ooni"),
Type: field.Type.String(),
}
}
return result, nil
}
// SetOptionBool sets a bool option
func (b *ExperimentBuilder) SetOptionBool(key string, value bool) error {
field, err := fieldbyname(b.config, key)
if err != nil {
return err
}
if field.Kind() != reflect.Bool {
return errors.New("field is not a bool")
}
field.SetBool(value)
return nil
}
// SetOptionInt sets an int option
func (b *ExperimentBuilder) SetOptionInt(key string, value int64) error {
field, err := fieldbyname(b.config, key)
if err != nil {
return err
}
if field.Kind() != reflect.Int64 {
return errors.New("field is not an int64")
}
field.SetInt(value)
return nil
}
// SetOptionString sets a string option
func (b *ExperimentBuilder) SetOptionString(key, value string) error {
field, err := fieldbyname(b.config, key)
if err != nil {
return err
}
if field.Kind() != reflect.String {
return errors.New("field is not a string")
}
field.SetString(value)
return nil
}
var intregexp = regexp.MustCompile("^[0-9]+$")
// SetOptionGuessType sets an option whose type depends on the
// option value. If the value is `"true"` or `"false"` we
// assume the option is boolean. If the value is numeric, then we
// set an integer option. Otherwise we set a string option.
func (b *ExperimentBuilder) SetOptionGuessType(key, value string) error {
if value == "true" || value == "false" {
return b.SetOptionBool(key, value == "true")
}
if !intregexp.MatchString(value) {
return b.SetOptionString(key, value)
}
number, _ := strconv.ParseInt(value, 10, 64)
return b.SetOptionInt(key, number)
}
// SetOptionsGuessType calls the SetOptionGuessType method for every
// key, value pair contained by the opts input map.
func (b *ExperimentBuilder) SetOptionsGuessType(opts map[string]string) error {
for k, v := range opts {
if err := b.SetOptionGuessType(k, v); err != nil {
return err
}
}
return nil
}
// SetCallbacks sets the interactive callbacks
func (b *ExperimentBuilder) SetCallbacks(callbacks model.ExperimentCallbacks) {
b.callbacks = callbacks
}
func fieldbyname(v interface{}, key string) (reflect.Value, error) {
// See https://stackoverflow.com/a/6396678/4354461
ptrinfo := reflect.ValueOf(v)
if ptrinfo.Kind() != reflect.Ptr {
return reflect.Value{}, errors.New("value is not a pointer")
}
structinfo := ptrinfo.Elem()
if structinfo.Kind() != reflect.Struct {
return reflect.Value{}, errors.New("value is not a pointer to struct")
}
field := structinfo.FieldByName(key)
if !field.IsValid() || !field.CanSet() {
return reflect.Value{}, errors.New("no such field")
}
return field, nil
}
// NewExperiment creates the experiment
func (b *ExperimentBuilder) NewExperiment() *Experiment {
experiment := b.build(b.config)
experiment.callbacks = b.callbacks
return experiment
}
// canonicalizeExperimentName allows code to provide experiment names
// in a more flexible way, where we have aliases.
func canonicalizeExperimentName(name string) string {
switch name = strcase.ToSnake(name); name {
case "ndt_7":
name = "ndt" // since 2020-03-18, we use ndt7 to implement ndt by default
default:
}
return name
}
func newExperimentBuilder(session *Session, name string) (*ExperimentBuilder, error) {
factory, _ := experimentsByName[canonicalizeExperimentName(name)]
if factory == nil {
return nil, fmt.Errorf("no such experiment: %s", name)
}
builder := factory(session)
builder.callbacks = model.NewPrinterCallbacks(session.Logger())
return builder, nil
}