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)
|
2024-03-04 13:20:34 +00:00
|
|
|
|
2022-01-29 15:10:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
// UnmarshalEnv implements env.Unmarshaler.
|
|
|
|
func (s *StringSize) UnmarshalEnv(_ string, v string) error {
|
2022-01-29 15:10:08 +00:00
|
|
|
return s.UnmarshalJSON([]byte(`"` + v + `"`))
|
|
|
|
}
|