Better naming for the ID length constant

pull/10616/head
Leonardo Di Donato 2018-07-31 02:20:08 +02:00 committed by Leonardo Di Donato
parent 02a80a0665
commit c16a855a58
4 changed files with 13 additions and 13 deletions

View File

@ -309,9 +309,9 @@ func bucketIndexKey(b *platform.Bucket) ([]byte, error) {
if err != nil {
return nil, err
}
k := make([]byte, platform.IDLength+len(b.Name))
k := make([]byte, platform.IDStringLength+len(b.Name))
copy(k, orgID)
copy(k[platform.IDLength:], []byte(b.Name))
copy(k[platform.IDStringLength:], []byte(b.Name))
return k, nil
}

14
id.go
View File

@ -8,8 +8,8 @@ import (
"fmt"
)
// IDLength is the exact length a byte slice must have in order to be decoded into an ID
const IDLength = 16
// IDStringLength 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
// ID is a unique identifier.
//
@ -39,14 +39,14 @@ func IDFromString(str string) (*ID, error) {
// It errors if the input byte slice does not have the correct length
// or if it contains all zeros.
func (i *ID) Decode(b []byte) error {
if len(b) != IDLength {
return fmt.Errorf("input must be an array of %d bytes", IDLength)
if len(b) != IDStringLength {
return fmt.Errorf("input must be an array of %d bytes", IDStringLength)
}
if bytes.Equal(b, make([]byte, IDLength)) {
if bytes.Equal(b, make([]byte, IDStringLength)) {
return fmt.Errorf("all 0s is not a valid ID")
}
dst := make([]byte, hex.DecodedLen(IDLength))
dst := make([]byte, hex.DecodedLen(IDStringLength))
_, err := hex.Decode(dst, b)
if err != nil {
return err
@ -68,7 +68,7 @@ func (i ID) Encode() ([]byte, error) {
return nil, fmt.Errorf("all 0s is not a valid ID")
}
b := make([]byte, hex.DecodedLen(IDLength))
b := make([]byte, hex.DecodedLen(IDStringLength))
binary.BigEndian.PutUint64(b, uint64(i))
dst := make([]byte, hex.EncodedLen(len(b)))

View File

@ -45,7 +45,7 @@ func TestIDFromString(t *testing.T) {
name: "Should not be able to decode inputs with length other than 16 bytes",
id: "abc",
wantErr: true,
err: fmt.Sprintf("input must be an array of %d bytes", platform.IDLength),
err: fmt.Sprintf("input must be an array of %d bytes", platform.IDStringLength),
},
}
for _, tt := range tests {
@ -109,7 +109,7 @@ func TestEncode(t *testing.T) {
func TestDecodeFromAllZeros(t *testing.T) {
var id platform.ID
err := id.Decode(make([]byte, platform.IDLength))
err := id.Decode(make([]byte, platform.IDStringLength))
if err == nil {
t.Errorf("expecting all zeros ID to not be a valid ID")
}

View File

@ -6,14 +6,14 @@ import (
"github.com/influxdata/platform"
)
func TestIDLength(t *testing.T) {
func TestIDStringLength(t *testing.T) {
gen := NewIDGenerator()
id := gen.ID()
if !id.Valid() {
t.Fail()
}
enc, _ := id.Encode()
if len(enc) != platform.IDLength {
if len(enc) != platform.IDStringLength {
t.Fail()
}
}