formatter.go
formatter.go - Overview
-
Overview This file defines an interface
Formatter
and several concreteFormatter
implementations for formatting numerical values with units. It also provides a functionFromUnit
to retrieve aFormatter
based on a unit string. -
Detailed Documentation
Formatter
Interface
- Purpose: Defines the interface for formatting numerical values with units into human-readable strings.
type Formatter interface {
Format(value float64, unit string) string
Name() string
}
Format
- Purpose: Formats a given value with its unit.
- Parameters:
value
(float64): The numerical value to format.unit
(string): The unit of the value.
- Returns: string: The formatted string representation of the value and unit.
Name
- Purpose: Returns the name of the formatter.
- Returns: string: The name of the formatter
Global Variables
var (
DurationFormatter = NewDurationFormatter()
BoolFormatter = NewBoolFormatter()
PercentFormatter = NewPercentFormatter()
NoneFormatter = NewNoneFormatter()
DataFormatter = NewDataFormatter()
DataRateFormatter = NewDataRateFormatter()
ThroughputFormatter = NewThroughputFormatter()
)
- Purpose: These variables hold instances of different
Formatter
implementations.DurationFormatter
: Formatter for durations.BoolFormatter
: Formatter for boolean values.PercentFormatter
: Formatter for percentages.NoneFormatter
: Formatter that performs no formatting.DataFormatter
: Formatter for data sizes (e.g., bytes, kilobytes).DataRateFormatter
: Formatter for data rates (e.g., Bps, Mbps).ThroughputFormatter
: Formatter for throughput values (e.g., ops, reqps).
FromUnit
Function
- Purpose: Returns a
Formatter
instance based on the provided unit string.
func FromUnit(u string) Formatter {
switch u {
case "ns", "us", "µs", "ms", "s", "m", "h", "d":
return DurationFormatter
case "bytes", "decbytes", "bits", "decbits", "kbytes", "decKbytes", "deckbytes", "mbytes", "decMbytes", "decmbytes", "gbytes", "decGbytes", "decgbytes", "tbytes", "decTbytes", "dectbytes", "pbytes", "decPbytes", "decpbytes":
return DataFormatter
case "binBps", "Bps", "binbps", "bps", "KiBs", "Kibits", "KBs", "Kbits", "MiBs", "Mibits", "MBs", "Mbits", "GiBs", "Gibits", "GBs", "Gbits", "TiBs", "Tibits", "TBs", "Tbits", "PiBs", "Pibits", "PBs", "Pbits":
return DataRateFormatter
case "percent", "percentunit":
return PercentFormatter
case "bool", "bool_yes_no", "bool_true_false", "bool_1_0":
return BoolFormatter
case "cps", "ops", "reqps", "rps", "wps", "iops", "cpm", "opm", "rpm", "wpm":
return ThroughputFormatter
default:
return NoneFormatter
}
}
- Parameters:
u
(string): The unit string used to determine the appropriate formatter.
- Returns:
Formatter
: AFormatter
instance corresponding to the unit, orNoneFormatter
if the unit is not recognized.
- Code Examples
Not applicable, as the file primarily defines interfaces and a factory function.
- Clarity and Accuracy
The documentation reflects the code accurately.
- Markdown & MDX Perfection
The markdown is properly formatted.
- Edge Cases To Avoid Breaking MDX:
No issues found.
- Getting Started Relevance Include in Getting Started: YES