/
bytes.go
49 lines (39 loc) · 896 Bytes
/
bytes.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
package mapper
import (
"github.com/docker/go-units"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/mapper"
)
var abbrs = []string{"", "k", "m", "g", "t", "p"}
type Bytes struct {
Field string
}
func (d Bytes) FromInternal(data map[string]interface{}) {
v, ok := data[d.Field]
if !ok {
return
}
n, err := convert.ToNumber(v)
if err != nil {
return
}
data[d.Field] = units.CustomSize("%.4g%s", float64(n), 1024.0, abbrs)
}
func (d Bytes) ToInternal(data map[string]interface{}) error {
v, ok := data[d.Field]
if !ok {
return nil
}
if str, ok := v.(string); ok {
sec, err := units.RAMInBytes(str)
if err != nil {
return err
}
data[d.Field] = sec
}
return nil
}
func (d Bytes) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
return mapper.ValidateField(d.Field, schema)
}