Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf6d9286

Browse files
cmagliealessio-perugini
authored andcommitted
Using LibrariesList / refactored BinaryMarshal metods
1 parente973cc9 commitf6d9286

File tree

3 files changed

+80
-70
lines changed

3 files changed

+80
-70
lines changed

‎internal/arduino/libraries/libraries.go‎

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package libraries
1717

1818
import (
19-
"bytes"
2019
"encoding/binary"
2120
"errors"
2221
"fmt"
@@ -93,18 +92,17 @@ func (library *Library) String() string {
9392
returnlibrary.Name+"@"+library.Version.String()
9493
}
9594

96-
func (library*Library)MarshalBinary() ([]byte,error) {
97-
buffer:=bytes.NewBuffer(make([]byte,0,4096))
95+
func (library*Library)MarshalBinary(out io.Writer)error {
9896
writeString:=func(instring)error {
9997
inBytes:= []byte(in)
100-
iferr:=binary.Write(buffer,binary.NativeEndian,uint16(len(inBytes)));err!=nil {
98+
iferr:=binary.Write(out,binary.NativeEndian,uint16(len(inBytes)));err!=nil {
10199
returnerr
102100
}
103-
_,err:=buffer.Write(inBytes)
101+
_,err:=out.Write(inBytes)
104102
returnerr
105103
}
106104
writeStringArray:=func(in []string)error {
107-
iferr:=binary.Write(buffer,binary.NativeEndian,uint16(len(in)));err!=nil {
105+
iferr:=binary.Write(out,binary.NativeEndian,uint16(len(in)));err!=nil {
108106
returnerr
109107
}
110108
for_,i:=rangein {
@@ -115,14 +113,14 @@ func (library *Library) MarshalBinary() ([]byte, error) {
115113
returnnil
116114
}
117115
writeMap:=func(inmap[string]bool)error {
118-
iferr:=binary.Write(buffer,binary.NativeEndian,uint16(len(in)));err!=nil {
116+
iferr:=binary.Write(out,binary.NativeEndian,uint16(len(in)));err!=nil {
119117
returnerr
120118
}
121119
fork,v:=rangein {
122120
iferr:=writeString(k);err!=nil {
123121
returnerr
124122
}
125-
iferr:=binary.Write(buffer,binary.NativeEndian,v);err!=nil {
123+
iferr:=binary.Write(out,binary.NativeEndian,v);err!=nil {
126124
returnerr
127125
}
128126
}
@@ -136,89 +134,89 @@ func (library *Library) MarshalBinary() ([]byte, error) {
136134
}
137135
}
138136
iferr:=writeString(library.Name);err!=nil {
139-
returnnil,err
137+
returnerr
140138
}
141139
iferr:=writeString(library.Author);err!=nil {
142-
returnnil,err
140+
returnerr
143141
}
144142
iferr:=writeString(library.Maintainer);err!=nil {
145-
returnnil,err
143+
returnerr
146144
}
147145
iferr:=writeString(library.Sentence);err!=nil {
148-
returnnil,err
146+
returnerr
149147
}
150148
iferr:=writeString(library.Paragraph);err!=nil {
151-
returnnil,err
149+
returnerr
152150
}
153151
iferr:=writeString(library.Website);err!=nil {
154-
returnnil,err
152+
returnerr
155153
}
156154
iferr:=writeString(library.Category);err!=nil {
157-
returnnil,err
155+
returnerr
158156
}
159157
iferr:=writeStringArray(library.Architectures);err!=nil {
160-
returnnil,err
158+
returnerr
161159
}
162160
iferr:=writeStringArray(library.Types);err!=nil {
163-
returnnil,err
161+
returnerr
164162
}
165163
iferr:=writePath(library.InstallDir);err!=nil {
166-
returnnil,err
164+
returnerr
167165
}
168166
iferr:=writeString(library.DirName);err!=nil {
169-
returnnil,err
167+
returnerr
170168
}
171169
iferr:=writePath(library.SourceDir);err!=nil {
172-
returnnil,err
170+
returnerr
173171
}
174172
iferr:=writePath(library.UtilityDir);err!=nil {
175-
returnnil,err
173+
returnerr
176174
}
177-
iferr:=binary.Write(buffer,binary.NativeEndian,int32(library.Location));err!=nil {
178-
returnnil,err
175+
iferr:=binary.Write(out,binary.NativeEndian,int32(library.Location));err!=nil {
176+
returnerr
179177
}
180178
// library.ContainerPlatform *cores.PlatformRelease `json:""`
181-
iferr:=binary.Write(buffer,binary.NativeEndian,int32(library.Layout));err!=nil {
182-
returnnil,err
179+
iferr:=binary.Write(out,binary.NativeEndian,int32(library.Layout));err!=nil {
180+
returnerr
183181
}
184-
iferr:=binary.Write(buffer,binary.NativeEndian,library.DotALinkage);err!=nil {
185-
returnnil,err
182+
iferr:=binary.Write(out,binary.NativeEndian,library.DotALinkage);err!=nil {
183+
returnerr
186184
}
187-
iferr:=binary.Write(buffer,binary.NativeEndian,library.Precompiled);err!=nil {
188-
returnnil,err
185+
iferr:=binary.Write(out,binary.NativeEndian,library.Precompiled);err!=nil {
186+
returnerr
189187
}
190-
iferr:=binary.Write(buffer,binary.NativeEndian,library.PrecompiledWithSources);err!=nil {
191-
returnnil,err
188+
iferr:=binary.Write(out,binary.NativeEndian,library.PrecompiledWithSources);err!=nil {
189+
returnerr
192190
}
193191
iferr:=writeString(library.LDflags);err!=nil {
194-
returnnil,err
192+
returnerr
195193
}
196-
iferr:=binary.Write(buffer,binary.NativeEndian,library.IsLegacy);err!=nil {
197-
returnnil,err
194+
iferr:=binary.Write(out,binary.NativeEndian,library.IsLegacy);err!=nil {
195+
returnerr
198196
}
199-
iferr:=binary.Write(buffer,binary.NativeEndian,library.InDevelopment);err!=nil {
200-
returnnil,err
197+
iferr:=binary.Write(out,binary.NativeEndian,library.InDevelopment);err!=nil {
198+
returnerr
201199
}
202200
iferr:=writeString(library.Version.String());err!=nil {
203-
returnnil,err
201+
returnerr
204202
}
205203
iferr:=writeString(library.License);err!=nil {
206-
returnnil,err
204+
returnerr
207205
}
208206
//writeStringArray(library.Properties.AsSlice())
209207
iferr:=writeStringArray(library.Examples.AsStrings());err!=nil {
210-
returnnil,err
208+
returnerr
211209
}
212210
iferr:=writeStringArray(library.declaredHeaders);err!=nil {
213-
returnnil,err
211+
returnerr
214212
}
215213
iferr:=writeStringArray(library.sourceHeaders);err!=nil {
216-
returnnil,err
214+
returnerr
217215
}
218216
iferr:=writeMap(library.CompatibleWith);err!=nil {
219-
returnnil,err
217+
returnerr
220218
}
221-
returnbuffer.Bytes(),nil
219+
returnnil
222220
}
223221

224222
func (library*Library)UnmarshalBinary(in io.Reader)error {

‎internal/arduino/libraries/librarieslist.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
package libraries
1717

1818
import (
19+
"encoding/binary"
20+
"fmt"
21+
"io"
1922
"sort"
2023

2124
semver"go.bug.st/relaxed-semver"
@@ -41,6 +44,35 @@ func (list *List) Add(libs ...*Library) {
4144
}
4245
}
4346

47+
func (list*List)UnmarshalBinary(in io.Reader)error {
48+
varnint32
49+
iferr:=binary.Read(in,binary.NativeEndian,&n);err!=nil {
50+
returnerr
51+
}
52+
res:=make([]*Library,n)
53+
fori:=rangeres {
54+
varlibLibrary
55+
iferr:=lib.UnmarshalBinary(in);err!=nil {
56+
returnerr
57+
}
58+
res[i]=&lib
59+
}
60+
*list=res
61+
returnnil
62+
}
63+
64+
func (list*List)MarshalBinary(out io.Writer)error {
65+
iferr:=binary.Write(out,binary.NativeEndian,int32(len(*list)));err!=nil {
66+
returnerr
67+
}
68+
for_,lib:=range*list {
69+
iferr:=lib.MarshalBinary(out);err!=nil {
70+
returnfmt.Errorf("could not encode lib data of %s: %w",lib.InstallDir.String(),err)
71+
}
72+
}
73+
returnnil
74+
}
75+
4476
// Remove removes the given library from the list
4577
func (list*List)Remove(libraryToRemove*Library) {
4678
fori,lib:=range*list {

‎internal/arduino/libraries/librariesmanager/librariesmanager.go‎

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package librariesmanager
1717

1818
import (
19-
"encoding/binary"
2019
"errors"
2120
"fmt"
2221
"os"
@@ -202,7 +201,7 @@ func (lm *LibrariesManager) loadLibrariesFromDir(librariesDir *LibrariesDir) []*
202201

203202
librariesDir.scanned=true
204203

205-
varloadedLibs[]*libraries.Library
204+
varloadedLibs libraries.List
206205
ifcacheFilePath:=librariesDir.Path.Join("libraries-loader-cache");cacheFilePath.Exist() {
207206
logrus.WithField("file",cacheFilePath).Info("Using library cache")
208207

@@ -213,20 +212,11 @@ func (lm *LibrariesManager) loadLibrariesFromDir(librariesDir *LibrariesDir) []*
213212
returnappend(statuses,s)
214213
}
215214
defercache.Close()
216-
varnint32
217-
iferr:=binary.Read(cache,binary.NativeEndian,&n);err!=nil {
215+
216+
iferr:=loadedLibs.UnmarshalBinary(cache);err!=nil {
218217
s:=status.Newf(codes.FailedPrecondition,"reading lib cache %[1]s: %[2]s",cacheFilePath,err)
219218
returnappend(statuses,s)
220219
}
221-
loadedLibs=make([]*libraries.Library,n)
222-
fori:=rangeloadedLibs {
223-
varlib libraries.Library
224-
iferr:=lib.UnmarshalBinary(cache);err!=nil {
225-
s:=status.Newf(codes.FailedPrecondition,"reading lib cache %[1]s: %[2]s",cacheFilePath,err)
226-
returnappend(statuses,s)
227-
}
228-
loadedLibs[i]=&lib
229-
}
230220
}else {
231221
varlibDirs paths.PathList
232222
iflibrariesDir.IsSingleLibrary {
@@ -258,26 +248,16 @@ func (lm *LibrariesManager) loadLibrariesFromDir(librariesDir *LibrariesDir) []*
258248
// Write lib cache
259249
cache,err:=cacheFilePath.Create()
260250
iferr!=nil {
261-
s:=status.Newf(codes.FailedPrecondition,"writing lib cache %[1]s: %[2]s",cacheFilePath,err)
251+
s:=status.Newf(codes.FailedPrecondition,"creating lib cache %[1]s: %[2]s",cacheFilePath,err)
262252
returnappend(statuses,s)
263253
}
264-
defercache.Close()
265-
iferr:=binary.Write(cache,binary.NativeEndian,int32(len(loadedLibs)));err!=nil {
254+
err=loadedLibs.MarshalBinary(cache)
255+
cache.Close()
256+
iferr!=nil {
266257
cacheFilePath.Remove()
267258
s:=status.Newf(codes.FailedPrecondition,"writing lib cache %[1]s: %[2]s",cacheFilePath,err)
268259
returnappend(statuses,s)
269260
}
270-
for_,lib:=rangeloadedLibs {
271-
data,err:=lib.MarshalBinary()
272-
iferr!=nil {
273-
panic("could not encode lib data for: "+lib.InstallDir.String())
274-
}
275-
if_,err:=cache.Write(data);err!=nil {
276-
cacheFilePath.Remove()
277-
s:=status.Newf(codes.FailedPrecondition,"writing lib cache %[1]s: %[2]s",cacheFilePath,err)
278-
returnappend(statuses,s)
279-
}
280-
}
281261
}
282262

283263
for_,library:=rangeloadedLibs {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp