From 9c7ca35c886bf0d1fdddcb729b70981edc9af1f4 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Thu, 27 Apr 2023 13:42:42 +0200 Subject: [PATCH 1/3] Respect the order of menu, when computing the config options --- arduino/cores/board.go | 13 +++++++++++-- arduino/cores/board_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/arduino/cores/board.go b/arduino/cores/board.go index 13c84badf41..9c7952c9e15 100644 --- a/arduino/cores/board.go +++ b/arduino/cores/board.go @@ -79,8 +79,17 @@ func (b *Board) buildConfigOptionsStructures() { b.configOptions = properties.NewMap() allConfigs := b.Properties.SubTree("menu") - for _, option := range allConfigs.FirstLevelKeys() { - b.configOptions.Set(option, b.PlatformRelease.Menus.Get(option)) + + // Used to show the config options in the same order as the menu, defined at the begging of platform.txt + if b.PlatformRelease.Menus != nil { + for _, menuOption := range b.PlatformRelease.Menus.FirstLevelKeys() { + for _, option := range allConfigs.FirstLevelKeys() { + if menuOption == option { + b.configOptions.Set(option, b.PlatformRelease.Menus.Get(menuOption)) + break + } + } + } } b.configOptionValues = map[string]*properties.Map{} diff --git a/arduino/cores/board_test.go b/arduino/cores/board_test.go index 1dade30a3bd..577a9af09de 100644 --- a/arduino/cores/board_test.go +++ b/arduino/cores/board_test.go @@ -16,6 +16,7 @@ package cores import ( + "fmt" "testing" properties "github.com/arduino/go-properties-orderedmap" @@ -370,6 +371,41 @@ func TestBoardOptions(t *testing.T) { // fmt.Print(string(data)) } +func TestBoarConfigOptionSortingSameAsMenu(t *testing.T) { + menus := properties.NewMap() + menus.Set("BoardModel", "Model") + menus.Set("xtal", "CPU Frequency") + menus.Set("vt", "VTables") + menus.Set("wipe", "Erase Flash") + + props := properties.NewMap() + props.Set("menu.xtal.80", "80 MHz") + props.Set("menu.wipe.none", "Only Sketch") + props.Set("menu.BoardModel.primo", "Primo") + props.Set("menu.BoardModel.primo.build.board", "ESP8266_ARDUINO_PRIMO") + props.Set("menu.vt.flash", "Flash") + + esp8266 := &Board{ + BoardID: "arduino-esp8266", + Properties: props, + PlatformRelease: &PlatformRelease{ + Platform: &Platform{ + Architecture: "esp8266", + Package: &Package{ + Name: "esp8266", + }, + }, + Menus: menus, + }, + } + + config := `xtal=80,wipe=none,BoardModel=primo,vt=flash` + + _, err := esp8266.GeneratePropertiesForConfiguration(config) + require.NoError(t, err, fmt.Sprintf("generating %s configuration", config)) + require.True(t, esp8266.configOptions.EqualsWithOrder(menus)) +} + func TestOSSpecificBoardOptions(t *testing.T) { boardWihOSSpecificOptionProperties := properties.NewMap() boardWihOSSpecificOptionProperties.Set("menu.UploadSpeed.115200", "115200") From 3618d28d373fe0fef1197f80dea0a6a05a877655 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Thu, 27 Apr 2023 16:31:59 +0200 Subject: [PATCH 2/3] reduce cyclomatic complexity --- arduino/cores/board.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/arduino/cores/board.go b/arduino/cores/board.go index 9c7952c9e15..7e2660c48c8 100644 --- a/arduino/cores/board.go +++ b/arduino/cores/board.go @@ -79,15 +79,13 @@ func (b *Board) buildConfigOptionsStructures() { b.configOptions = properties.NewMap() allConfigs := b.Properties.SubTree("menu") + allConfigOptions := allConfigs.FirstLevelOf() // Used to show the config options in the same order as the menu, defined at the begging of platform.txt if b.PlatformRelease.Menus != nil { for _, menuOption := range b.PlatformRelease.Menus.FirstLevelKeys() { - for _, option := range allConfigs.FirstLevelKeys() { - if menuOption == option { - b.configOptions.Set(option, b.PlatformRelease.Menus.Get(menuOption)) - break - } + if _, ok := allConfigOptions[menuOption]; ok { + b.configOptions.Set(menuOption, b.PlatformRelease.Menus.Get(menuOption)) } } } @@ -95,7 +93,7 @@ func (b *Board) buildConfigOptionsStructures() { b.configOptionValues = map[string]*properties.Map{} b.configOptionProperties = map[string]*properties.Map{} b.defaultConfig = properties.NewMap() - for option, optionProps := range allConfigs.FirstLevelOf() { + for option, optionProps := range allConfigOptions { b.configOptionValues[option] = properties.NewMap() values := optionProps.FirstLevelKeys() b.defaultConfig.Set(option, values[0]) From 0fbb88aa661967977ed0329b15ce07903d11ba65 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Thu, 27 Apr 2023 16:33:21 +0200 Subject: [PATCH 3/3] fix the comment that was referring to the wrong filename --- arduino/cores/board.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arduino/cores/board.go b/arduino/cores/board.go index 7e2660c48c8..8a41a8fb198 100644 --- a/arduino/cores/board.go +++ b/arduino/cores/board.go @@ -81,7 +81,7 @@ func (b *Board) buildConfigOptionsStructures() { allConfigs := b.Properties.SubTree("menu") allConfigOptions := allConfigs.FirstLevelOf() - // Used to show the config options in the same order as the menu, defined at the begging of platform.txt + // Used to show the config options in the same order as the menu, defined at the begging of boards.txt if b.PlatformRelease.Menus != nil { for _, menuOption := range b.PlatformRelease.Menus.FirstLevelKeys() { if _, ok := allConfigOptions[menuOption]; ok {