refactor(id): update name to IDLength
parent
21475c22cd
commit
b68a98043d
|
@ -308,9 +308,9 @@ func bucketIndexKey(b *platform.Bucket) ([]byte, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
k := make([]byte, platform.IDStringLength+len(b.Name))
|
k := make([]byte, platform.IDLength+len(b.Name))
|
||||||
copy(k, orgID)
|
copy(k, orgID)
|
||||||
copy(k[platform.IDStringLength:], []byte(b.Name))
|
copy(k[platform.IDLength:], []byte(b.Name))
|
||||||
return k, nil
|
return k, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
id.go
10
id.go
|
@ -7,8 +7,8 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IDStringLength is the exact length a string (or a byte slice representing it) must have in order to be decoded into a valid ID.
|
// IDLength is the exact length a string (or a byte slice representing it) must have in order to be decoded into a valid ID.
|
||||||
const IDStringLength = 16
|
const IDLength = 16
|
||||||
|
|
||||||
// ErrInvalidID is the error thrown to notify invalid IDs.
|
// ErrInvalidID is the error thrown to notify invalid IDs.
|
||||||
var ErrInvalidID = errors.New("invalid ID")
|
var ErrInvalidID = errors.New("invalid ID")
|
||||||
|
@ -49,11 +49,11 @@ func InvalidID() ID {
|
||||||
// It errors if the input byte slice does not have the correct length
|
// It errors if the input byte slice does not have the correct length
|
||||||
// or if it contains all zeros.
|
// or if it contains all zeros.
|
||||||
func (i *ID) Decode(b []byte) error {
|
func (i *ID) Decode(b []byte) error {
|
||||||
if len(b) != IDStringLength {
|
if len(b) != IDLength {
|
||||||
return ErrInvalidIDLength
|
return ErrInvalidIDLength
|
||||||
}
|
}
|
||||||
|
|
||||||
dst := make([]byte, hex.DecodedLen(IDStringLength))
|
dst := make([]byte, hex.DecodedLen(IDLength))
|
||||||
_, err := hex.Decode(dst, b)
|
_, err := hex.Decode(dst, b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -80,7 +80,7 @@ func (i ID) Encode() ([]byte, error) {
|
||||||
return nil, ErrInvalidID
|
return nil, ErrInvalidID
|
||||||
}
|
}
|
||||||
|
|
||||||
b := make([]byte, hex.DecodedLen(IDStringLength))
|
b := make([]byte, hex.DecodedLen(IDLength))
|
||||||
binary.BigEndian.PutUint64(b, uint64(i))
|
binary.BigEndian.PutUint64(b, uint64(i))
|
||||||
|
|
||||||
dst := make([]byte, hex.EncodedLen(len(b)))
|
dst := make([]byte, hex.EncodedLen(len(b)))
|
||||||
|
|
|
@ -114,7 +114,7 @@ func TestEncode(t *testing.T) {
|
||||||
|
|
||||||
func TestDecodeFromAllZeros(t *testing.T) {
|
func TestDecodeFromAllZeros(t *testing.T) {
|
||||||
var id platform.ID
|
var id platform.ID
|
||||||
err := id.Decode(make([]byte, platform.IDStringLength))
|
err := id.Decode(make([]byte, platform.IDLength))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expecting all zeros ID to not be a valid ID")
|
t.Errorf("expecting all zeros ID to not be a valid ID")
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,14 +6,14 @@ import (
|
||||||
"github.com/influxdata/platform"
|
"github.com/influxdata/platform"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIDStringLength(t *testing.T) {
|
func TestIDLength(t *testing.T) {
|
||||||
gen := NewIDGenerator()
|
gen := NewIDGenerator()
|
||||||
id := gen.ID()
|
id := gen.ID()
|
||||||
if !id.Valid() {
|
if !id.Valid() {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
enc, _ := id.Encode()
|
enc, _ := id.Encode()
|
||||||
if len(enc) != platform.IDStringLength {
|
if len(enc) != platform.IDLength {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue