Skip to content

Commit

Permalink
Merge pull request #32 from arduino/lists
Browse files Browse the repository at this point in the history
ExtractSubIndexLists now allows non-sequential elements
  • Loading branch information
cmaglie authored Oct 12, 2023
2 parents 87b718b + 348fa6a commit b9618c5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 33 deletions.
47 changes: 37 additions & 10 deletions properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ import (
"reflect"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"unicode/utf8"

Expand Down Expand Up @@ -630,6 +632,9 @@ func (m *Map) ExtractSubIndexSets(root string) []*Map {
// "tre.discovery.required.1": "itemA",
// "tre.discovery.required.2": "itemB",
// "tre.discovery.required.3": "itemC",
// "quattro.discovery.required.1": "itemA",
// "quattro.discovery.required.4": "itemB",
// "quattro.discovery.required.5": "itemC",
// }
//
// calling ExtractSubIndexLists("uno.discovery.required") returns the array:
Expand All @@ -644,23 +649,45 @@ func (m *Map) ExtractSubIndexSets(root string) []*Map {
//
// [ "itemA", "itemB", "itemC" ]
//
// also the list may contains holes, so calling ExtractSubIndexLists("quattro.discovery.required") returns:
//
// [ "itemA", "itemB", "itemC" ]
//
// Numeric subindex cannot be mixed with non-numeric, in that case only the numeric sub
// index sets will be returned.
func (m *Map) ExtractSubIndexLists(root string) []string {
// First check the properties with numeric sub index "root.N.xxx"
isNotDigit := func(in string) bool {
for _, r := range in {
if r < '0' || r > '9' {
return true
}
}
return false
}

// Extract numeric keys
subProps := m.SubTree(root)
indexes := []int{}
for _, key := range subProps.o {
if isNotDigit(key) {
continue
}
if idx, err := strconv.Atoi(key); err == nil {
indexes = append(indexes, idx)
}
}
sort.Ints(indexes)

res := []string{}
portIDPropsSet := m.SubTree(root)
idx := 0
haveIndexedProperties := false
for {
k := fmt.Sprintf("%d", idx)
idx++
if v, ok := portIDPropsSet.GetOk(k); ok {
for i, idx := range indexes {
if i > 0 && idx == indexes[i-1] {
// de-duplicate cases like "05" and "5"
continue
}
if v, ok := subProps.GetOk(strconv.Itoa(idx)); ok {
haveIndexedProperties = true
res = append(res, v)
} else if idx > 1 {
// Always check sub-id 0 and 1 (https://github.com/arduino/arduino-cli/issues/456)
break
}
}

Expand Down
38 changes: 15 additions & 23 deletions properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,32 +357,24 @@ func TestExtractSubIndexLists(t *testing.T) {
"quattro.discovery.required.1": "itemB",
"quattro.discovery.required.2": "itemC",
"cinque.discovery.something": "itemX",
"sei.discovery.something.1": "itemA",
"sei.discovery.something.2": "itemB",
"sei.discovery.something.5": "itemC",
"sei.discovery.something.12": "itemD",
"sette.discovery.something.01": "itemA",
"sette.discovery.something.2": "itemB",
"sette.discovery.something.05": "itemC",
"sette.discovery.something.5": "itemD",
}
m := NewFromHashmap(data)

s1 := m.ExtractSubIndexLists("uno.discovery.required")
require.Len(t, s1, 1)
require.Equal(t, s1[0], "item")

s2 := m.ExtractSubIndexLists("due.discovery.required")
require.Len(t, s2, 3)
require.Equal(t, s2[0], "item1")
require.Equal(t, s2[1], "item2")
require.Equal(t, s2[2], "item3")

s3 := m.ExtractSubIndexLists("tre.discovery.required")
require.Len(t, s3, 3)
require.Equal(t, s3[0], "itemA")
require.Equal(t, s3[1], "itemB")
require.Equal(t, s3[2], "itemC")

s4 := m.ExtractSubIndexLists("quattro.discovery.required")
require.Len(t, s4, 2)
require.Equal(t, s4[0], "itemB")
require.Equal(t, s4[1], "itemC")

s5 := m.ExtractSubIndexLists("cinque.discovery.required")
require.Len(t, s5, 0)
require.Equal(t, []string{"item"}, m.ExtractSubIndexLists("uno.discovery.required"))
require.Equal(t, []string{"item1", "item2", "item3"}, m.ExtractSubIndexLists("due.discovery.required"))
require.Equal(t, []string{"itemA", "itemB", "itemC"}, m.ExtractSubIndexLists("tre.discovery.required"))
require.Equal(t, []string{"itemB", "itemC"}, m.ExtractSubIndexLists("quattro.discovery.required"))
require.Equal(t, []string{}, m.ExtractSubIndexLists("cinque.discovery.required"))
require.Equal(t, []string{"itemA", "itemB", "itemC", "itemD"}, m.ExtractSubIndexLists("sei.discovery.something"))
require.Equal(t, []string{"itemB", "itemD"}, m.ExtractSubIndexLists("sette.discovery.something"))
}

func TestLoadingNonUTF8Properties(t *testing.T) {
Expand Down

0 comments on commit b9618c5

Please sign in to comment.