Skip to content

Commit c350e88

Browse files
authored
[skip-changelog] Fixed LibraryLocation and LibraryLayout JSON encoder/decoders (#1757)
1 parent 899dc91 commit c350e88

File tree

3 files changed

+75
-29
lines changed

3 files changed

+75
-29
lines changed

arduino/libraries/libraries_layout.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,14 @@ func (d *LibraryLayout) String() string {
3838
return "flat"
3939
case RecursiveLayout:
4040
return "recursive"
41+
default:
42+
panic(fmt.Sprintf("invalid LibraryLayout value %d", *d))
4143
}
42-
panic(fmt.Sprintf("invalid LibraryLayout value %d", *d))
4344
}
4445

4546
// MarshalJSON implements the json.Marshaler interface
46-
func (d *LibraryLayout) MarshalJSON() ([]byte, error) {
47-
switch *d {
48-
case FlatLayout:
49-
return json.Marshal("flat")
50-
case RecursiveLayout:
51-
return json.Marshal("recursive")
52-
}
53-
return nil, fmt.Errorf(tr("invalid library layout value: %d"), *d)
47+
func (d LibraryLayout) MarshalJSON() ([]byte, error) {
48+
return json.Marshal(d.String())
5449
}
5550

5651
// UnmarshalJSON implements the json.Unmarshaler interface
@@ -62,10 +57,13 @@ func (d *LibraryLayout) UnmarshalJSON(b []byte) error {
6257
switch s {
6358
case "flat":
6459
*d = FlatLayout
60+
return nil
6561
case "recursive":
6662
*d = RecursiveLayout
63+
return nil
64+
default:
65+
return fmt.Errorf(tr("invalid library layout: %s"), s)
6766
}
68-
return fmt.Errorf(tr("invalid library layout: %s"), s)
6967
}
7068

7169
// ToRPCLibraryLayout converts this LibraryLayout to rpc.LibraryLayout
@@ -75,6 +73,7 @@ func (d *LibraryLayout) ToRPCLibraryLayout() rpc.LibraryLayout {
7573
return rpc.LibraryLayout_LIBRARY_LAYOUT_FLAT
7674
case RecursiveLayout:
7775
return rpc.LibraryLayout_LIBRARY_LAYOUT_RECURSIVE
76+
default:
77+
panic(fmt.Sprintf("invalid LibraryLayout value %d", *d))
7878
}
79-
panic(fmt.Sprintf("invalid LibraryLayout value %d", *d))
8079
}

arduino/libraries/libraries_location.go

+15-18
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,14 @@ func (d *LibraryLocation) String() string {
5252
return "user"
5353
case Unmanaged:
5454
return "unmanaged"
55+
default:
56+
panic(fmt.Sprintf("invalid LibraryLocation value %d", *d))
5557
}
56-
panic(fmt.Sprintf("invalid LibraryLocation value %d", *d))
5758
}
5859

5960
// MarshalJSON implements the json.Marshaler interface
60-
func (d *LibraryLocation) MarshalJSON() ([]byte, error) {
61-
switch *d {
62-
case IDEBuiltIn:
63-
return json.Marshal("ide")
64-
case PlatformBuiltIn:
65-
return json.Marshal("platform")
66-
case ReferencedPlatformBuiltIn:
67-
return json.Marshal("ref-platform")
68-
case User:
69-
return json.Marshal("user")
70-
case Unmanaged:
71-
return json.Marshal("unmanaged")
72-
}
73-
return nil, fmt.Errorf(tr("invalid library location value: %d"), *d)
61+
func (d LibraryLocation) MarshalJSON() ([]byte, error) {
62+
return json.Marshal(d.String())
7463
}
7564

7665
// UnmarshalJSON implements the json.Unmarshaler interface
@@ -82,16 +71,22 @@ func (d *LibraryLocation) UnmarshalJSON(b []byte) error {
8271
switch s {
8372
case "ide":
8473
*d = IDEBuiltIn
74+
return nil
8575
case "platform":
8676
*d = PlatformBuiltIn
77+
return nil
8778
case "ref-platform":
8879
*d = ReferencedPlatformBuiltIn
80+
return nil
8981
case "user":
9082
*d = User
83+
return nil
9184
case "unmanaged":
9285
*d = Unmanaged
86+
return nil
87+
default:
88+
return fmt.Errorf(tr("invalid library location: %s"), s)
9389
}
94-
return fmt.Errorf(tr("invalid library location: %s"), s)
9590
}
9691

9792
// ToRPCLibraryLocation converts this LibraryLocation to rpc.LibraryLocation
@@ -107,8 +102,9 @@ func (d *LibraryLocation) ToRPCLibraryLocation() rpc.LibraryLocation {
107102
return rpc.LibraryLocation_LIBRARY_LOCATION_USER
108103
case Unmanaged:
109104
return rpc.LibraryLocation_LIBRARY_LOCATION_UNMANAGED
105+
default:
106+
panic(fmt.Sprintf("invalid LibraryLocation value %d", *d))
110107
}
111-
panic(fmt.Sprintf("invalid LibraryLocation value %d", *d))
112108
}
113109

114110
// FromRPCLibraryLocation converts a rpc.LibraryLocation to a LibraryLocation
@@ -124,6 +120,7 @@ func FromRPCLibraryLocation(l rpc.LibraryLocation) LibraryLocation {
124120
return User
125121
case rpc.LibraryLocation_LIBRARY_LOCATION_UNMANAGED:
126122
return Unmanaged
123+
default:
124+
panic(fmt.Sprintf("invalid rpc.LibraryLocation value %d", l))
127125
}
128-
panic(fmt.Sprintf("invalid rpc.LibraryLocation value %d", l))
129126
}

arduino/libraries/libraries_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package libraries
17+
18+
import (
19+
"encoding/json"
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func TestLibLayoutAndLocationJSONUnMarshaler(t *testing.T) {
26+
testLayout := func(l LibraryLayout) {
27+
d, err := json.Marshal(l)
28+
require.NoError(t, err)
29+
var m LibraryLayout
30+
err = json.Unmarshal(d, &m)
31+
require.NoError(t, err)
32+
require.Equal(t, l, m)
33+
}
34+
testLayout(FlatLayout)
35+
testLayout(RecursiveLayout)
36+
37+
testLocation := func(l LibraryLocation) {
38+
d, err := json.Marshal(l)
39+
require.NoError(t, err)
40+
var m LibraryLocation
41+
err = json.Unmarshal(d, &m)
42+
require.NoError(t, err)
43+
require.Equal(t, l, m)
44+
}
45+
testLocation(IDEBuiltIn)
46+
testLocation(PlatformBuiltIn)
47+
testLocation(ReferencedPlatformBuiltIn)
48+
testLocation(User)
49+
testLocation(Unmanaged)
50+
}

0 commit comments

Comments
 (0)