Skip to content

Commit

Permalink
Backport #100 into 2.10
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 31, 2019
1 parent 1160390 commit a701633
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public void setSchema(FormatSchema schema) {
_basePath.append(indent);
_jpropContext = JPropWriteContext.createRootContext(_indentLength);
}
// [dataformats-text#100]: Allow use of optional prefix
final String prefix = _schema.prefix();
if (prefix != null) {
_basePath.append(prefix);
}
}
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ public class JavaPropsSchema
*/
protected String _header = "";

/**
* Optional prefix to strip and append to key names.
* Useful when subset of properties need to be processed.
*
* @since 2.10
*/
protected String _prefix;

/*
/**********************************************************************
/* Construction, factories, mutant factories
Expand All @@ -157,6 +165,7 @@ public JavaPropsSchema(JavaPropsSchema base) {
_keyValueSeparator = base._keyValueSeparator;
_lineEnding = base._lineEnding;
_header = base._header;
_prefix = base._prefix;
}

/**
Expand Down Expand Up @@ -287,6 +296,22 @@ public JavaPropsSchema withLineEnding(String v) {
return s;
}

/**
* Mutant factory for constructing schema instance where specified
* prefix is prepended before logical path when generator writes output
* and removed by parser before binding back as properties.
*
* @since 2.10
*/
public JavaPropsSchema withPrefix(String v) {
if (_equals(v, _prefix)) {
return this;
}
JavaPropsSchema s = new JavaPropsSchema(this);
s._prefix = v;
return s;
}

/**
* Mutant factory for constructing schema instance where specified
* header section (piece of text written out right before actual
Expand Down Expand Up @@ -371,6 +396,13 @@ public String pathSeparator() {
return _pathSeparator;
}

/**
* @since 2.10
*/
public String prefix() {
return _prefix;
}

public boolean writeIndexUsingMarkers() {
return _writeIndexUsingMarkers && (_indexMarker != null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public static JPropPathSplitter create(JavaPropsSchema schema)
}
return new FullSplitter(sep, schema.parseSimpleIndexes(),
indexMarker,
pathOnlySplitter(schema));
pathOnlySplitter(schema),
schema.prefix());
}

private static JPropPathSplitter pathOnlySplitter(JavaPropsSchema schema)
Expand Down Expand Up @@ -262,14 +263,25 @@ public static class FullSplitter extends JPropPathSplitter
// small but important optimization for cases where index markers are absent
protected final int _indexFirstChar;
protected final JPropPathSplitter _simpleSplitter;


/**
* @since 2.10
*/
protected final String _prefix;

public FullSplitter(String pathSeparator, boolean useSimpleIndex,
Markers indexMarker, JPropPathSplitter fallbackSplitter)
Markers indexMarker, JPropPathSplitter fallbackSplitter,
String prefix)
{
super(useSimpleIndex);
String startMarker = indexMarker.getStart();
_indexFirstChar = startMarker.charAt(0);
_simpleSplitter = fallbackSplitter;
if (prefix == null || prefix.isEmpty()) {
_prefix = null;
} else {
_prefix = prefix + pathSeparator;
}
_indexMatch = Pattern.compile(String.format
("(%s)|(%s(\\d{1,9})%s)",
Pattern.quote(pathSeparator),
Expand All @@ -281,6 +293,13 @@ public FullSplitter(String pathSeparator, boolean useSimpleIndex,
public JPropNode splitAndAdd(JPropNode parent,
String key, String value)
{
// [dataformats-text#100]: handle possible prefix
if (_prefix != null) {
if (!key.startsWith(_prefix)) {
return null;
}
key = key.substring(_prefix.length());
}
if (key.indexOf(_indexFirstChar) < 0) { // no index start marker
return _simpleSplitter.splitAndAdd(parent, key, value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.fasterxml.jackson.dataformat.javaprop;

import java.util.Map;
import java.util.Properties;

import com.fasterxml.jackson.databind.ObjectMapper;

public class PrefixTest extends ModuleTestBase
{
private final JavaPropsMapper MAPPER = mapperForProps();

public void testPrefixParsing() throws Exception {
final String INPUT = "org.o1.firstName=Bob\n"
+"org.o1.lastName=Palmer\n"
+"org.o2.firstName=Alice\n"
+"org.o2.lastName=Black\n"
+"junk=AQIDBA==\n";
FiveMinuteUser result1 = _mapFrom(MAPPER.reader(JavaPropsSchema.emptySchema().withPrefix("org.o1")), INPUT, FiveMinuteUser.class, false);
assertEquals("Bob", result1.firstName);
assertEquals("Palmer", result1.lastName);
FiveMinuteUser result2 = _mapFrom(MAPPER.reader(JavaPropsSchema.emptySchema().withPrefix("org.o2")), INPUT, FiveMinuteUser.class, false);
assertEquals("Alice", result2.firstName);
assertEquals("Black", result2.lastName);
}

public void testPrefixGeneration() throws Exception
{
FiveMinuteUser input = new FiveMinuteUser("Bob", "Palmer", true, Gender.MALE,
new byte[] { 1, 2, 3, 4 });
String output = MAPPER.writer(JavaPropsSchema.emptySchema().withPrefix("org.o1")).writeValueAsString(input);
assertEquals("org.o1.firstName=Bob\n"
+"org.o1.lastName=Palmer\n"
+"org.o1.gender=MALE\n"
+"org.o1.verified=true\n"
+"org.o1.userImage=AQIDBA==\n"
,output);
Properties props = MAPPER.writeValueAsProperties(input, JavaPropsSchema.emptySchema().withPrefix("org.o1"));
assertEquals(5, props.size());
assertEquals("true", props.get("org.o1.verified"));
assertEquals("MALE", props.get("org.o1.gender"));
}
}
5 changes: 3 additions & 2 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ Henning Schmiedehausen (hgschmie@github)
(2.9.9)

Tanguy Leroux (tlrx@github)

* Reported #90: Exception when decoding Jackson-encoded `Base64` binary value in YAML
(2.10.0)

Andrey Somov (asomov@github)

* Contributed #101: Use latest SnakeYAML version 1.23 and get rid of deprecated methods
(2.10.0)

Alon Bar-Lev (alonbl@github)
* Contributed #100: (properties) Add an option to specify properties prefix
(2.10.0)
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Modules:

2.10.0 (not yet released)

#100: (properties) Add an option to specify properties prefix
(contributed by Alon B-L)
#101: (yaml) Use latest SnakeYAML version 1.24 and get rid of deprecated methods
(contributed by Andrey S)
#108: (yaml) Add new `CsvParser.Feature.ALLOW_COMMENTS` to replace deprecated
Expand Down

0 comments on commit a701633

Please sign in to comment.