提交 7ece0e21 编写于 作者: J Juergen Hoeller

Correct code example for YamlProcessor.setDocumentMatchers

Issue: SPR-16849
上级 bfcc1a1f
...@@ -75,15 +75,16 @@ public abstract class YamlProcessor { ...@@ -75,15 +75,16 @@ public abstract class YamlProcessor {
* name: My Cool App * name: My Cool App
* </pre> * </pre>
* when mapped with * when mapped with
* <code>documentMatchers = YamlProcessor.mapMatcher({"environment": "prod"})</code> * <pre class="code">
* setDocumentMatchers(properties ->
* ("prod".equals(properties.getProperty("environment")) ? MatchStatus.FOUND : MatchStatus.NOT_FOUND));
* </pre>
* would end up as * would end up as
* <pre class="code"> * <pre class="code">
* environment=prod * environment=prod
* url=http://foo.bar.com * url=http://foo.bar.com
* name=My Cool App * name=My Cool App
* url=http://dev.bar.com
* </pre> * </pre>
* @param matchers a map of keys to value patterns (regular expressions)
*/ */
public void setDocumentMatchers(DocumentMatcher... matchers) { public void setDocumentMatchers(DocumentMatcher... matchers) {
this.documentMatchers = Arrays.asList(matchers); this.documentMatchers = Arrays.asList(matchers);
...@@ -92,8 +93,7 @@ public abstract class YamlProcessor { ...@@ -92,8 +93,7 @@ public abstract class YamlProcessor {
/** /**
* Flag indicating that a document for which all the * Flag indicating that a document for which all the
* {@link #setDocumentMatchers(DocumentMatcher...) document matchers} abstain will * {@link #setDocumentMatchers(DocumentMatcher...) document matchers} abstain will
* nevertheless match. * nevertheless match. Default is {@code true}.
* @param matchDefault the flag to set (default true)
*/ */
public void setMatchDefault(boolean matchDefault) { public void setMatchDefault(boolean matchDefault) {
this.matchDefault = matchDefault; this.matchDefault = matchDefault;
...@@ -102,9 +102,7 @@ public abstract class YamlProcessor { ...@@ -102,9 +102,7 @@ public abstract class YamlProcessor {
/** /**
* Method to use for resolving resources. Each resource will be converted to a Map, * Method to use for resolving resources. Each resource will be converted to a Map,
* so this property is used to decide which map entries to keep in the final output * so this property is used to decide which map entries to keep in the final output
* from this factory. * from this factory. Default is {@link ResolutionMethod#OVERRIDE}.
* @param resolutionMethod the resolution method to set (defaults to
* {@link ResolutionMethod#OVERRIDE}).
*/ */
public void setResolutionMethod(ResolutionMethod resolutionMethod) { public void setResolutionMethod(ResolutionMethod resolutionMethod) {
Assert.notNull(resolutionMethod, "ResolutionMethod must not be null"); Assert.notNull(resolutionMethod, "ResolutionMethod must not be null");
......
...@@ -107,7 +107,7 @@ public class YamlPropertiesFactoryBeanTests { ...@@ -107,7 +107,7 @@ public class YamlPropertiesFactoryBeanTests {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(new ByteArrayResource( factory.setResources(new ByteArrayResource(
"foo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes())); "foo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes()));
factory.setDocumentMatchers((DocumentMatcher) properties -> ("bag".equals(properties.getProperty("foo")) ? factory.setDocumentMatchers(properties -> ("bag".equals(properties.getProperty("foo")) ?
MatchStatus.FOUND : MatchStatus.NOT_FOUND)); MatchStatus.FOUND : MatchStatus.NOT_FOUND));
Properties properties = factory.getObject(); Properties properties = factory.getObject();
assertThat(properties.getProperty("foo"), equalTo("bag")); assertThat(properties.getProperty("foo"), equalTo("bag"));
...@@ -120,7 +120,7 @@ public class YamlPropertiesFactoryBeanTests { ...@@ -120,7 +120,7 @@ public class YamlPropertiesFactoryBeanTests {
factory.setMatchDefault(true); factory.setMatchDefault(true);
factory.setResources(new ByteArrayResource( factory.setResources(new ByteArrayResource(
"one: two\n---\nfoo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes())); "one: two\n---\nfoo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes()));
factory.setDocumentMatchers((DocumentMatcher) properties -> { factory.setDocumentMatchers(properties -> {
if (!properties.containsKey("foo")) { if (!properties.containsKey("foo")) {
return MatchStatus.ABSTAIN; return MatchStatus.ABSTAIN;
} }
...@@ -161,7 +161,7 @@ public class YamlPropertiesFactoryBeanTests { ...@@ -161,7 +161,7 @@ public class YamlPropertiesFactoryBeanTests {
factory.setMatchDefault(true); factory.setMatchDefault(true);
factory.setResources(new ByteArrayResource( factory.setResources(new ByteArrayResource(
"one: two\n---\nfoo: bag\nspam: bad\n---\nfoo: bar\nspam: baz".getBytes())); "one: two\n---\nfoo: bag\nspam: bad\n---\nfoo: bar\nspam: baz".getBytes()));
factory.setDocumentMatchers((DocumentMatcher) properties -> { factory.setDocumentMatchers(properties -> {
if (!properties.containsKey("foo")) { if (!properties.containsKey("foo")) {
return MatchStatus.ABSTAIN; return MatchStatus.ABSTAIN;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册