Skip to content

Fixed LibraryLocation and LibraryLayout JSON encoder/decoders #1757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions arduino/libraries/libraries_layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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))
}
33 changes: 15 additions & 18 deletions arduino/libraries/libraries_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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))
}
50 changes: 50 additions & 0 deletions arduino/libraries/libraries_test.go
Original file line number Diff line number Diff line change
@@ -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 [email protected].

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)
}