refactor(engine): set options from any value (#837)

This diff refactors how we set options for experiments to accept
in input an any value or a map[string]any, depending on which method
we choose to actually set options.

There should be no functional change, except that now we're not
guessing the type and then attempting to set the value of the selected
field: now, instead, we match the provided type and the field's type
as part of the same function (i.e., SetOptionAny).

This diff is functional to https://github.com/ooni/probe/issues/2184,
because it will allow us to load options from a map[string]any,
which will be part of the OONI Run v2 JSON descriptor.

If we didn't apply this change, we would only have been to set options
from a map[string]string, which is good enough as a solution for the
CLI but is definitely clumsy when you have to write stuff like:

```JSON
{
  "options": {
    "HTTP3Enabled": "true"
  }
}
```

when you could instead more naturally write:

```JSON
{
  "options": {
    "HTTP3Enabled": true
  }
}
```
This commit is contained in:
Simone Basso
2022-07-08 11:51:59 +02:00
committed by GitHub
parent 6019b25baf
commit 086ae43b15
4 changed files with 416 additions and 199 deletions
+18 -8
View File
@@ -176,7 +176,7 @@ func warnOnError(err error, msg string) {
}
}
func mustMakeMap(input []string) (output map[string]string) {
func mustMakeMapString(input []string) (output map[string]string) {
output = make(map[string]string)
for _, opt := range input {
key, value, err := split(opt)
@@ -186,6 +186,16 @@ func mustMakeMap(input []string) (output map[string]string) {
return
}
func mustMakeMapAny(input []string) (output map[string]any) {
output = make(map[string]any)
for _, opt := range input {
key, value, err := split(opt)
fatalOnError(err, "cannot split key-value pair")
output[key] = value
}
return
}
func mustParseURL(URL string) *url.URL {
rv, err := url.Parse(URL)
fatalOnError(err, "cannot parse URL")
@@ -233,15 +243,15 @@ Do you consent to OONI Probe data collection?
OONI Probe collects evidence of internet censorship and measures
network performance:
- OONI Probe will likely test objectionable sites and services;
- Anyone monitoring your internet activity (such as a government
or Internet provider) may be able to tell that you are using OONI Probe;
- The network data you collect will be published automatically
unless you use miniooni's -n command line flag.
To learn more, see https://ooni.org/about/risks/.
If you're onboard, re-run the same command and add the --yes flag, to
@@ -296,8 +306,8 @@ func MainWithConfiguration(experimentName string, currentOptions Options) {
ctx := context.Background()
extraOptions := mustMakeMap(currentOptions.ExtraOptions)
annotations := mustMakeMap(currentOptions.Annotations)
extraOptions := mustMakeMapAny(currentOptions.ExtraOptions)
annotations := mustMakeMapString(currentOptions.Annotations)
logger := &log.Logger{Level: log.InfoLevel, Handler: &logHandler{Writer: os.Stderr}}
if currentOptions.Verbose {
@@ -413,7 +423,7 @@ func MainWithConfiguration(experimentName string, currentOptions Options) {
})
}
err = builder.SetOptionsGuessType(extraOptions)
err = builder.SetOptionsAny(extraOptions)
fatalOnError(err, "cannot parse extraOptions")
experiment := builder.NewExperiment()