Skip to content

Commit 8927804

Browse files
committed
Added FirstLevelKeys method
1 parent 7caa910 commit 8927804

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

properties.go

+37
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,43 @@ func (m *Map) FirstLevelOf() map[string]*Map {
291291
return newMap
292292
}
293293

294+
// FirstLevelKeys returns the keys in the first level of the hierarchy
295+
// of the current Map. For example the following Map:
296+
//
297+
// properties.Map{
298+
// "uno.name": "Arduino/Genuino Uno",
299+
// "uno.upload.tool": "avrdude",
300+
// "uno.upload.protocol": "arduino",
301+
// "uno.upload.maximum_size": "32256",
302+
// "diecimila.name": "Arduino Duemilanove or Diecimila",
303+
// "diecimila.upload.tool": "avrdude",
304+
// "diecimila.upload.protocol": "arduino",
305+
// "diecimila.bootloader.tool": "avrdude",
306+
// "diecimila.bootloader.low_fuses": "0xFF",
307+
// }
308+
//
309+
// will produce the following result:
310+
//
311+
// []string{
312+
// "uno",
313+
// "diecimila",
314+
// }
315+
//
316+
// the order of the original map is preserved
317+
func (m *Map) FirstLevelKeys() []string {
318+
res := []string{}
319+
taken := map[string]bool{}
320+
for _, k := range m.o {
321+
first := strings.SplitN(k, ".", 2)[0]
322+
if taken[first] {
323+
continue
324+
}
325+
taken[first] = true
326+
res = append(res, first)
327+
}
328+
return res
329+
}
330+
294331
// SubTree extracts a sub Map from an existing map using the first level
295332
// of the keys hierarchy as selector.
296333
// For example the following Map:

0 commit comments

Comments
 (0)