-
Notifications
You must be signed in to change notification settings - Fork 390
/
csvtypes.go
61 lines (49 loc) · 1.45 KB
/
csvtypes.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
59
60
61
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package compensation
import (
"time"
"storj.io/common/storj"
)
// NodeID is a wrapper type around storj.NodeID that implements CSV helpers.
type NodeID storj.NodeID
// Bytes calls the underlying type's Bytes function.
func (nodeID NodeID) Bytes() []byte {
return storj.NodeID(nodeID).Bytes()
}
// String calls the underlying type's String function.
func (nodeID NodeID) String() string {
return storj.NodeID(nodeID).String()
}
// UnmarshalCSV reads the csv entry into a storj.NodeID.
func (nodeID *NodeID) UnmarshalCSV(s string) error {
v, err := storj.NodeIDFromString(s)
if err != nil {
return err
}
*nodeID = NodeID(v)
return nil
}
// MarshalCSV writes the storj.NodeID into a CSV entry.
func (nodeID NodeID) MarshalCSV() (string, error) {
return nodeID.String(), nil
}
// UTCDate is a wrapper type around time.Time that implements CSV helpers.
type UTCDate time.Time
// String formats the date into YYYY-MM-DD.
func (date UTCDate) String() string {
return time.Time(date).In(time.UTC).Format("2006-01-02")
}
// UnmarshalCSV reads the YYYY-MM-DD date into the date.
func (date *UTCDate) UnmarshalCSV(s string) error {
v, err := time.Parse("2006-01-02", s)
if err != nil {
return err
}
*date = UTCDate(v)
return nil
}
// MarshalCSV writes out a CSV row containing the YYYY-MM-DD of the time.
func (date UTCDate) MarshalCSV() (string, error) {
return date.String(), nil
}