forked from ooni/probe-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
experimentbuilder.go
165 lines (146 loc) · 4.55 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
package engine
import (
"errors"
"fmt"
"reflect"
"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 (
// InputRequired indicates that the experiment requires
// external input to run. If this input is not provided to
// the experiment, it will not know what to do.
InputRequired = InputPolicy("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
}
// 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
}