@@ -118,31 +118,38 @@ func (f FileSize) MarshalJSON() ([]byte, error) {
118118 return fmt .Appendf (nil , `"%s"` , f .String ()), nil
119119}
120120
121- // UnmarshalJSON implements the json.Unmarshaler interface for FileSize
121+ // UnmarshalJSON implements the json.Unmarshaler interface for FileSize.
122+ // It can parse either number (nn) or string (nn KB, nn MB).
122123func (f * FileSize ) UnmarshalJSON (data []byte ) error {
123- var usage string
124- if err := json .Unmarshal (data , & usage ); err != nil {
125- return err
124+ var sizeNum uint64
125+ if err := json .Unmarshal (data , & sizeNum ); err == nil {
126+ * f = FileSize (sizeNum )
127+ return nil
128+ }
129+
130+ var sizeStr string
131+ if err := json .Unmarshal (data , & sizeStr ); err != nil {
132+ return fmt .Errorf ("cannot unmarshal FileSize value(%s) to neither string nor uint64: %w" , data , err )
126133 }
127134
128- bytes , err := parseUsageToBytes ( usage )
135+ size , err := parseSizeStrToBytes ( sizeStr )
129136 if err != nil {
130137 return err
131138 }
132139
133- * f = FileSize (bytes )
140+ * f = FileSize (size )
134141 return nil
135142}
136143
137- func parseUsageToBytes (usage string ) (int64 , error ) {
144+ func parseSizeStrToBytes (usage string ) (int64 , error ) {
138145 parts := strings .Fields (usage )
139146 if len (parts ) < 1 {
140147 return 0 , fmt .Errorf ("invalid format: %s" , usage )
141148 }
142149
143150 value , err := strconv .ParseFloat (parts [0 ], 64 )
144151 if err != nil {
145- return 0 , err
152+ return 0 , fmt . Errorf ( "cannot parse float value: %f, %w" , value , err )
146153 }
147154
148155 if len (parts ) < 2 {
0 commit comments