Add support for DataSize

This commit adds support for Spring Framework's `DataSize` allowing to
express a size in bytes and other convenient units.

Similar to the `Duration` support introduced previously, this commit
adds transparent binding support as well as detection of default values
in `@ConfigurationProperties`-annotated object.

Closes gh-13974
This commit is contained in:
Stephane Nicoll
2018-08-10 14:58:11 +02:00
parent 78dd7bd934
commit 94013aaba6
11 changed files with 478 additions and 8 deletions

View File

@@ -116,6 +116,20 @@ public class JavaCompilerFieldValuesParser implements FieldValuesParser {
DURATION_SUFFIX = Collections.unmodifiableMap(values);
}
private static final String DATA_SIZE_OF = "DataSize.of";
private static final Map<String, String> DATA_SIZE_SUFFIX;
static {
Map<String, String> values = new HashMap<>();
values.put("Bytes", "B");
values.put("KiloBytes", "KB");
values.put("MegaBytes", "MB");
values.put("GigaBytes", "GB");
values.put("TeraBytes", "TB");
DATA_SIZE_SUFFIX = Collections.unmodifiableMap(values);
}
private final Map<String, Object> fieldValues = new HashMap<>();
private final Map<String, Object> staticFinals = new HashMap<>();
@@ -173,16 +187,31 @@ public class JavaCompilerFieldValuesParser implements FieldValuesParser {
}
private Object getFactoryValue(ExpressionTree expression, Object factoryValue) {
Object instance = expression.getInstance();
if (instance != null && instance.toString().startsWith(DURATION_OF)) {
String type = instance.toString();
type = type.substring(DURATION_OF.length(), type.indexOf('('));
String suffix = DURATION_SUFFIX.get(type);
return (suffix != null) ? factoryValue + suffix : null;
Object durationValue = getFactoryValue(expression, factoryValue, DURATION_OF,
DURATION_SUFFIX);
if (durationValue != null) {
return durationValue;
}
Object dataSizeValue = getFactoryValue(expression, factoryValue, DATA_SIZE_OF,
DATA_SIZE_SUFFIX);
if (dataSizeValue != null) {
return dataSizeValue;
}
return factoryValue;
}
private Object getFactoryValue(ExpressionTree expression, Object factoryValue,
String prefix, Map<String, String> suffixMapping) {
Object instance = expression.getInstance();
if (instance != null && instance.toString().startsWith(prefix)) {
String type = instance.toString();
type = type.substring(prefix.length(), type.indexOf('('));
String suffix = suffixMapping.get(type);
return (suffix != null) ? factoryValue + suffix : null;
}
return null;
}
public Map<String, Object> getFieldValues() {
return this.fieldValues;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -95,6 +95,12 @@ public abstract class AbstractFieldValuesProcessorTests {
assertThat(values.get("durationMinutes")).isEqualTo("30m");
assertThat(values.get("durationHours")).isEqualTo("40h");
assertThat(values.get("durationDays")).isEqualTo("50d");
assertThat(values.get("dataSizeNone")).isNull();
assertThat(values.get("dataSizeBytes")).isEqualTo("5B");
assertThat(values.get("dataSizeKiloBytes")).isEqualTo("10KB");
assertThat(values.get("dataSizeMegaBytes")).isEqualTo("20MB");
assertThat(values.get("dataSizeGigaBytes")).isEqualTo("30GB");
assertThat(values.get("dataSizeTeraBytes")).isEqualTo("40TB");
}
@SupportedAnnotationTypes({

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import java.time.Duration;
import org.springframework.boot.configurationsample.ConfigurationProperties;
import org.springframework.util.MimeType;
import org.springframework.util.unit.DataSize;
/**
* Sample object containing fields with initial values.
@@ -123,4 +124,16 @@ public class FieldValues {
private Duration durationDays = Duration.ofDays(50);
private DataSize dataSizeNone;
private DataSize dataSizeBytes = DataSize.ofBytes(5);
private DataSize dataSizeKiloBytes = DataSize.ofKiloBytes(10);
private DataSize dataSizeMegaBytes = DataSize.ofMegaBytes(20);
private DataSize dataSizeGigaBytes = DataSize.ofGigaBytes(30);
private DataSize dataSizeTeraBytes = DataSize.ofTeraBytes(40);
}