Skip to content

Commit e4121da

Browse files
authored
Merge pull request #5 from arduino/fix1
Added ExtractSubIndexLists method
2 parents 7e5a5ec + 78ca7bd commit e4121da

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

properties.go

+53
Original file line numberDiff line numberDiff line change
@@ -588,3 +588,56 @@ func (m *Map) ExtractSubIndexSets(root string) []*Map {
588588

589589
return res
590590
}
591+
592+
// ExtractSubIndexLists extracts a list of arguments from a root `root.N=...`.
593+
// For example the following Map:
594+
//
595+
// properties.Map{
596+
// "uno.discovery.required": "item",
597+
// "due.discovery.required.0": "item1",
598+
// "due.discovery.required.1": "item2",
599+
// "due.discovery.required.2": "item3",
600+
// "tre.discovery.required.1": "itemA",
601+
// "tre.discovery.required.2": "itemB",
602+
// "tre.discovery.required.3": "itemC",
603+
// }
604+
//
605+
// calling ExtractSubIndexLists("uno.discovery.required") returns the array:
606+
//
607+
// [ "item" ]
608+
//
609+
// calling ExtractSubIndexLists("due.discovery.required") returns the array:
610+
//
611+
// [ "item1", "item2", "item3" ]
612+
//
613+
// the sub-index may start with .1 too, so calling ExtractSubIndexLists("tre.discovery.required") returns:
614+
//
615+
// [ "itemA", "itemB", "itemC" ]
616+
//
617+
// Numeric subindex cannot be mixed with non-numeric, in that case only the numeric sub
618+
// index sets will be returned.
619+
func (m *Map) ExtractSubIndexLists(root string) []string {
620+
// First check the properties with numeric sub index "root.N.xxx"
621+
res := []string{}
622+
portIDPropsSet := m.SubTree(root)
623+
idx := 0
624+
haveIndexedProperties := false
625+
for {
626+
k := fmt.Sprintf("%d", idx)
627+
idx++
628+
if v, ok := portIDPropsSet.GetOk(k); ok {
629+
haveIndexedProperties = true
630+
res = append(res, v)
631+
} else if idx > 1 {
632+
// Always check sub-id 0 and 1 (https://github.com/arduino/arduino-cli/issues/456)
633+
break
634+
}
635+
}
636+
637+
// if there are no subindexed then return the whole "roox.xxx" subtree
638+
if !haveIndexedProperties {
639+
res = append(res, m.Get(root))
640+
}
641+
642+
return res
643+
}

properties_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,40 @@ func TestExtractSubIndexSets(t *testing.T) {
343343
require.Equal(t, s4[1].Get("vid"), "0x1003")
344344
require.Equal(t, s4[1].Get("pid"), "0x2003")
345345
}
346+
347+
func TestExtractSubIndexLists(t *testing.T) {
348+
data := map[string]string{
349+
"uno.discovery.required": "item",
350+
"due.discovery.required.0": "item1",
351+
"due.discovery.required.1": "item2",
352+
"due.discovery.required.2": "item3",
353+
"tre.discovery.required.1": "itemA",
354+
"tre.discovery.required.2": "itemB",
355+
"tre.discovery.required.3": "itemC",
356+
"quattro.discovery.required": "itemA",
357+
"quattro.discovery.required.1": "itemB",
358+
"quattro.discovery.required.2": "itemC",
359+
}
360+
m := NewFromHashmap(data)
361+
362+
s1 := m.ExtractSubIndexLists("uno.discovery.required")
363+
require.Len(t, s1, 1)
364+
require.Equal(t, s1[0], "item")
365+
366+
s2 := m.ExtractSubIndexLists("due.discovery.required")
367+
require.Len(t, s2, 3)
368+
require.Equal(t, s2[0], "item1")
369+
require.Equal(t, s2[1], "item2")
370+
require.Equal(t, s2[2], "item3")
371+
372+
s3 := m.ExtractSubIndexLists("tre.discovery.required")
373+
require.Len(t, s3, 3)
374+
require.Equal(t, s3[0], "itemA")
375+
require.Equal(t, s3[1], "itemB")
376+
require.Equal(t, s3[2], "itemC")
377+
378+
s4 := m.ExtractSubIndexLists("quattro.discovery.required")
379+
require.Len(t, s4, 2)
380+
require.Equal(t, s4[0], "itemB")
381+
require.Equal(t, s4[1], "itemC")
382+
}

0 commit comments

Comments
 (0)