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:
@@ -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({
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user