Commit 4379a60f authored by Cristian Maglie's avatar Cristian Maglie Committed by Cristian Maglie

Use default FQBN options when missing

Fix #23
parent fd1ed1ac
......@@ -38,11 +38,11 @@
[[projects]]
branch = "master"
digest = "1:4abd23fb5076a0d6b83363a5f317714e30a2971c11184bbd8a546b71f89a9cc3"
digest = "1:0ee4da82ad4588eec5fff9a3d2b954cfa37ee46fe6c2e6c7ee6da12111583211"
name = "github.com/arduino/go-properties-orderedmap"
packages = ["."]
pruneopts = "UT"
revision = "10dff422b3669aaa85870522fd0138a89fb244ca"
revision = "89278049acd3b807d566b8290a71e73b7c12ce3d"
[[projects]]
branch = "master"
......
......@@ -62,30 +62,40 @@ func (b *Board) String() string {
// GetBuildProperties returns the build properties and the build
// platform for the Board with the configuration passed as parameter.
func (b *Board) GetBuildProperties(configs *properties.Map) (*properties.Map, error) {
func (b *Board) GetBuildProperties(userConfigs *properties.Map) (*properties.Map, error) {
// Clone user configs because they are destroyed during iteration
userConfigs = userConfigs.Clone()
// Start with board's base properties
buildProperties := b.Properties.Clone()
// Add all sub-configurations one by one
// Add all sub-configurations one by one (a config is: option=value)
menu := b.Properties.SubTree("menu")
for _, option := range configs.Keys() {
if option == "" {
return nil, fmt.Errorf("invalid empty option found")
}
for _, option := range menu.FirstLevelKeys() {
optionMenu := menu.SubTree(option)
if optionMenu.Size() == 0 {
return nil, fmt.Errorf("invalid option '%s'", option)
}
optionValue := configs.Get(option)
if !optionMenu.ContainsKey(optionValue) {
return nil, fmt.Errorf("invalid value '%s' for option '%s'", optionValue, option)
userValue, haveUserValue := userConfigs.GetOk(option)
if haveUserValue {
userConfigs.Remove(option)
if !optionMenu.ContainsKey(userValue) {
return nil, fmt.Errorf("invalid value '%s' for option '%s'", userValue, option)
}
} else {
// apply default
userValue = optionMenu.FirstLevelKeys()[0]
}
optionsConf := optionMenu.SubTree(optionValue)
optionsConf := optionMenu.SubTree(userValue)
buildProperties.Merge(optionsConf)
}
// Check for residual invalid options...
for _, invalidOption := range userConfigs.Keys() {
if invalidOption == "" {
return nil, fmt.Errorf("invalid empty option found")
}
return nil, fmt.Errorf("invalid option '%s'", invalidOption)
}
return buildProperties, nil
}
......
......@@ -30,6 +30,8 @@
package properties
import (
"strings"
"github.com/arduino/go-paths-helper"
)
......@@ -37,7 +39,7 @@ import (
// equals to the string "true", in any other case returns false.
func (m *Map) GetBoolean(key string) bool {
value, ok := m.GetOk(key)
return ok && value == "true"
return ok && strings.TrimSpace(value) == "true"
}
// SetBoolean sets the specified key to the string "true" or "false" if the value
......
......@@ -291,6 +291,43 @@ func (m *Map) FirstLevelOf() map[string]*Map {
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:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment