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

@@ -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);
}