Skip to content

Commit

Permalink
debug: Allow type-specification of JSON output for cortex-debug
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Oct 30, 2023
1 parent ef72bde commit 779e212
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
34 changes: 28 additions & 6 deletions commands/debug/debug_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"encoding/json"
"regexp"
"strconv"
"strings"

"github.com/arduino/arduino-cli/arduino"
Expand Down Expand Up @@ -209,28 +210,49 @@ func getDebugProperties(req *rpc.GetDebugConfigRequest, pme *packagemanager.Expl
// my.indexed.array.2=third
//
// into the corresponding JSON arrays.
// If a key should be converted into a JSON specific type different from string ("boolean" or "number")
// it must be specified in the keyTypes arguments.
func convertToJsonMap(in *properties.Map) string {
// XXX: Maybe this method could be a good candidate for propertis.Map?

// Find the values that should be kept as is, and the indexed arrays
// that should be later converted into arrays.
arraysKeys := map[string]bool{}
stringKeys := []string{}
scalarKeys := []string{}
trailingNumberMatcher := regexp.MustCompile(`^(.*)\.[0-9]+$`)
for _, k := range in.Keys() {
match := trailingNumberMatcher.FindAllStringSubmatch(k, -1)
if len(match) > 0 && len(match[0]) > 1 {
arraysKeys[match[0][1]] = true
} else {
stringKeys = append(stringKeys, k)
scalarKeys = append(scalarKeys, k)
}
}

// Compose a map that can be later marshaled into JSON keeping
// the arrays where they are expected to be.
res := map[string]any{}
for _, k := range stringKeys {
res[k] = in.Get(k)
for _, k := range scalarKeys {
v := in.Get(k)
switch {
case strings.HasPrefix(v, "[boolean]"):
v = strings.TrimSpace(strings.TrimPrefix(v, "[boolean]"))
res[k] = strings.EqualFold(v, "true")
case strings.HasPrefix(v, "[number]"):
v = strings.TrimPrefix(v, "[number]")
if i, err := strconv.Atoi(v); err == nil {
res[k] = i
} else if f, err := strconv.ParseFloat(v, 64); err == nil {
res[k] = f
}
case strings.HasPrefix(v, "[object]"):
v = strings.TrimPrefix(v, "[object]")
var o interface{}
if err := json.Unmarshal([]byte(v), &o); err != nil {
continue // silently ignore error
}
res[k] = o
default:
res[k] = in.Get(k)
}
}
for k := range arraysKeys {
res[k] = in.ExtractSubIndexLists(k)
Expand Down
24 changes: 24 additions & 0 deletions docs/platform-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,30 @@ will result in the following JSON to be merged in the Arduino IDE generated `lau
}
```

All the values are converted by default to a string in the resulting JSON. If another type is needed the value can be
prefixed with the tags `[boolean]`, `[number]`, or `[object]` to force a specific type in the JSON, for example:

```
debug.cortex-debug.custom.aBoolean=[boolean]true
debug.cortex-debug.custom.anNumber=[number]10
debug.cortex-debug.custom.anotherNumber=[number]10.20
debug.cortex-debug.custom.anObject=[object]{"key":"value", "boolean":true}
```

will result in the following JSON:

```json
{
"aBoolean": true,
"anNumber": 10,
"anotherNumber": 10.2,
"anObject": {
"boolean": true,
"key": "value"
}
}
```

### Optimization level for debugging

The compiler optimization level that is appropriate for normal usage will often not provide a good experience while
Expand Down
18 changes: 18 additions & 0 deletions internal/integrationtest/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ func testAllDebugInformation(t *testing.T, env *integrationtest.Environment, cli
},
"svd_file": "svd-file",
"cortex-debug_custom_configuration": {
"aBoolean": true,
"aStringBoolean": "true",
"aStringNumber": "10",
"anNumber": 10,
"anotherNumber": 10.2,
"anObject": {
"boolean": true,
"key": "value"
},
"anotherStringParamer": "hellooo",
"overrideRestartCommands": [
"monitor reset halt",
Expand Down Expand Up @@ -176,6 +185,15 @@ func testAllDebugInformation(t *testing.T, env *integrationtest.Environment, cli
},
"svd_file": "svd-file",
"cortex-debug_custom_configuration": {
"aBoolean": true,
"aStringBoolean": "true",
"aStringNumber": "10",
"anNumber": 10,
"anotherNumber": 10.2,
"anObject": {
"boolean": true,
"key": "value"
},
"anotherStringParamer": "hellooo",
"overrideRestartCommands": [
"monitor reset halt",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ my.debug.cortex-debug.custom.overrideRestartCommands.1=monitor gdb_sync
my.debug.cortex-debug.custom.overrideRestartCommands.2=thb setup
my.debug.cortex-debug.custom.overrideRestartCommands.3=c
my.debug.cortex-debug.custom.anotherStringParamer=hellooo
my.debug.cortex-debug.custom.aBoolean=[boolean]true
my.debug.cortex-debug.custom.aStringBoolean=true
my.debug.cortex-debug.custom.anNumber=[number]10
my.debug.cortex-debug.custom.anotherNumber=[number]10.20
my.debug.cortex-debug.custom.aStringNumber=10
my.debug.cortex-debug.custom.anObject=[object]{"key":"value", "boolean":true}
my.debug.svd_file=svd-file

my2.name=My Cool Board
Expand Down

0 comments on commit 779e212

Please sign in to comment.