mirror of
https://github.com/lightbend/config.git
synced 2025-03-25 08:40:40 +08:00
support terabytes when parsing a memory size
This commit is contained in:
parent
6ca952e516
commit
598c5d045f
src
@ -235,7 +235,7 @@ public final class Config {
|
||||
|
||||
private static enum MemoryUnit {
|
||||
BYTES(1), KILOBYTES(1024), MEGABYTES(1024 * 1024), GIGABYTES(
|
||||
1024 * 1024 * 1024);
|
||||
1024 * 1024 * 1024), TERABYTES(1024 * 1024 * 1024 * 1024);
|
||||
|
||||
int bytes;
|
||||
MemoryUnit(int bytes) {
|
||||
@ -293,10 +293,12 @@ public final class Config {
|
||||
units = MemoryUnit.MEGABYTES;
|
||||
} else if (unitStringLower.equals("g") || unitString.equals("gigabyte")) {
|
||||
units = MemoryUnit.GIGABYTES;
|
||||
} else if (unitStringLower.equals("t") || unitString.equals("terabyte")) {
|
||||
units = MemoryUnit.TERABYTES;
|
||||
} else {
|
||||
throw new ConfigException.BadValue(originForException,
|
||||
pathForException, "Could not parse size unit '"
|
||||
+ unitStringMaybePlural + "' (try b, k, m, g)");
|
||||
+ unitStringMaybePlural + "' (try b, k, m, g, t)");
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -46,11 +46,19 @@ class UnitParserTest extends TestUtils {
|
||||
"1024k", "1024K", "1024 kilobytes", "1024 kilobyte",
|
||||
"1m", "1M", "1 M", "1 megabytes", "1 megabyte",
|
||||
"0.0009765625g", "0.0009765625G", "0.0009765625 gigabytes", "0.0009765625 gigabyte")
|
||||
|
||||
def parseMem(s: String) = Config.parseMemorySize(s, fakeOrigin(), "test")
|
||||
|
||||
for (s <- oneMegs) {
|
||||
val result = Config.parseMemorySize(s, fakeOrigin(), "test")
|
||||
val result = parseMem(s)
|
||||
assertEquals(1024 * 1024, result)
|
||||
}
|
||||
|
||||
assertEquals(1024 * 1024 * 1024 * 1024, parseMem("1t"))
|
||||
assertEquals(1024 * 1024 * 1024 * 1024, parseMem(" 1 T "))
|
||||
assertEquals(1024 * 1024 * 1024 * 1024, parseMem("1 terabyte"))
|
||||
assertEquals(1024 * 1024 * 1024 * 1024, parseMem(" 1 terabytes "))
|
||||
|
||||
// bad units
|
||||
val e = intercept[ConfigException.BadValue] {
|
||||
Config.parseMemorySize("100 dollars", fakeOrigin(), "test")
|
||||
|
Loading…
Reference in New Issue
Block a user