diff --git a/go-configmap/go.mod b/go-configmap/go.mod deleted file mode 100644 index e4241e081c1..00000000000 --- a/go-configmap/go.mod +++ /dev/null @@ -1,13 +0,0 @@ -module github.com/arduino/go-configmap - -go 1.21 - -require ( - github.com/stretchr/testify v1.8.4 - gopkg.in/yaml.v3 v3.0.1 -) - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect -) diff --git a/go-configmap/go.sum b/go-configmap/go.sum deleted file mode 100644 index fa4b6e6825c..00000000000 --- a/go-configmap/go.sum +++ /dev/null @@ -1,10 +0,0 @@ -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/go-configmap/json.go b/go-configmap/json.go deleted file mode 100644 index cad9a646210..00000000000 --- a/go-configmap/json.go +++ /dev/null @@ -1,37 +0,0 @@ -package configmap - -import "encoding/json" - -func (c Map) MarshalJSON() ([]byte, error) { - return json.Marshal(c.values) -} - -func (c *Map) UnmarshalJSON(data []byte) error { - in := map[string]any{} - if err := json.Unmarshal(data, &in); err != nil { - return err - } - - c.values = map[string]any{} - for k, v := range flattenMap(in) { - if err := c.Set(k, v); err != nil { - return err - } - } - return nil -} - -func flattenMap(in map[string]any) map[string]any { - out := map[string]any{} - for k, v := range in { - switch v := v.(type) { - case map[string]any: - for kk, vv := range flattenMap(v) { - out[k+"."+kk] = vv - } - default: - out[k] = v - } - } - return out -} diff --git a/go-configmap/json_test.go b/go-configmap/json_test.go deleted file mode 100644 index 4730a8fb959..00000000000 --- a/go-configmap/json_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package configmap_test - -import ( - "encoding/json" - "fmt" - "testing" - - "github.com/arduino/go-configmap" - "github.com/stretchr/testify/require" -) - -func TestJson(t *testing.T) { - c := configmap.New() - c.Set("foo", "bar") - c.Set("fooz.bar", "baz") - c.Set("answer", 42) - require.Equal(t, "bar", c.Get("foo")) - require.Equal(t, "baz", c.Get("fooz.bar")) - require.Equal(t, 42, c.Get("answer")) - - j1, err := json.Marshal(c) - require.NoError(t, err) - fmt.Println(string(j1)) - - d := configmap.New() - err = json.Unmarshal(j1, d) - require.NoError(t, err) - require.Equal(t, "baz", d.Get("fooz.bar")) - - j2, err := json.Marshal(d) - require.NoError(t, err) - require.Equal(t, string(j1), string(j2)) -} diff --git a/go-configmap/yaml.go b/go-configmap/yaml.go deleted file mode 100644 index d780b7eb2d8..00000000000 --- a/go-configmap/yaml.go +++ /dev/null @@ -1,23 +0,0 @@ -package configmap - -import ( - "gopkg.in/yaml.v3" -) - -func (c Map) MarshalYAML() (interface{}, error) { - return c.values, nil -} - -func (c *Map) UnmarshalYAML(node *yaml.Node) error { - in := map[string]any{} - if err := node.Decode(&in); err != nil { - return err - } - - for k, v := range flattenMap(in) { - if err := c.Set(k, v); err != nil { - return err - } - } - return nil -} diff --git a/go-configmap/yaml_test.go b/go-configmap/yaml_test.go deleted file mode 100644 index ef25e28f620..00000000000 --- a/go-configmap/yaml_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package configmap_test - -import ( - "fmt" - "testing" - - "github.com/arduino/go-configmap" - "github.com/stretchr/testify/require" - "gopkg.in/yaml.v3" -) - -func TestYaml(t *testing.T) { - c := configmap.New() - c.Set("foo", "bar") - c.Set("fooz.bar", "baz") - c.Set("answer", 42) - require.Equal(t, "bar", c.Get("foo")) - require.Equal(t, "baz", c.Get("fooz.bar")) - require.Equal(t, 42, c.Get("answer")) - - y1, err := yaml.Marshal(c) - require.NoError(t, err) - fmt.Println(string(y1)) - - d := configmap.New() - err = yaml.Unmarshal(y1, d) - require.NoError(t, err) - require.Equal(t, "baz", d.Get("fooz.bar")) - - y2, err := yaml.Marshal(d) - require.NoError(t, err) - require.Equal(t, string(y1), string(y2)) -} diff --git a/go.mod b/go.mod index f24a6246576..13185198605 100644 --- a/go.mod +++ b/go.mod @@ -5,11 +5,8 @@ go 1.21 // We must use this fork until https://github.com/mailru/easyjson/pull/372 is merged replace github.com/mailru/easyjson => github.com/cmaglie/easyjson v0.8.1 -replace github.com/arduino/go-configmap => ./go-configmap - require ( github.com/ProtonMail/go-crypto v1.1.0-alpha.0 - github.com/arduino/go-configmap v0.0.1 github.com/arduino/go-paths-helper v1.12.0 github.com/arduino/go-properties-orderedmap v1.8.0 github.com/arduino/go-serial-utils v0.1.2 diff --git a/internal/cli/configuration/configuration.go b/internal/cli/configuration/configuration.go index 8f6481d643c..5176acc9a28 100644 --- a/internal/cli/configuration/configuration.go +++ b/internal/cli/configuration/configuration.go @@ -21,8 +21,8 @@ import ( "runtime" "github.com/arduino/arduino-cli/internal/cli/feedback" + "github.com/arduino/arduino-cli/internal/go-configmap" "github.com/arduino/arduino-cli/internal/i18n" - "github.com/arduino/go-configmap" "github.com/arduino/go-win32-utils" ) diff --git a/go-configmap/cli.go b/internal/go-configmap/cli.go similarity index 74% rename from go-configmap/cli.go rename to internal/go-configmap/cli.go index b268e1f8d18..3d4313a076e 100644 --- a/go-configmap/cli.go +++ b/internal/go-configmap/cli.go @@ -1,3 +1,18 @@ +// This file is part of arduino-cli. +// +// Copyright 2024 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + package configmap import ( diff --git a/go-configmap/configuration.go b/internal/go-configmap/configuration.go similarity index 85% rename from go-configmap/configuration.go rename to internal/go-configmap/configuration.go index 205c2a021cb..5ea74eac983 100644 --- a/go-configmap/configuration.go +++ b/internal/go-configmap/configuration.go @@ -1,3 +1,18 @@ +// This file is part of arduino-cli. +// +// Copyright 2024 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + package configmap import ( diff --git a/go-configmap/configuration_test.go b/internal/go-configmap/configuration_test.go similarity index 83% rename from go-configmap/configuration_test.go rename to internal/go-configmap/configuration_test.go index a2a3bcca597..222928e0b8f 100644 --- a/go-configmap/configuration_test.go +++ b/internal/go-configmap/configuration_test.go @@ -1,3 +1,18 @@ +// This file is part of arduino-cli. +// +// Copyright 2024 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + package configmap_test import ( @@ -5,7 +20,7 @@ import ( "fmt" "testing" - "github.com/arduino/go-configmap" + "github.com/arduino/arduino-cli/internal/go-configmap" "github.com/stretchr/testify/require" "gopkg.in/yaml.v3" ) diff --git a/internal/go-configmap/json.go b/internal/go-configmap/json.go new file mode 100644 index 00000000000..7ab0deaad4f --- /dev/null +++ b/internal/go-configmap/json.go @@ -0,0 +1,52 @@ +// This file is part of arduino-cli. +// +// Copyright 2024 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package configmap + +import "encoding/json" + +func (c Map) MarshalJSON() ([]byte, error) { + return json.Marshal(c.values) +} + +func (c *Map) UnmarshalJSON(data []byte) error { + in := map[string]any{} + if err := json.Unmarshal(data, &in); err != nil { + return err + } + + c.values = map[string]any{} + for k, v := range flattenMap(in) { + if err := c.Set(k, v); err != nil { + return err + } + } + return nil +} + +func flattenMap(in map[string]any) map[string]any { + out := map[string]any{} + for k, v := range in { + switch v := v.(type) { + case map[string]any: + for kk, vv := range flattenMap(v) { + out[k+"."+kk] = vv + } + default: + out[k] = v + } + } + return out +} diff --git a/internal/go-configmap/json_test.go b/internal/go-configmap/json_test.go new file mode 100644 index 00000000000..54566589baf --- /dev/null +++ b/internal/go-configmap/json_test.go @@ -0,0 +1,48 @@ +// This file is part of arduino-cli. +// +// Copyright 2024 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package configmap_test + +import ( + "encoding/json" + "fmt" + "testing" + + "github.com/arduino/arduino-cli/internal/go-configmap" + "github.com/stretchr/testify/require" +) + +func TestJson(t *testing.T) { + c := configmap.New() + c.Set("foo", "bar") + c.Set("fooz.bar", "baz") + c.Set("answer", 42) + require.Equal(t, "bar", c.Get("foo")) + require.Equal(t, "baz", c.Get("fooz.bar")) + require.Equal(t, 42, c.Get("answer")) + + j1, err := json.Marshal(c) + require.NoError(t, err) + fmt.Println(string(j1)) + + d := configmap.New() + err = json.Unmarshal(j1, d) + require.NoError(t, err) + require.Equal(t, "baz", d.Get("fooz.bar")) + + j2, err := json.Marshal(d) + require.NoError(t, err) + require.Equal(t, string(j1), string(j2)) +} diff --git a/go-configmap/types.go b/internal/go-configmap/types.go similarity index 84% rename from go-configmap/types.go rename to internal/go-configmap/types.go index b349165f802..d6a032b8dd6 100644 --- a/go-configmap/types.go +++ b/internal/go-configmap/types.go @@ -1,3 +1,18 @@ +// This file is part of arduino-cli. +// +// Copyright 2024 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + package configmap import ( diff --git a/internal/go-configmap/yaml.go b/internal/go-configmap/yaml.go new file mode 100644 index 00000000000..214e12ba727 --- /dev/null +++ b/internal/go-configmap/yaml.go @@ -0,0 +1,38 @@ +// This file is part of arduino-cli. +// +// Copyright 2024 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package configmap + +import ( + "gopkg.in/yaml.v3" +) + +func (c Map) MarshalYAML() (interface{}, error) { + return c.values, nil +} + +func (c *Map) UnmarshalYAML(node *yaml.Node) error { + in := map[string]any{} + if err := node.Decode(&in); err != nil { + return err + } + + for k, v := range flattenMap(in) { + if err := c.Set(k, v); err != nil { + return err + } + } + return nil +} diff --git a/internal/go-configmap/yaml_test.go b/internal/go-configmap/yaml_test.go new file mode 100644 index 00000000000..9293f70662f --- /dev/null +++ b/internal/go-configmap/yaml_test.go @@ -0,0 +1,48 @@ +// This file is part of arduino-cli. +// +// Copyright 2024 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package configmap_test + +import ( + "fmt" + "testing" + + "github.com/arduino/arduino-cli/internal/go-configmap" + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" +) + +func TestYaml(t *testing.T) { + c := configmap.New() + c.Set("foo", "bar") + c.Set("fooz.bar", "baz") + c.Set("answer", 42) + require.Equal(t, "bar", c.Get("foo")) + require.Equal(t, "baz", c.Get("fooz.bar")) + require.Equal(t, 42, c.Get("answer")) + + y1, err := yaml.Marshal(c) + require.NoError(t, err) + fmt.Println(string(y1)) + + d := configmap.New() + err = yaml.Unmarshal(y1, d) + require.NoError(t, err) + require.Equal(t, "baz", d.Get("fooz.bar")) + + y2, err := yaml.Marshal(d) + require.NoError(t, err) + require.Equal(t, string(y1), string(y2)) +}