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
commit 086ae43b15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 409 additions and 192 deletions

View file

@ -157,7 +157,7 @@ func TestSetCallbacks(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if err := builder.SetOptionInt("SleepTime", 0); err != nil {
if err := builder.SetOptionAny("SleepTime", 0); err != nil {
t.Fatal(err)
}
register := &registerCallbacksCalled{}
@ -203,7 +203,7 @@ func TestMeasurementFailure(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if err := builder.SetOptionBool("ReturnError", true); err != nil {
if err := builder.SetOptionAny("ReturnError", true); err != nil {
t.Fatal(err)
}
measurement, err := builder.NewExperiment().Measure("")
@ -279,13 +279,13 @@ func TestUseOptions(t *testing.T) {
if !sleepTime {
t.Fatal("did not find SleepTime option")
}
if err := builder.SetOptionBool("ReturnError", true); err != nil {
if err := builder.SetOptionAny("ReturnError", true); err != nil {
t.Fatal("cannot set ReturnError field")
}
if err := builder.SetOptionInt("SleepTime", 10); err != nil {
if err := builder.SetOptionAny("SleepTime", 10); err != nil {
t.Fatal("cannot set SleepTime field")
}
if err := builder.SetOptionString("Message", "antani"); err != nil {
if err := builder.SetOptionAny("Message", "antani"); err != nil {
t.Fatal("cannot set Message field")
}
config := builder.config.(*example.Config)