lib/protocol: Use DeviceID in protocol messages, with custom marshalling
This makes the device ID a real type that can be used in the protobuf schema. That avoids the juggling back and forth from []byte in a bunch of places and simplifies the code. GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3695
This commit is contained in:
committed by
Audrius Butkevicius
parent
1cdfef4d6a
commit
0296c23685
@@ -16,7 +16,9 @@ import (
|
||||
"github.com/calmh/luhn"
|
||||
)
|
||||
|
||||
type DeviceID [32]byte
|
||||
const DeviceIDLength = 32
|
||||
|
||||
type DeviceID [DeviceIDLength]byte
|
||||
type ShortID uint64
|
||||
|
||||
var LocalDeviceID = DeviceID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
|
||||
@@ -114,6 +116,29 @@ func (n *DeviceID) UnmarshalText(bs []byte) error {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *DeviceID) ProtoSize() int {
|
||||
// Used by protobuf marshaller.
|
||||
return DeviceIDLength
|
||||
}
|
||||
|
||||
func (n *DeviceID) MarshalTo(bs []byte) (int, error) {
|
||||
// Used by protobuf marshaller.
|
||||
if len(bs) < DeviceIDLength {
|
||||
return 0, errors.New("destination too short")
|
||||
}
|
||||
copy(bs, (*n)[:])
|
||||
return DeviceIDLength, nil
|
||||
}
|
||||
|
||||
func (n *DeviceID) Unmarshal(bs []byte) error {
|
||||
// Used by protobuf marshaller.
|
||||
if len(bs) < DeviceIDLength {
|
||||
return errors.New("not enough data")
|
||||
}
|
||||
copy((*n)[:], bs)
|
||||
return nil
|
||||
}
|
||||
|
||||
func luhnify(s string) (string, error) {
|
||||
if len(s) != 52 {
|
||||
panic("unsupported string length")
|
||||
|
||||
Reference in New Issue
Block a user