-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproperties.go
702 lines (633 loc) · 18.8 KB
/
properties.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
* This file is part of PropertiesOrderedMap library.
*
* Copyright 2017-2018 Arduino AG (http://www.arduino.cc/)
*
* PropertiesMap library is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*/
/*
Package properties is a library for handling maps of hierarchical properties.
This library is mainly used in the Arduino platform software to handle
configurations made of key/value pairs stored in files with an INI like
syntax, for example:
...
uno.name=Arduino/Genuino Uno
uno.upload.tool=avrdude
uno.upload.protocol=arduino
uno.upload.maximum_size=32256
uno.upload.maximum_data_size=2048
uno.upload.speed=115200
uno.build.mcu=atmega328p
uno.build.f_cpu=16000000L
uno.build.board=AVR_UNO
uno.build.core=arduino
uno.build.variant=standard
diecimila.name=Arduino Duemilanove or Diecimila
diecimila.upload.tool=avrdude
diecimila.upload.protocol=arduino
diecimila.build.f_cpu=16000000L
diecimila.build.board=AVR_DUEMILANOVE
diecimila.build.core=arduino
diecimila.build.variant=standard
...
This library has methods to parse this kind of file into a Map object.
The Map internally keeps the insertion order so it can be retrieved later when
cycling through the key sets.
The Map object has many helper methods to accomplish some common operations
on this kind of data like cloning, merging, comparing and also extracting
a submap or generating a map-of-submaps from the first "level" of the hierarchy.
On the Arduino platform the properties are used to populate command line recipes
so there are some methods to help this task like SplitQuotedString or ExpandPropsInString.
*/
package properties
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"reflect"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"unicode/utf8"
"github.com/arduino/go-paths-helper"
)
// Map is a container of properties
type Map struct {
kv map[string]string
o []string
// Debug if set to true ExpandPropsInString will always output debugging information
Debug bool
}
var osSuffix string
func init() {
switch value := runtime.GOOS; value {
case "darwin":
osSuffix = "macosx"
default:
osSuffix = runtime.GOOS
}
}
// GetOSSuffix returns the os name used to filter os-specific properties in Load* functions
func GetOSSuffix() string {
return osSuffix
}
// SetOSSuffix forces the OS suffix to the given value
func SetOSSuffix(suffix string) {
osSuffix = suffix
}
// NewMap returns a new Map
func NewMap() *Map {
return &Map{
kv: map[string]string{},
o: []string{},
}
}
// NewFromHashmap creates a new Map from the given map[string]string.
// Insertion order is not preserved.
func NewFromHashmap(orig map[string]string) *Map {
res := NewMap()
for k, v := range orig {
res.Set(k, v)
}
return res
}
func toUtf8(iso8859_1_buf []byte) string {
buf := make([]rune, len(iso8859_1_buf))
for i, b := range iso8859_1_buf {
buf[i] = rune(b)
}
return string(buf)
}
// LoadFromBytes reads properties data and makes a Map out of it.
func LoadFromBytes(bytes []byte) (*Map, error) {
var text string
if utf8.Valid(bytes) {
text = string(bytes)
} else {
// Assume ISO8859-1 encoding and convert to UTF-8
text = toUtf8(bytes)
}
text = strings.Replace(text, "\r\n", "\n", -1)
text = strings.Replace(text, "\r", "\n", -1)
properties := NewMap()
for lineNum, line := range strings.Split(text, "\n") {
if err := properties.parseLine(line); err != nil {
return nil, fmt.Errorf("error parsing data at line %d: %s", lineNum, err)
}
}
return properties, nil
}
// Load reads a properties file and makes a Map out of it.
func Load(filepath string) (*Map, error) {
bytes, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, fmt.Errorf("error reading file: %s", err)
}
res, err := LoadFromBytes(bytes)
if err != nil {
return nil, fmt.Errorf("error reading file: %s", err)
}
return res, nil
}
// LoadFromPath reads a properties file and makes a Map out of it.
func LoadFromPath(path *paths.Path) (*Map, error) {
return Load(path.String())
}
// LoadFromSlice reads a properties file from an array of string
// and makes a Map out of it
func LoadFromSlice(lines []string) (*Map, error) {
properties := NewMap()
for lineNum, line := range lines {
if err := properties.parseLine(line); err != nil {
return nil, fmt.Errorf("error reading from slice (index:%d): %s", lineNum, err)
}
}
return properties, nil
}
func (m *Map) parseLine(line string) error {
line = strings.TrimSpace(line)
// Skip empty lines or comments
if len(line) == 0 || line[0] == '#' {
return nil
}
lineParts := strings.SplitN(line, "=", 2)
if len(lineParts) != 2 {
return fmt.Errorf("invalid line format, should be 'key=value'")
}
key := strings.TrimSpace(lineParts[0])
value := strings.TrimSpace(lineParts[1])
key = strings.Replace(key, "."+osSuffix, "", 1)
m.Set(key, value)
return nil
}
// SafeLoadFromPath is like LoadFromPath, except that it returns an empty Map if
// the specified file doesn't exist
func SafeLoadFromPath(path *paths.Path) (*Map, error) {
return SafeLoad(path.String())
}
// SafeLoad is like Load, except that it returns an empty Map if the specified
// file doesn't exist
func SafeLoad(filepath string) (*Map, error) {
_, err := os.Stat(filepath)
if os.IsNotExist(err) {
return NewMap(), nil
}
properties, err := Load(filepath)
if err != nil {
return nil, err
}
return properties, nil
}
// Get retrieves the value corresponding to key
func (m *Map) Get(key string) string {
return m.kv[key]
}
// GetOk retrieves the value corresponding to key and returns a true/false indicator
// to check if the key is present in the map (true if the key is present)
func (m *Map) GetOk(key string) (string, bool) {
v, ok := m.kv[key]
return v, ok
}
// ContainsKey returns true if the map contains the specified key
func (m *Map) ContainsKey(key string) bool {
_, has := m.kv[key]
return has
}
// ContainsValue returns true if the map contains the specified value
func (m *Map) ContainsValue(value string) bool {
for _, v := range m.kv {
if v == value {
return true
}
}
return false
}
// Set inserts or replaces an existing key-value pair in the map
func (m *Map) Set(key, value string) {
if _, has := m.kv[key]; has {
m.Remove(key)
}
m.kv[key] = value
m.o = append(m.o, key)
}
// Size returns the number of elements in the map
func (m *Map) Size() int {
return len(m.kv)
}
// Remove removes the key from the map
func (m *Map) Remove(key string) {
delete(m.kv, key)
for i, k := range m.o {
if k == key {
m.o = append(m.o[:i], m.o[i+1:]...)
return
}
}
}
// FirstLevelOf generates a map-of-Maps using the first level of the hierarchy
// of the current Map. For example the following Map:
//
// properties.Map{
// "uno.name": "Arduino/Genuino Uno",
// "uno.upload.tool": "avrdude",
// "uno.upload.protocol": "arduino",
// "uno.upload.maximum_size": "32256",
// "diecimila.name": "Arduino Duemilanove or Diecimila",
// "diecimila.upload.tool": "avrdude",
// "diecimila.upload.protocol": "arduino",
// "diecimila.bootloader.tool": "avrdude",
// "diecimila.bootloader.low_fuses": "0xFF",
// }
//
// is transformed into the following map-of-Maps:
//
// map[string]Map{
// "uno" : properties.Map{
// "name": "Arduino/Genuino Uno",
// "upload.tool": "avrdude",
// "upload.protocol": "arduino",
// "upload.maximum_size": "32256",
// },
// "diecimila" : properties.Map{
// "name": "Arduino Duemilanove or Diecimila",
// "upload.tool": "avrdude",
// "upload.protocol": "arduino",
// "bootloader.tool": "avrdude",
// "bootloader.low_fuses": "0xFF",
// }
// }
func (m *Map) FirstLevelOf() map[string]*Map {
newMap := make(map[string]*Map)
for _, key := range m.o {
if !strings.Contains(key, ".") {
continue
}
keyParts := strings.SplitN(key, ".", 2)
if newMap[keyParts[0]] == nil {
newMap[keyParts[0]] = NewMap()
}
value := m.kv[key]
newMap[keyParts[0]].Set(keyParts[1], value)
}
return newMap
}
// FirstLevelKeys returns the keys in the first level of the hierarchy
// of the current Map. For example the following Map:
//
// properties.Map{
// "uno.name": "Arduino/Genuino Uno",
// "uno.upload.tool": "avrdude",
// "uno.upload.protocol": "arduino",
// "uno.upload.maximum_size": "32256",
// "diecimila.name": "Arduino Duemilanove or Diecimila",
// "diecimila.upload.tool": "avrdude",
// "diecimila.upload.protocol": "arduino",
// "diecimila.bootloader.tool": "avrdude",
// "diecimila.bootloader.low_fuses": "0xFF",
// }
//
// will produce the following result:
//
// []string{
// "uno",
// "diecimila",
// }
//
// the order of the original map is preserved
func (m *Map) FirstLevelKeys() []string {
res := []string{}
taken := map[string]bool{}
for _, k := range m.o {
first := strings.SplitN(k, ".", 2)[0]
if taken[first] {
continue
}
taken[first] = true
res = append(res, first)
}
return res
}
// SubTree extracts a sub Map from an existing map using the first level
// of the keys hierarchy as selector.
// For example the following Map:
//
// properties.Map{
// "uno.name": "Arduino/Genuino Uno",
// "uno.upload.tool": "avrdude",
// "uno.upload.protocol": "arduino",
// "uno.upload.maximum_size": "32256",
// "diecimila.name": "Arduino Duemilanove or Diecimila",
// "diecimila.upload.tool": "avrdude",
// "diecimila.upload.protocol": "arduino",
// "diecimila.bootloader.tool": "avrdude",
// "diecimila.bootloader.low_fuses": "0xFF",
// }
//
// after calling SubTree("uno") will be transformed into:
//
// properties.Map{
// "name": "Arduino/Genuino Uno",
// "upload.tool": "avrdude",
// "upload.protocol": "arduino",
// "upload.maximum_size": "32256",
// },
func (m *Map) SubTree(rootKey string) *Map {
rootKey += "."
newMap := NewMap()
for _, key := range m.o {
if !strings.HasPrefix(key, rootKey) {
continue
}
value := m.kv[key]
newMap.Set(key[len(rootKey):], value)
}
return newMap
}
// ExpandPropsInString uses the Map to replace values into a format string.
// The format string should contains markers between braces, for example:
//
// "The selected upload protocol is {upload.protocol}."
//
// Each marker is replaced by the corresponding value of the Map.
// The values in the Map may contain other markers, they are evaluated
// recursively up to 10 times.
func (m *Map) ExpandPropsInString(str string) string {
return m.expandProps(str, false)
}
// IsPropertyMissingInExpandPropsInString checks if a property 'prop' is missing
// when the ExpandPropsInString method is applied to the input string 'str'.
// This method returns false if the 'prop' is defined in the map
// or if 'prop' is not used in the string expansion of 'str', otherwise
// the method returns true.
func (m *Map) IsPropertyMissingInExpandPropsInString(prop, str string) bool {
if m.ContainsKey(prop) {
return false
}
xm := m.Clone()
// Find a random tag that is not contained in the dictionary and the src pattern
var token string
for {
token = fmt.Sprintf("%d", rand.Int63())
if strings.Contains(str, token) {
continue
}
if xm.ContainsKey(token) {
continue
}
if xm.ContainsValue(token) {
continue
}
break
}
xm.Set(prop, token)
res := xm.expandProps(str, false)
return strings.Contains(res, token)
}
// Merge merges other Maps into this one. Each key/value of the merged Maps replaces
// the key/value present in the original Map.
func (m *Map) Merge(sources ...*Map) *Map {
for _, source := range sources {
for _, key := range source.o {
value := source.kv[key]
m.Set(key, value)
}
}
return m
}
// Keys returns an array of the keys contained in the Map
func (m *Map) Keys() []string {
keys := make([]string, len(m.o))
copy(keys, m.o)
return keys
}
// Values returns an array of the values contained in the Map. Duplicated
// values are repeated in the list accordingly.
func (m *Map) Values() []string {
values := make([]string, len(m.o))
for i, key := range m.o {
values[i] = m.kv[key]
}
return values
}
// AsMap returns the underlying map[string]string. This is useful if you need to
// for ... range but without the requirement of the ordered elements.
func (m *Map) AsMap() map[string]string {
return m.kv
}
// AsSlice returns the underlying map[string]string as a slice of
// strings with the pattern `{key}={value}`, maintaining the insertion order of the keys.
func (m *Map) AsSlice() []string {
properties := make([]string, len(m.o))
for i, key := range m.o {
properties[i] = strings.Join([]string{key, m.kv[key]}, "=")
}
return properties
}
// Clone makes a copy of the Map
func (m *Map) Clone() *Map {
clone := NewMap()
clone.Merge(m)
return clone
}
// Equals returns true if the current Map contains the same key/value pairs of
// the Map passed as argument, the order of insertion does not matter.
func (m *Map) Equals(other *Map) bool {
return reflect.DeepEqual(m.kv, other.kv)
}
// EqualsWithOrder returns true if the current Map contains the same key/value pairs of
// the Map passed as argument with the same order of insertion.
func (m *Map) EqualsWithOrder(other *Map) bool {
return reflect.DeepEqual(m.o, other.o) && reflect.DeepEqual(m.kv, other.kv)
}
// MergeMapsOfProperties merges the map-of-Maps (obtained from the method FirstLevelOf()) into the
// target map-of-Maps.
func MergeMapsOfProperties(target map[string]*Map, sources ...map[string]*Map) map[string]*Map {
for _, source := range sources {
for key, value := range source {
target[key] = value
}
}
return target
}
// DeleteUnexpandedPropsFromString removes all the brace markers "{xxx}" that are not expanded
// into a value using the Map.ExpandPropsInString() method.
func DeleteUnexpandedPropsFromString(str string) string {
rxp := regexp.MustCompile(`\{.+?\}`)
return rxp.ReplaceAllString(str, "")
}
// ExtractSubIndexSets works like SubTree but it considers also the numeric sub index in the form
// `root.N.xxx...` as separate subsets.
// For example the following Map:
//
// properties.Map{
// "uno.upload_port.vid": "0x1000",
// "uno.upload_port.pid": "0x2000",
// "due.upload_port.0.vid": "0x1000",
// "due.upload_port.0.pid": "0x2000",
// "due.upload_port.1.vid": "0x1001",
// "due.upload_port.1.pid": "0x2001",
// "tre.upload_port.1.vid": "0x1001",
// "tre.upload_port.1.pid": "0x2001",
// "tre.upload_port.2.vid": "0x1002",
// "tre.upload_port.2.pid": "0x2002",
// }
//
// calling ExtractSubIndexSets("uno.upload_port") returns the array:
//
// [ properties.Map{
// "vid": "0x1000",
// "pid": "0x2000",
// },
// ]
//
// calling ExtractSubIndexSets("due.upload_port") returns the array:
//
// [ properties.Map{
// "vid": "0x1000",
// "pid": "0x2000",
// },
// properties.Map{
// "vid": "0x1001",
// "pid": "0x2001",
// },
// ]
//
// the sub-index may start with .1 too, so calling ExtractSubIndexSets("tre.upload_port") returns:
//
// [ properties.Map{
// "vid": "0x1001",
// "pid": "0x2001",
// },
// properties.Map{
// "vid": "0x1002",
// "pid": "0x2002",
// },
// ]
//
// Numeric subindex cannot be mixed with non-numeric, in that case only the numeric sub
// index sets will be returned.
func (m *Map) ExtractSubIndexSets(root string) []*Map {
res := []*Map{}
portIDPropsSet := m.SubTree(root)
if portIDPropsSet.Size() == 0 {
return res
}
// First check the properties with numeric sub index "root.N.xxx"
idx := 0
haveIndexedProperties := false
for {
idProps := portIDPropsSet.SubTree(fmt.Sprintf("%d", idx))
idx++
if idProps.Size() > 0 {
haveIndexedProperties = true
res = append(res, idProps)
} 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, portIDPropsSet)
}
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",
// "quattro.discovery.required.1": "itemA",
// "quattro.discovery.required.4": "itemB",
// "quattro.discovery.required.5": "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" ]
//
// 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 {
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{}
haveIndexedProperties := false
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)
}
}
// if there are no subindexed then return the whole "roox.xxx" subtree
if !haveIndexedProperties {
if value, ok := m.GetOk(root); ok {
res = append(res, value)
}
}
return res
}