Replace IntToBytes(value int) and ToBytes() with a single ToBytes() function

This commit is contained in:
2024-12-05 17:50:40 +03:00
parent 589a94216b
commit caff27b43d
6 changed files with 20 additions and 27 deletions

View File

@@ -82,15 +82,8 @@ func StringToDataTyp(str string) (DataTyp, error) {
}
}
// Convert DataTyp to []byte
func (d DataTyp) ToBytes() []byte {
bytes := make([]byte, 2)
binary.BigEndian.PutUint16(bytes, uint16(d))
return bytes
}
// Convert int to []byte
func IntToBytes(value int) []byte {
// ToBytes converts DataTyp or int to a byte slice
func ToBytes[T ~uint16 | int](value T) []byte {
bytes := make([]byte, 2)
binary.BigEndian.PutUint16(bytes, uint16(value))
return bytes

View File

@@ -61,9 +61,9 @@ func GetTransferData(ctx context.Context, db storage.PrefixDb, publicKey string,
data := make(map[DataTyp]string)
for _, key := range keys {
value, err := db.Get(ctx, key.ToBytes())
value, err := db.Get(ctx, ToBytes(key))
if err != nil {
return "", fmt.Errorf("failed to get %s: %v", key.ToBytes(), err)
return "", fmt.Errorf("failed to get %s: %v", ToBytes(key), err)
}
data[key] = string(value)
}

View File

@@ -68,9 +68,9 @@ func GetVoucherData(ctx context.Context, db storage.PrefixDb, input string) (*da
data := make(map[DataTyp]string)
for _, key := range keys {
value, err := db.Get(ctx, key.ToBytes())
value, err := db.Get(ctx, ToBytes(key))
if err != nil {
return nil, fmt.Errorf("failed to get %s: %v", key.ToBytes(), err)
return nil, fmt.Errorf("failed to get %s: %v", ToBytes(key), err)
}
data[key] = string(value)
}

View File

@@ -85,7 +85,7 @@ func TestGetVoucherData(t *testing.T) {
t.Fatal(err)
}
prefix := IntToBytes(visedb.DATATYPE_USERDATA)
prefix := ToBytes(visedb.DATATYPE_USERDATA)
spdb := storage.NewSubPrefixDb(db, prefix)
// Test voucher data
@@ -98,7 +98,7 @@ func TestGetVoucherData(t *testing.T) {
// Put the data
for key, value := range mockData {
err = spdb.Put(ctx, []byte(key.ToBytes()), []byte(value))
err = spdb.Put(ctx, []byte(ToBytes(key)), []byte(value))
if err != nil {
t.Fatal(err)
}