diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-10-24 07:20:10 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-10-24 07:20:29 +0200 |
| commit | 18476c6cba2d5d2bb3ecf3ad136dacbf41d0360e (patch) | |
| tree | a7a958ff7a4bf14ed7f7ee3ffd9c84b7592facbd /core/core.go | |
feat: Extract from other repo
Diffstat (limited to 'core/core.go')
| -rw-r--r-- | core/core.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/core/core.go b/core/core.go new file mode 100644 index 0000000..5cd654d --- /dev/null +++ b/core/core.go @@ -0,0 +1,34 @@ +package core + +import ( + "encoding/base64" + + "github.com/gofrs/uuid" +) + +type ID struct { + uuid.UUID +} + +func NewID() ID { + id, err := uuid.NewV4() + Expect(err, "error generating uuid") + + return ID{id} +} + +func (id ID) String() string { + return base64.RawURLEncoding.EncodeToString(id.Bytes()) +} + +func ParseID(value string) (ID, error) { + bytes, err := base64.RawURLEncoding.DecodeString(value) + if err != nil { + return ID{}, Wrap(err, "error decoding value") + } + id, err := uuid.FromBytes(bytes) + if err != nil { + return ID{}, Wrap(err, "error parsing uuid") + } + return ID{id}, nil +} |
