Skip to content

Commit

Permalink
Merge pull request #5 from arduino/fix1
Browse files Browse the repository at this point in the history
Added ExtractSubIndexLists method
  • Loading branch information
cmaglie authored Jun 23, 2021
2 parents 7e5a5ec + 78ca7bd commit e4121da
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
53 changes: 53 additions & 0 deletions properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,56 @@ func (m *Map) ExtractSubIndexSets(root string) []*Map {

return res
}

// ExtractSubIndexLists extracts a list of arguments from a root `root.N=...`.
// For example the following Map:
//
// properties.Map{
// "uno.discovery.required": "item",
// "due.discovery.required.0": "item1",
// "due.discovery.required.1": "item2",
// "due.discovery.required.2": "item3",
// "tre.discovery.required.1": "itemA",
// "tre.discovery.required.2": "itemB",
// "tre.discovery.required.3": "itemC",
// }
//
// calling ExtractSubIndexLists("uno.discovery.required") returns the array:
//
// [ "item" ]
//
// calling ExtractSubIndexLists("due.discovery.required") returns the array:
//
// [ "item1", "item2", "item3" ]
//
// the sub-index may start with .1 too, so calling ExtractSubIndexLists("tre.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"
res := []string{}
portIDPropsSet := m.SubTree(root)
idx := 0
haveIndexedProperties := false
for {
k := fmt.Sprintf("%d", idx)
idx++
if v, ok := portIDPropsSet.GetOk(k); 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
}
}

// if there are no subindexed then return the whole "roox.xxx" subtree
if !haveIndexedProperties {
res = append(res, m.Get(root))
}

return res
}
37 changes: 37 additions & 0 deletions properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,40 @@ func TestExtractSubIndexSets(t *testing.T) {
require.Equal(t, s4[1].Get("vid"), "0x1003")
require.Equal(t, s4[1].Get("pid"), "0x2003")
}

func TestExtractSubIndexLists(t *testing.T) {
data := map[string]string{
"uno.discovery.required": "item",
"due.discovery.required.0": "item1",
"due.discovery.required.1": "item2",
"due.discovery.required.2": "item3",
"tre.discovery.required.1": "itemA",
"tre.discovery.required.2": "itemB",
"tre.discovery.required.3": "itemC",
"quattro.discovery.required": "itemA",
"quattro.discovery.required.1": "itemB",
"quattro.discovery.required.2": "itemC",
}
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")
}

0 comments on commit e4121da

Please sign in to comment.