Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

internal/units: Create new units package #705

wants to merge 2 commits into from

Conversation

rfratto
Copy link
Member

@rfratto rfratto commented Apr 29, 2024

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.
  • If code sets a default as 5 * 1000 * 1000 (5MB), it displays as 4MiB786KiB832B, 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 of 4MiB, 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 will String() 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.

@rfratto rfratto force-pushed the units branch 2 times, most recently from db37b5c to 6285b41 Compare April 29, 2024 18:29
rfratto added 2 commits April 29, 2024 15:00
…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.
@rfratto rfratto marked this pull request as ready for review April 29, 2024 19:19

var unitMap = map[string]Bytes{
"": Byte,
"b": Byte,
Copy link
Collaborator

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.

@rfratto rfratto added the backport-to-agent:no PR should NOT be backported to the agent repo. label May 3, 2024
Copy link
Contributor

github-actions bot commented Jun 3, 2024

This PR has not had any activity in the past 30 days, so the needs-attention label has been added to it.
If you do not have enough time to follow up on this PR or you think it's no longer relevant, consider closing it.
The needs-attention label signals to maintainers that something has fallen through the cracks. No action is needed by you; your PR will be kept open and you do not have to respond to this comment. The label will be removed the next time this job runs if there is new activity.
Thank you for your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-to-agent:no PR should NOT be backported to the agent repo. needs-attention
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants