Commit 8d186945 authored by Dave Syer's avatar Dave Syer

Use RelaxedNames to search for prefix in RelaxedBinder

RelaxedDataBinder now supports "env var" style variables that include the
path prefix, e.g. FOO_BAR_BAZ=boom will bind to a bean with property "baz"
and a binder with prefix "foo.bar".

Fixes gh-98
parent eff587d5
...@@ -121,9 +121,11 @@ public class RelaxedDataBinder extends DataBinder { ...@@ -121,9 +121,11 @@ public class RelaxedDataBinder extends DataBinder {
MutablePropertyValues rtn = new MutablePropertyValues(); MutablePropertyValues rtn = new MutablePropertyValues();
for (PropertyValue pv : propertyValues.getPropertyValues()) { for (PropertyValue pv : propertyValues.getPropertyValues()) {
String name = pv.getName(); String name = pv.getName();
if (name.startsWith(this.namePrefix)) { for (String candidate : new RelaxedNames(this.namePrefix)) {
name = name.substring(this.namePrefix.length()); if (name.startsWith(candidate)) {
rtn.add(name, pv.getValue()); name = name.substring(candidate.length());
rtn.add(name, pv.getValue());
}
} }
} }
return rtn; return rtn;
......
...@@ -41,7 +41,6 @@ import org.junit.Test; ...@@ -41,7 +41,6 @@ import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.NotWritablePropertyException; import org.springframework.beans.NotWritablePropertyException;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.context.support.StaticMessageSource; import org.springframework.context.support.StaticMessageSource;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultConversionService;
...@@ -75,6 +74,34 @@ public class RelaxedDataBinderTests { ...@@ -75,6 +74,34 @@ public class RelaxedDataBinderTests {
assertEquals("bar", target.getFoo()); assertEquals("bar", target.getFoo());
} }
@Test
public void testBindStringWithPrefix() throws Exception {
VanillaTarget target = new VanillaTarget();
bind(target, "test.foo: bar", "test");
assertEquals("bar", target.getFoo());
}
@Test
public void testBindFromEnvironmentStyleWithPrefix() throws Exception {
VanillaTarget target = new VanillaTarget();
bind(target, "TEST_FOO: bar", "test");
assertEquals("bar", target.getFoo());
}
@Test
public void testBindFromEnvironmentStyleWithNestedPrefix() throws Exception {
VanillaTarget target = new VanillaTarget();
bind(target, "TEST_IT_FOO: bar", "test.it");
assertEquals("bar", target.getFoo());
}
@Test
public void testBindCapitals() throws Exception {
VanillaTarget target = new VanillaTarget();
bind(target, "FOO: bar");
assertEquals("bar", target.getFoo());
}
@Test @Test
public void testBindUnderscoreInActualPropertyName() throws Exception { public void testBindUnderscoreInActualPropertyName() throws Exception {
VanillaTarget target = new VanillaTarget(); VanillaTarget target = new VanillaTarget();
...@@ -215,8 +242,6 @@ public class RelaxedDataBinderTests { ...@@ -215,8 +242,6 @@ public class RelaxedDataBinderTests {
} }
@Test @Test
// @Ignore("Should be possible but currently not supported")
// FIXME: bind to map containing beans
public void testBindNestedMapOfBean() throws Exception { public void testBindNestedMapOfBean() throws Exception {
TargetWithNestedMapOfBean target = new TargetWithNestedMapOfBean(); TargetWithNestedMapOfBean target = new TargetWithNestedMapOfBean();
bind(target, "nested.foo.foo: bar\n" + "nested.bar.foo: bucket"); bind(target, "nested.foo.foo: bar\n" + "nested.bar.foo: bucket");
...@@ -225,8 +250,6 @@ public class RelaxedDataBinderTests { ...@@ -225,8 +250,6 @@ public class RelaxedDataBinderTests {
} }
@Test @Test
// @Ignore("Should be possible but currently not supported")
// FIXME: bind to map containing beans
public void testBindNestedMapOfListOfBean() throws Exception { public void testBindNestedMapOfListOfBean() throws Exception {
TargetWithNestedMapOfListOfBean target = new TargetWithNestedMapOfListOfBean(); TargetWithNestedMapOfListOfBean target = new TargetWithNestedMapOfListOfBean();
bind(target, "nested.foo[0].foo: bar\n" + "nested.bar[0].foo: bucket\n" bind(target, "nested.foo[0].foo: bar\n" + "nested.bar[0].foo: bucket\n"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment