2022-01-29 15:10:08 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"code.cloudfoundry.org/bytefmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StringSize is a size that is unmarshaled from a string.
|
|
|
|
type StringSize uint64
|
|
|
|
|
2022-06-21 11:41:15 +00:00
|
|
|
// MarshalJSON implements json.Marshaler.
|
2022-01-29 15:10:08 +00:00
|
|
|
func (s StringSize) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(`"` + bytefmt.ByteSize(uint64(s)) + `"`), nil
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:41:15 +00:00
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
2022-01-29 15:10:08 +00:00
|
|
|
func (s *StringSize) UnmarshalJSON(b []byte) error {
|
|
|
|
var in string
|
|
|
|
if err := json.Unmarshal(b, &in); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := bytefmt.ToBytes(in)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*s = StringSize(v)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-15 23:50:47 +00:00
|
|
|
// unmarshalEnv implements envUnmarshaler.
|
2022-01-29 15:10:08 +00:00
|
|
|
func (s *StringSize) unmarshalEnv(v string) error {
|
|
|
|
return s.UnmarshalJSON([]byte(`"` + v + `"`))
|
|
|
|
}
|