Skip to content

Commit 7e5a5ec

Browse files
authored
Merge pull request #4 from arduino/fix1
Added ExtractSubIndexSets method
2 parents 496b294 + 72a5d84 commit 7e5a5ec

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

properties.go

+81
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,84 @@ func DeleteUnexpandedPropsFromString(str string) string {
507507
rxp := regexp.MustCompile("\\{.+?\\}")
508508
return rxp.ReplaceAllString(str, "")
509509
}
510+
511+
// ExtractSubIndexSets works like SubTree but it considers also the numeric sub index in the form
512+
// `root.N.xxx...` as separate subsets.
513+
// For example the following Map:
514+
//
515+
// properties.Map{
516+
// "uno.upload_port.vid": "0x1000",
517+
// "uno.upload_port.pid": "0x2000",
518+
// "due.upload_port.0.vid": "0x1000",
519+
// "due.upload_port.0.pid": "0x2000",
520+
// "due.upload_port.1.vid": "0x1001",
521+
// "due.upload_port.1.pid": "0x2001",
522+
// "tre.upload_port.1.vid": "0x1001",
523+
// "tre.upload_port.1.pid": "0x2001",
524+
// "tre.upload_port.2.vid": "0x1002",
525+
// "tre.upload_port.2.pid": "0x2002",
526+
// }
527+
//
528+
// calling ExtractSubIndexSets("uno.upload_port") returns the array:
529+
//
530+
// [ properties.Map{
531+
// "vid": "0x1000",
532+
// "pid": "0x2000",
533+
// },
534+
// ]
535+
//
536+
// calling ExtractSubIndexSets("due.upload_port") returns the array:
537+
//
538+
// [ properties.Map{
539+
// "vid": "0x1000",
540+
// "pid": "0x2000",
541+
// },
542+
// properties.Map{
543+
// "vid": "0x1001",
544+
// "pid": "0x2001",
545+
// },
546+
// ]
547+
//
548+
// the sub-index may start with .1 too, so calling ExtractSubIndexSets("tre.upload_port") returns:
549+
//
550+
// [ properties.Map{
551+
// "vid": "0x1001",
552+
// "pid": "0x2001",
553+
// },
554+
// properties.Map{
555+
// "vid": "0x1002",
556+
// "pid": "0x2002",
557+
// },
558+
// ]
559+
//
560+
// Numeric subindex cannot be mixed with non-numeric, in that case only the numeric sub
561+
// index sets will be returned.
562+
func (m *Map) ExtractSubIndexSets(root string) []*Map {
563+
res := []*Map{}
564+
portIDPropsSet := m.SubTree(root)
565+
if portIDPropsSet.Size() == 0 {
566+
return res
567+
}
568+
569+
// First check the properties with numeric sub index "root.N.xxx"
570+
idx := 0
571+
haveIndexedProperties := false
572+
for {
573+
idProps := portIDPropsSet.SubTree(fmt.Sprintf("%d", idx))
574+
idx++
575+
if idProps.Size() > 0 {
576+
haveIndexedProperties = true
577+
res = append(res, idProps)
578+
} else if idx > 1 {
579+
// Always check sub-id 0 and 1 (https://github.com/arduino/arduino-cli/issues/456)
580+
break
581+
}
582+
}
583+
584+
// if there are no subindexed then return the whole "roox.xxx" subtree
585+
if !haveIndexedProperties {
586+
res = append(res, portIDPropsSet)
587+
}
588+
589+
return res
590+
}

properties_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,51 @@ func TestEqualsAndContains(t *testing.T) {
295295
json.Unmarshal([]byte(data2), &prevOpts)
296296
require.False(t, opts.Equals(prevOpts))
297297
}
298+
299+
func TestExtractSubIndexSets(t *testing.T) {
300+
data := map[string]string{
301+
"uno.upload_port.vid": "0x1000",
302+
"uno.upload_port.pid": "0x2000",
303+
"due.upload_port.0.vid": "0x1000",
304+
"due.upload_port.0.pid": "0x2000",
305+
"due.upload_port.1.vid": "0x1001",
306+
"due.upload_port.1.pid": "0x2001",
307+
"tre.upload_port.1.vid": "0x1001",
308+
"tre.upload_port.1.pid": "0x2001",
309+
"tre.upload_port.2.vid": "0x1002",
310+
"tre.upload_port.2.pid": "0x2002",
311+
"quattro.upload_port.vid": "0x1001",
312+
"quattro.upload_port.pid": "0x2001",
313+
"quattro.upload_port.1.vid": "0x1002",
314+
"quattro.upload_port.1.pid": "0x2002",
315+
"quattro.upload_port.2.vid": "0x1003",
316+
"quattro.upload_port.2.pid": "0x2003",
317+
}
318+
m := NewFromHashmap(data)
319+
320+
s1 := m.ExtractSubIndexSets("uno.upload_port")
321+
require.Len(t, s1, 1)
322+
require.Equal(t, s1[0].Get("vid"), "0x1000")
323+
require.Equal(t, s1[0].Get("pid"), "0x2000")
324+
325+
s2 := m.ExtractSubIndexSets("due.upload_port")
326+
require.Len(t, s2, 2)
327+
require.Equal(t, s2[0].Get("vid"), "0x1000")
328+
require.Equal(t, s2[0].Get("pid"), "0x2000")
329+
require.Equal(t, s2[1].Get("vid"), "0x1001")
330+
require.Equal(t, s2[1].Get("pid"), "0x2001")
331+
332+
s3 := m.ExtractSubIndexSets("tre.upload_port")
333+
require.Len(t, s3, 2)
334+
require.Equal(t, s3[0].Get("vid"), "0x1001")
335+
require.Equal(t, s3[0].Get("pid"), "0x2001")
336+
require.Equal(t, s3[1].Get("vid"), "0x1002")
337+
require.Equal(t, s3[1].Get("pid"), "0x2002")
338+
339+
s4 := m.ExtractSubIndexSets("quattro.upload_port")
340+
require.Len(t, s4, 2)
341+
require.Equal(t, s4[0].Get("vid"), "0x1002")
342+
require.Equal(t, s4[0].Get("pid"), "0x2002")
343+
require.Equal(t, s4[1].Get("vid"), "0x1003")
344+
require.Equal(t, s4[1].Get("pid"), "0x2003")
345+
}

0 commit comments

Comments
 (0)