-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
internal/units: Create new units package #705
base: main
Are you sure you want to change the base?
Conversation
db37b5c
to
6285b41
Compare
…ckage This commit creates a replacement for the github.com/alecthomas/units package. A replacement is desirable because the upstream package doesn't work very well with the Alloy syntax: * We use the UnmarshalText implementation of Base2Bytes, which treats metric (MB) and IEC (MiB) suffixes as the same. * Base2Bytes always reports values as IEC suffixes when marshalling them back, which can confuse users into thinking a unit conversion has occurred somewhere. I think it's potentially confusing to users that setting a limit of 4MB actually sets a limit of 4MiB, which is ~4% less than what the user intended. The new implementation supports parsing the same input as the old package, including complex byte sizes such as `4MiB3KiB`, though simplified byte sizes is preferred (`4099KiB`), and the simplified forms are returned when marshaling back into a string.
This switches everything over to the new units package.
|
||
var unitMap = map[string]Bytes{ | ||
"": Byte, | ||
"b": Byte, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be bit? Which I am not sure how we would even handle.
This PR has not had any activity in the past 30 days, so the |
This commit creates a replacement for the github.com/alecthomas/units package. A replacement is desirable because the upstream package doesn't work very well with the Alloy syntax:
Base2Bytes
, which treats metric (MB) and IEC (MiB) suffixes as the same.5 * 1000 * 1000
(5MB), it displays as4MiB786KiB832B
, which is incredibly hard to read and understand.I think it's confusing to users that setting a limit of
4MB
actually sets a limit of4MiB
, which is ~4% less than what the user intended.I couldn't find a third party package which supported both IEC and metric while also implementing encoding.TextMarshaler/encoding.TextUnmarshaler, so I wrote a new package.
The new
internal/units
package supports both IEC and metric suffixes in the same data type, unmarshaling to the proper type based on what suffix the user provided. Additionally, it willString()
to the most appropriate value, a byte count divisible by 1000 shows metric units, otherwise it shows IEC units.The new implementation supports parsing the same input as the old package, including complex byte sizes such as
4MiB3KiB
, though simplified byte sizes is preferred (4099KiB
), and the simplified forms are returned when marshaling back into a string.