Size returns the size of a value
(key string)
| 401 | |
| 402 | // Size returns the size of a value |
| 403 | func (s *StringStructure) Size(key string) (string, error) { |
| 404 | value, err := s.Get(key) |
| 405 | if err != nil { |
| 406 | return "", err |
| 407 | } |
| 408 | |
| 409 | toString, err := interfaceToString(value) |
| 410 | if err != nil { |
| 411 | return "", err |
| 412 | } |
| 413 | |
| 414 | sizeInBytes := len(toString) |
| 415 | |
| 416 | // Convert bytes to corresponding units (KB, MB...) |
| 417 | const ( |
| 418 | KB = 1 << 10 |
| 419 | MB = 1 << 20 |
| 420 | GB = 1 << 30 |
| 421 | ) |
| 422 | |
| 423 | var size string |
| 424 | switch { |
| 425 | case sizeInBytes < KB: |
| 426 | size = fmt.Sprintf("%dB", sizeInBytes) |
| 427 | case sizeInBytes < MB: |
| 428 | size = fmt.Sprintf("%.2fKB", float64(sizeInBytes)/KB) |
| 429 | case sizeInBytes < GB: |
| 430 | size = fmt.Sprintf("%.2fMB", float64(sizeInBytes)/MB) |
| 431 | } |
| 432 | |
| 433 | return size, nil |
| 434 | } |
| 435 | |
| 436 | func (s *StringStructure) MGet(keys ...string) ([]interface{}, error) { |
| 437 | // Create a slice to store the values |
nothing calls this directly
no test coverage detected