Documentation
¶
Index ¶
- func CalcCheckSum(b []byte) string
- type Safe
- func (s *Safe) AddBytes(cnt int64)
- func (s *Safe) AddLine()
- func (s *Safe) Checksum() string
- func (s *Safe) Created() string
- func (s *Safe) IsDir() bool
- func (s *Safe) JSONBytes() []byte
- func (s *Safe) JSONString() string
- func (s *Safe) ParseCreated() time.Time
- func (s *Safe) Path() string
- func (s *Safe) SetChecksum(hsh hash.Hash)
- func (s *Safe) SetCreated(t time.Time)
- func (s *Safe) SetDir(isDir bool)
- func (s *Safe) SetPath(pth string)
- func (s *Safe) SetSize(size int64)
- func (s *Safe) Stats() Stats
- type Stats
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalcCheckSum ¶ added in v0.30.0
CalcCheckSum creates a md5 hash based on the bytes passed in. This is a common method to get a checksum of a file.
Types ¶
type Safe ¶ added in v0.30.0
type Safe struct {
// LineCnt returns the file line count.
LineCnt int64 `json:"linecnt,omitempty"`
// ByteCount returns uncompressed raw file byte count.
ByteCnt int64 `json:"bytecnt,omitempty"`
// Size holds the actual file size.
Size int64 `json:"size"`
Files int64 `json:"files,omitempty"`
// contains filtered or unexported fields
}
Safe is thread safe without using mutex by using atomic values for all fields Do not directly modify on the values in this struct.
func (*Safe) AddBytes ¶ added in v0.30.0
AddBytes will atomically and safely increment ByteCnt by 'cnt'.
Example ¶
sts := Safe{}
sts.AddBytes(1)
sts.AddBytes(10)
sts.AddBytes(1100)
fmt.Println(sts.ByteCnt) // 1111
Output: 1111
func (*Safe) AddLine ¶ added in v0.30.0
func (s *Safe) AddLine()
AddLine will atomically and safely increment LineCnt by one.
Example ¶
sts := Safe{}
sts.AddLine()
sts.AddLine()
sts.AddLine()
fmt.Println(sts.LineCnt) // 3
Output: 3
func (*Safe) JSONString ¶ added in v0.30.0
func (*Safe) ParseCreated ¶ added in v0.30.0
ParseCreated will attempt to parse the Created field to a time.Time object. ParseCreated expects the Created time string is in time.RFC3339. If there is a parse error then the time.Time zero value is returned.
The returned time will always be in UTC.
func (*Safe) SetChecksum ¶ added in v0.30.0
SetChecksum will correctly calculate and set the base64 encoded checksum.
Example ¶
sts := Safe{}
// checksum
hsh := md5.New()
hsh.Write([]byte("test message"))
sts.SetChecksum(hsh)
fmt.Println(sts.Checksum()) // c72b9698fa1927e1dd12d3cf26ed84b2
Output: c72b9698fa1927e1dd12d3cf26ed84b2
func (*Safe) SetCreated ¶ added in v0.30.0
SetCreated will set the Created field in the format time.RFC3339 in UTC.
Example ¶
sts := Safe{}
created := "2017-01-02T03:04:05Z"
t, _ := time.Parse(time.RFC3339, created)
sts.SetCreated(t)
fmt.Println(sts.Created()) // 2017-01-02T03:04:05Z
Output: 2017-01-02T03:04:05Z
func (*Safe) SetPath ¶ added in v0.30.0
Example ¶
sts := Safe{}
sts.SetPath("path/to/file.txt")
fmt.Println(sts.Path()) // path/to/file.txt
Output: path/to/file.txt
type Stats ¶
type Stats struct {
LineCnt int64 `json:"linecnt,omitempty"`
// ByteCount returns uncompressed raw file byte count.
ByteCnt int64 `json:"bytecnt,omitempty"`
// Size holds the actual file size.
Size int64 `json:"size"`
// Checksum base64 encoded string of the file md5 hash
Checksum string `json:"checksum,omitempty"`
// Path returns the full absolute path of the file.
Path string `json:"path" uri:"origin"`
// Created date the file was created or last updated Format(time.RFC3339)
// whichever is more recent.
Created string `json:"created"`
IsDir bool `json:"isDir,omitempty"`
Files int64 `json:"files,omitempty"`
}
Stats is an immutable struct describe file details Safe should be used for any needed changes
func NewFromBytes ¶
NewFromBytes creates Stats from json bytes.
Example ¶
sts := NewFromBytes([]byte(`{"linecnt":10,"bytecnt":100,"size":200,"checksum":"test checksum","path":"test path","created":"test created"}`))
fmt.Println(sts.LineCnt) // 10
fmt.Println(sts.ByteCnt) // 100
fmt.Println(sts.Size) // 200
fmt.Println(sts.Checksum) // test checksum
fmt.Println(sts.Path) // test path
fmt.Println(sts.Created) // test created
Output: 10 100 200 test checksum test path test created
func NewFromInfo ¶
NewFromInfo creates Stats from a uri formatted info string.
func (Stats) JSONBytes ¶
Example ¶
sts := Stats{
LineCnt: 10,
ByteCnt: 100,
Size: 200,
Checksum: "test checksum",
Path: "test path",
Created: "test created",
}
b := sts.JSONBytes()
fmt.Println(string(b)) // {"linecnt":10,"bytecnt":100,"size":200,"checksum":"test checksum","path":"test path","created":"test created"}
Output: {"linecnt":10,"bytecnt":100,"size":200,"checksum":"test checksum","path":"test path","created":"test created"}
func (Stats) JSONString ¶
Example ¶
sts := Stats{
LineCnt: 10,
ByteCnt: 100,
Size: 200,
Checksum: "test checksum",
Path: "test path",
Created: "test created",
}
s := sts.JSONString()
fmt.Println(s) // {"linecnt":10,"bytecnt":100,"size":200,"checksum":"test checksum","path":"test path","created":"test created"}
Output: {"linecnt":10,"bytecnt":100,"size":200,"checksum":"test checksum","path":"test path","created":"test created"}
func (Stats) ParseCreated ¶
ParseCreated converts the store string timestamp to a time.Time value
Example ¶
sts := New() sts.Created = "2017-01-02T03:04:05Z" t := sts.ParseCreated() fmt.Println(t.Format(time.RFC3339)) // 2017-01-02T03:04:05Z
Output: 2017-01-02T03:04:05Z
func (Stats) ToSafe ¶ added in v0.30.0
Example ¶
sts := Stats{
LineCnt: 1,
ByteCnt: 2,
Size: 3,
Checksum: "4",
Path: "5",
Created: "6",
}
cln := sts.ToSafe()
fmt.Println(cln.LineCnt)
fmt.Println(cln.ByteCnt)
fmt.Println(cln.Size)
fmt.Println(cln.Checksum())
fmt.Println(cln.Path())
fmt.Println(cln.Created())
Output: 1 2 3 4 5 6