Skip to content

Commit

Permalink
Storage model: parse sizes given as strings (#1751)
Browse files Browse the repository at this point in the history
## Problem

The storage model cannot parse sizes when they are specified as strings
like "1024", "1 KiB", etc.

## Solution

This pull request introduces proper parsing based on xbytes (which is
already an Agama dependency).
  • Loading branch information
ancorgs authored Nov 13, 2024
2 parents 7c83b1f + 848b06f commit 2fcd9ab
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
51 changes: 44 additions & 7 deletions web/src/storage/model/config/size.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,35 @@ describe("#generate", () => {
}),
).toEqual({ min: 1024, max: 1024 });

// TODO: Generate bytes from string size.
// expect(model.generate(
// {
// size: "1 KiB"
// }
// )).toEqual({ min: 1024, max: 1024 });
expect(
model.generate({
size: "1 KiB",
}),
).toEqual({ min: 1024, max: 1024 });

expect(
model.generate({
size: "1KiB",
}),
).toEqual({ min: 1024, max: 1024 });

expect(
model.generate({
size: "1kb",
}),
).toEqual({ min: 1000, max: 1000 });

expect(
model.generate({
size: "1k",
}),
).toEqual({ min: 1000, max: 1000 });

expect(
model.generate({
size: "665.284 TiB",
}),
).toEqual({ min: 731487493773328, max: 731487493773328 });

expect(
model.generate({
Expand All @@ -49,6 +72,12 @@ describe("#generate", () => {
}),
).toEqual({ min: 1024, max: 2048 });

expect(
model.generate({
size: ["1 kib", "2 KIB"],
}),
).toEqual({ min: 1024, max: 2048 });

expect(
model.generate({
size: {
Expand All @@ -65,8 +94,16 @@ describe("#generate", () => {
},
}),
).toEqual({ min: 1024, max: 2048 });
});

expect(
model.generate({
size: {
min: "1 kib",
max: "2 KiB",
},
}),
).toEqual({ min: 1024, max: 2048 });
});
it("returns undefined for 'custom' value", () => {
expect(
model.generate({
Expand Down
13 changes: 11 additions & 2 deletions web/src/storage/model/config/size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import { config } from "~/api/storage/types";
import * as checks from "~/api/storage/types/checks";
import xbytes from "xbytes";

export type Size = {
min?: number;
Expand Down Expand Up @@ -68,10 +69,18 @@ class SizeGenerator<TypeWithSize extends WithSize> {
return size;
}

private parseSizeString(value: string): number | undefined {
// xbytes.parseSize will not work with a string like '10k', the unit must end with 'b' or 'B'
let adapted = value.trim();
if (!adapted.match(/b$/i)) adapted = adapted + "b";

const parsed = xbytes.parseSize(adapted, { bits: false }) || parseInt(adapted);
if (parsed) return Math.trunc(parsed);
}

private bytes(value: config.SizeValueWithCurrent): number | undefined {
if (checks.isSizeCurrent(value)) return;
// TODO: bytes from string.
if (checks.isSizeString(value)) return;
if (checks.isSizeString(value)) return this.parseSizeString(value);
if (checks.isSizeBytes(value)) return value;
}
}
Expand Down

0 comments on commit 2fcd9ab

Please sign in to comment.