-
Notifications
You must be signed in to change notification settings - Fork 0
/
id.go
56 lines (46 loc) · 784 Bytes
/
id.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// id.go
//
// Copyright (c) 2018 Markku Rossi
//
// All rights reserved.
//
package storage
import (
"bytes"
"encoding/hex"
"fmt"
)
var (
EmptyID = ID{}
)
type ID struct {
Data []byte
}
func NewID(data []byte) ID {
return ID{
Data: data,
}
}
func IDFromString(input string) (id ID, err error) {
data, err := hex.DecodeString(input)
if err != nil {
return
}
return NewID(data), nil
}
func (id ID) Undefined() bool {
return len(id.Data) == 0
}
func (id ID) Equal(o ID) bool {
return bytes.Equal(id.Data, o.Data)
}
func (id ID) String() string {
if len(id.Data) > 16 {
return fmt.Sprintf("%x...%x", id.Data[0:8], id.Data[len(id.Data)-8:])
}
return fmt.Sprintf("%x", id.Data)
}
func (id ID) ToFullString() string {
return fmt.Sprintf("%x", id.Data)
}