Skip to content

Commit 746c286

Browse files
cmagliealessio-perugini
authored andcommitted
Cache library properties
1 parent 620d16d commit 746c286

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

internal/arduino/libraries/libraries.go

+41-9
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ func (library *Library) MarshalBinary(out io.Writer, prefix *paths.Path) error {
126126
}
127127
return nil
128128
}
129+
writeProperties := func(in *properties.Map) error {
130+
keys := in.Keys()
131+
if err := binary.Write(out, binary.NativeEndian, uint16(len(keys))); err != nil {
132+
return err
133+
}
134+
for _, k := range keys {
135+
v := in.Get(k)
136+
if err := writeString(k); err != nil {
137+
return err
138+
}
139+
if err := writeString(v); err != nil {
140+
return err
141+
}
142+
}
143+
return nil
144+
}
129145
writePath := func(in *paths.Path) error {
130146
if in == nil {
131147
return writeString("")
@@ -216,7 +232,6 @@ func (library *Library) MarshalBinary(out io.Writer, prefix *paths.Path) error {
216232
if err := writeString(library.License); err != nil {
217233
return err
218234
}
219-
//writeStringArray(library.Properties.AsSlice())
220235
if err := writePathList(library.Examples); err != nil {
221236
return err
222237
}
@@ -229,6 +244,10 @@ func (library *Library) MarshalBinary(out io.Writer, prefix *paths.Path) error {
229244
if err := writeMap(library.CompatibleWith); err != nil {
230245
return err
231246
}
247+
if err := writeProperties(library.Properties); err != nil {
248+
return err
249+
}
250+
232251
return nil
233252
}
234253

@@ -305,6 +324,23 @@ func (library *Library) UnmarshalBinary(in io.Reader, prefix *paths.Path) error
305324
}
306325
return list, nil
307326
}
327+
readProperties := func() (*properties.Map, error) {
328+
var len uint16
329+
if err := binary.Read(in, binary.NativeEndian, &len); err != nil {
330+
return nil, err
331+
}
332+
props := properties.NewMap()
333+
for range len {
334+
if k, err := readString(); err != nil {
335+
return nil, err
336+
} else if v, err := readString(); err != nil {
337+
return nil, err
338+
} else {
339+
props.Set(k, v)
340+
}
341+
}
342+
return props, nil
343+
}
308344
var err error
309345
library.Name, err = readString()
310346
if err != nil {
@@ -397,14 +433,6 @@ func (library *Library) UnmarshalBinary(in io.Reader, prefix *paths.Path) error
397433
if err != nil {
398434
return err
399435
}
400-
// props, err := readStringArray()
401-
// if err != nil {
402-
// return err
403-
// }
404-
// library.Properties, err = properties.LoadFromSlice(props)
405-
// if err != nil {
406-
// return err
407-
// }
408436
library.Examples, err = readPathList()
409437
if err != nil {
410438
return err
@@ -421,6 +449,10 @@ func (library *Library) UnmarshalBinary(in io.Reader, prefix *paths.Path) error
421449
if err != nil {
422450
return err
423451
}
452+
library.Properties, err = readProperties()
453+
if err != nil {
454+
return err
455+
}
424456
return nil
425457
}
426458

0 commit comments

Comments
 (0)