36 lines
651 B
Go
36 lines
651 B
Go
|
package change
|
||
|
|
||
|
import "io"
|
||
|
|
||
|
func Write(changes []Change, w io.Writer) error {
|
||
|
count := uint64(len(changes))
|
||
|
if err := writeBin(w, count); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
for _, c := range changes {
|
||
|
if err := c.writeTo(w); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func Read(changes []Change, r io.Reader) ([]Change, error) {
|
||
|
var count uint64
|
||
|
if err := readBin(r, &count); err != nil {
|
||
|
return changes, err
|
||
|
}
|
||
|
|
||
|
if uint64(len(changes)) < count {
|
||
|
changes = make([]Change, count)
|
||
|
}
|
||
|
changes = changes[:count]
|
||
|
|
||
|
for i := range changes {
|
||
|
if err := changes[i].readFrom(r); err != nil {
|
||
|
return changes, err
|
||
|
}
|
||
|
}
|
||
|
return changes, nil
|
||
|
}
|