You've already forked openaccounting-server
forked from cybercinch/openaccounting-server
49 lines
932 B
Go
49 lines
932 B
Go
|
|
package id
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/binary"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
)
|
||
|
|
|
||
|
|
// New creates a new binary ID (16 bytes) using UUID v4
|
||
|
|
func New() []byte {
|
||
|
|
u := uuid.New()
|
||
|
|
return u[:]
|
||
|
|
}
|
||
|
|
|
||
|
|
// ToUUID converts a binary ID back to UUID
|
||
|
|
func ToUUID(b []byte) (uuid.UUID, error) {
|
||
|
|
return uuid.FromBytes(b)
|
||
|
|
}
|
||
|
|
|
||
|
|
// String returns the string representation of a binary ID
|
||
|
|
func String(b []byte) string {
|
||
|
|
u, err := uuid.FromBytes(b)
|
||
|
|
if err != nil {
|
||
|
|
return ""
|
||
|
|
}
|
||
|
|
return u.String()
|
||
|
|
}
|
||
|
|
|
||
|
|
// FromString parses a string UUID into binary format
|
||
|
|
func FromString(s string) ([]byte, error) {
|
||
|
|
u, err := uuid.Parse(s)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return u[:], nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// Uint64ToBytes converts a uint64 to 8-byte slice
|
||
|
|
func Uint64ToBytes(v uint64) []byte {
|
||
|
|
b := make([]byte, 8)
|
||
|
|
binary.BigEndian.PutUint64(b, v)
|
||
|
|
return b
|
||
|
|
}
|
||
|
|
|
||
|
|
// BytesToUint64 converts 8-byte slice to uint64
|
||
|
|
func BytesToUint64(b []byte) uint64 {
|
||
|
|
return binary.BigEndian.Uint64(b)
|
||
|
|
}
|