-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsize_bytes.go
More file actions
42 lines (37 loc) · 887 Bytes
/
size_bytes.go
File metadata and controls
42 lines (37 loc) · 887 Bytes
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
package baker
import (
"fmt"
"math"
"github.com/dustin/go-humanize"
)
type SizeBytes uint64
func (b *SizeBytes) UnmarshalTOML(p interface{}) error {
var actual uint64
switch v := p.(type) {
case float64:
if v < 0 {
return fmt.Errorf("invalid size in bytes(%v): value must >= 0", v)
}
if v >= math.MaxUint64 {
return fmt.Errorf("invalid size in bytes (%v): value must be smaller than %v", v, uint64(math.MaxUint64))
}
actual = uint64(v)
case int64:
if v < 0 {
return fmt.Errorf("invalid size in bytes (%v): value must be >= 0", v)
}
actual = uint64(v)
case string:
if v != "" {
var err error
actual, err = humanize.ParseBytes(v)
if err != nil {
return fmt.Errorf("invalid size in bytes (%v): %v", v, err)
}
}
default:
return fmt.Errorf("unexpected type (%T): unexpected value type", v)
}
*b = SizeBytes(actual)
return nil
}