Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://github.com/javers/javers/issues/416
- Loading branch information
1 parent
090a5ca
commit f18dfa5
Showing
3 changed files
with
53 additions
and
10 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
javers-persistence-mongo/src/main/java/org/javers/repository/mongo/MapKeyDotReplacer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.javers.repository.mongo; | ||
|
||
import org.bson.Document; | ||
|
||
/** | ||
* @author bartosz.walacik | ||
*/ | ||
class MapKeyDotReplacer { | ||
private static final String REPLACEMENT = "#dot#"; | ||
|
||
Document replaceInSnapshotState(Document snapshot) { | ||
return replaceInPropertyMaps(snapshot, "\\.", ".", REPLACEMENT); | ||
} | ||
|
||
Document back(Document snapshot) { | ||
return replaceInPropertyMaps(snapshot, REPLACEMENT, REPLACEMENT, "."); | ||
} | ||
|
||
private Document replaceInPropertyMaps(Document snapshot, String regexFrom, String from, String to) { | ||
Document state = getState(snapshot); | ||
|
||
for (String pName : state.keySet()){ | ||
if (state.get(pName) instanceof Document) { | ||
Document mapProperty = (Document)state.get(pName); | ||
state.put(pName, replaceInMapKeys(mapProperty, regexFrom, from, to)); | ||
} | ||
} | ||
|
||
return snapshot; | ||
} | ||
|
||
private Document getState(Document snapshot) { | ||
return (Document) snapshot.get("state"); | ||
} | ||
|
||
private Document replaceInMapKeys(Document map, String regexFrom, String from, String to) { | ||
for (String key : map.keySet()){ | ||
if (key.contains(from)){ | ||
String escaped = key.replaceAll(regexFrom, to); | ||
Object val = map.get(key); | ||
map.remove(key); | ||
map.put(escaped, val); | ||
} | ||
} | ||
return map; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters