forked from repbin/repbin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchliststruct.go
58 lines (51 loc) · 1.61 KB
/
fetchliststruct.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
57
58
package structs
import (
"bytes"
"github.com/repbin/repbin/message"
"github.com/repbin/repbin/utils"
"strconv"
)
const (
// FetchListStructSize is the maximum size of an encoded FetchListStruct
FetchListStructSize = 66 // messageID 45 + uint 8 + fieldsep
// FetchListStructMin is the minimum size of a fetchlist struct
FetchListStructMin = 3
)
// FetchListStruct encodes data for the list of fetch jobs
type FetchListStruct struct {
MessageID [message.MessageIDSize]byte // The messageID
TimeEntered uint64
}
// FetchListStructEncoded represents an encoded FetchList
type FetchListStructEncoded []byte
// Fill fills an encoded FetchList to maximum size
func (flse FetchListStructEncoded) Fill() FetchListStructEncoded {
if len(flse) < FetchListStructSize {
return append(flse, bytes.Repeat([]byte(" "), FetchListStructSize-len(flse))...)
}
return flse
}
// Encode a FetchListStruct to human readable format
func (fl FetchListStruct) Encode() FetchListStructEncoded {
out := make([]byte, 0, FetchListStructSize)
out = append(out, []byte(utils.B58encode(fl.MessageID[:])+" ")...)
out = append(out, []byte(strconv.FormatUint(fl.TimeEntered, 10)+" ")...)
return out[:len(out)]
}
// FetchListStructDecode decodes human readable to struct
func FetchListStructDecode(d FetchListStructEncoded) *FetchListStruct {
if len(d) < FetchListStructMin {
return nil
}
fields := bytes.Fields(d)
if len(fields) != 2 {
return nil
}
fs := new(FetchListStruct)
cur := 0
t := utils.B58decode(string(fields[cur]))
copy(fs.MessageID[:], t)
cur++
fs.TimeEntered, _ = strconv.ParseUint(string(fields[cur]), 10, 64)
return fs
}