diff --git a/arduino/libraries/libraries_layout.go b/arduino/libraries/libraries_layout.go index 2a85f95d5ce..8391141322a 100644 --- a/arduino/libraries/libraries_layout.go +++ b/arduino/libraries/libraries_layout.go @@ -38,19 +38,14 @@ func (d *LibraryLayout) String() string { return "flat" case RecursiveLayout: return "recursive" + default: + panic(fmt.Sprintf("invalid LibraryLayout value %d", *d)) } - panic(fmt.Sprintf("invalid LibraryLayout value %d", *d)) } // MarshalJSON implements the json.Marshaler interface -func (d *LibraryLayout) MarshalJSON() ([]byte, error) { - switch *d { - case FlatLayout: - return json.Marshal("flat") - case RecursiveLayout: - return json.Marshal("recursive") - } - return nil, fmt.Errorf(tr("invalid library layout value: %d"), *d) +func (d LibraryLayout) MarshalJSON() ([]byte, error) { + return json.Marshal(d.String()) } // UnmarshalJSON implements the json.Unmarshaler interface @@ -62,10 +57,13 @@ func (d *LibraryLayout) UnmarshalJSON(b []byte) error { switch s { case "flat": *d = FlatLayout + return nil case "recursive": *d = RecursiveLayout + return nil + default: + return fmt.Errorf(tr("invalid library layout: %s"), s) } - return fmt.Errorf(tr("invalid library layout: %s"), s) } // ToRPCLibraryLayout converts this LibraryLayout to rpc.LibraryLayout @@ -75,6 +73,7 @@ func (d *LibraryLayout) ToRPCLibraryLayout() rpc.LibraryLayout { return rpc.LibraryLayout_LIBRARY_LAYOUT_FLAT case RecursiveLayout: return rpc.LibraryLayout_LIBRARY_LAYOUT_RECURSIVE + default: + panic(fmt.Sprintf("invalid LibraryLayout value %d", *d)) } - panic(fmt.Sprintf("invalid LibraryLayout value %d", *d)) } diff --git a/arduino/libraries/libraries_location.go b/arduino/libraries/libraries_location.go index e6bd933ebd6..f93a6a01f40 100644 --- a/arduino/libraries/libraries_location.go +++ b/arduino/libraries/libraries_location.go @@ -52,25 +52,14 @@ func (d *LibraryLocation) String() string { return "user" case Unmanaged: return "unmanaged" + default: + panic(fmt.Sprintf("invalid LibraryLocation value %d", *d)) } - panic(fmt.Sprintf("invalid LibraryLocation value %d", *d)) } // MarshalJSON implements the json.Marshaler interface -func (d *LibraryLocation) MarshalJSON() ([]byte, error) { - switch *d { - case IDEBuiltIn: - return json.Marshal("ide") - case PlatformBuiltIn: - return json.Marshal("platform") - case ReferencedPlatformBuiltIn: - return json.Marshal("ref-platform") - case User: - return json.Marshal("user") - case Unmanaged: - return json.Marshal("unmanaged") - } - return nil, fmt.Errorf(tr("invalid library location value: %d"), *d) +func (d LibraryLocation) MarshalJSON() ([]byte, error) { + return json.Marshal(d.String()) } // UnmarshalJSON implements the json.Unmarshaler interface @@ -82,16 +71,22 @@ func (d *LibraryLocation) UnmarshalJSON(b []byte) error { switch s { case "ide": *d = IDEBuiltIn + return nil case "platform": *d = PlatformBuiltIn + return nil case "ref-platform": *d = ReferencedPlatformBuiltIn + return nil case "user": *d = User + return nil case "unmanaged": *d = Unmanaged + return nil + default: + return fmt.Errorf(tr("invalid library location: %s"), s) } - return fmt.Errorf(tr("invalid library location: %s"), s) } // ToRPCLibraryLocation converts this LibraryLocation to rpc.LibraryLocation @@ -107,8 +102,9 @@ func (d *LibraryLocation) ToRPCLibraryLocation() rpc.LibraryLocation { return rpc.LibraryLocation_LIBRARY_LOCATION_USER case Unmanaged: return rpc.LibraryLocation_LIBRARY_LOCATION_UNMANAGED + default: + panic(fmt.Sprintf("invalid LibraryLocation value %d", *d)) } - panic(fmt.Sprintf("invalid LibraryLocation value %d", *d)) } // FromRPCLibraryLocation converts a rpc.LibraryLocation to a LibraryLocation @@ -124,6 +120,7 @@ func FromRPCLibraryLocation(l rpc.LibraryLocation) LibraryLocation { return User case rpc.LibraryLocation_LIBRARY_LOCATION_UNMANAGED: return Unmanaged + default: + panic(fmt.Sprintf("invalid rpc.LibraryLocation value %d", l)) } - panic(fmt.Sprintf("invalid rpc.LibraryLocation value %d", l)) } diff --git a/arduino/libraries/libraries_test.go b/arduino/libraries/libraries_test.go new file mode 100644 index 00000000000..09461790f8a --- /dev/null +++ b/arduino/libraries/libraries_test.go @@ -0,0 +1,50 @@ +// This file is part of arduino-cli. +// +// Copyright 2020 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 libraries + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestLibLayoutAndLocationJSONUnMarshaler(t *testing.T) { + testLayout := func(l LibraryLayout) { + d, err := json.Marshal(l) + require.NoError(t, err) + var m LibraryLayout + err = json.Unmarshal(d, &m) + require.NoError(t, err) + require.Equal(t, l, m) + } + testLayout(FlatLayout) + testLayout(RecursiveLayout) + + testLocation := func(l LibraryLocation) { + d, err := json.Marshal(l) + require.NoError(t, err) + var m LibraryLocation + err = json.Unmarshal(d, &m) + require.NoError(t, err) + require.Equal(t, l, m) + } + testLocation(IDEBuiltIn) + testLocation(PlatformBuiltIn) + testLocation(ReferencedPlatformBuiltIn) + testLocation(User) + testLocation(Unmanaged) +}