Skip to content

Commit 8080727

Browse files
authored
feat: nested object or list can also be set with dot notation (#110)
* feat: nested object or list can also be set with dot notation * feat: nested object or list can also be set with dot notation * feat: nested object or list can also be set with dot notation
1 parent 51f30ba commit 8080727

File tree

3 files changed

+82
-34
lines changed

3 files changed

+82
-34
lines changed

tzatziki-core/src/test/resources/com/decathlon/tzatziki/steps/objects.feature

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -853,49 +853,65 @@ Feature: to interact with objects in the context
853853
- stringUser
854854
"""
855855

856-
Scenario: we can use dot-notation to specify a single nested field
856+
Scenario: we can use dot-notation to specify nested fields
857857
Given that yamlNests is a List<Nest>:
858858
"""
859859
- subNest.bird.name: Titi
860-
- subNest.bird.name: Tutu
860+
- subNest.subNest:
861+
subNest.bird.name: Tutu
862+
bird.name: Tata
861863
"""
862864
Then yamlNests contains only:
863865
"""
864866
- subNest:
865867
bird:
866868
name: Titi
867869
- subNest:
868-
bird:
869-
name: Tutu
870+
subNest:
871+
subNest:
872+
bird:
873+
name: Tutu
874+
bird:
875+
name: Tata
870876
"""
871877
Given that jsonNests is a List<Nest>:
872878
"""
873879
[
874-
{
875-
"subNest.bird.name": "Titi"
876-
},
877-
{
878-
"subNest.bird.name": "Tutu"
879-
}
880+
{
881+
"subNest.bird.name": "Titi"
882+
},
883+
{
884+
"subNest.subNest": {
885+
"subNest.bird.name": "Tutu",
886+
"bird.name": "Tata"
887+
}
888+
}
880889
]
881890
"""
882891
And jsonNests contains only:
883892
"""
884893
[
885-
{
886-
"subNest": {
887-
"bird": {
888-
"name": "Titi"
894+
{
895+
"subNest": {
896+
"bird": {
897+
"name": "Titi"
898+
}
889899
}
890-
}
891-
},
892-
{
893-
"subNest": {
894-
"bird": {
895-
"name": "Tutu"
900+
},
901+
{
902+
"subNest": {
903+
"subNest": {
904+
"subNest": {
905+
"bird": {
906+
"name": "Tutu"
907+
}
908+
},
909+
"bird": {
910+
"name": "Tata"
911+
}
912+
}
896913
}
897914
}
898-
}
899915
]
900916
"""
901917

tzatziki-mapper/src/main/java/com/decathlon/tzatziki/utils/Mapper.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Map;
99
import java.util.ServiceLoader;
1010
import java.util.Set;
11+
import java.util.stream.Collectors;
1112

1213
@NoArgsConstructor(access = AccessLevel.PRIVATE)
1314
public class Mapper {
@@ -62,11 +63,24 @@ public static String toYaml(Object object) {
6263
}
6364

6465
private static String dotNotationToYamlObject(String content) {
65-
while (content.matches("(?m)[\\s\\S]*?^[- ]*(?![\"']?\\?e)[\\w]+\\.[\\w.]+ *:[\\S\\s]+")) {
66-
content = content.replaceAll("(?m)^([- ]*)(?![\"']?\\?e)([\\w]+)\\.([\\w.]+ *:)", "$1$2:\n$1 $3");
67-
content = content.replaceAll("-( {3,})", " $1");
66+
List<String> lines = content.lines().collect(Collectors.toList());
67+
68+
for (int idx = 0; idx < lines.size(); idx++) {
69+
String line;
70+
String matchOnlyIfNonRegexFlag = "(?![ \"']*\\?e)";
71+
String captureDotNotation = "^(?>([ \\-]*))" + matchOnlyIfNonRegexFlag + "([^.]+)\\.([\\S ]+:" + matchOnlyIfNonRegexFlag + "[^\\n]*)\\n?";
72+
while ((line = lines.get(idx)).matches(captureDotNotation)) {
73+
String rootObjectIndent = line.replaceAll(captureDotNotation, "$1").replace("-", " ");
74+
String subObjectIndent = " " + rootObjectIndent;
75+
lines.set(idx, line.replaceAll(captureDotNotation, "$1$2:"));
76+
lines.add(idx + 1, line.replaceAll(captureDotNotation, subObjectIndent + "$3"));
77+
for (int subIdx = idx + 2; subIdx < lines.size() && lines.get(subIdx).startsWith(subObjectIndent); subIdx++) {
78+
lines.set(subIdx, " " + lines.get(subIdx));
79+
}
80+
}
6881
}
69-
return content;
82+
83+
return lines.stream().collect(Collectors.joining("\n"));
7084
}
7185

7286
public static boolean isJson(String value) {

tzatziki-mapper/src/test/java/com/decathlon/tzatziki/utils/MapperTest.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,37 @@ void checkFirstNonWhitespaceCharacter() {
1313
}
1414

1515
@Test
16-
void yamlDotPropertyToObject(){
17-
Assertions.assertEquals(Mapper.toYaml("""
18-
user.name: bob
19-
"""),
16+
void yamlDotPropertyToObject() {
17+
Assertions.assertEquals(
2018
"""
21-
user:
22-
name: bob
23-
""");
19+
users:
20+
- user:
21+
children:
22+
- user:
23+
name: babba
24+
- user:
25+
name: bobby
26+
- user:
27+
children:
28+
- user:
29+
name: titi
30+
- user:
31+
name: tata
32+
""".trim().stripIndent(),
33+
Mapper.toYaml("""
34+
users:
35+
- user.children:
36+
- user.name: babba
37+
- user.name: bobby
38+
- user.children:
39+
- user.name: titi
40+
- user.name: tata
41+
""".trim().stripIndent()));
2442

2543
Mapper.shouldConvertDotPropertiesToObject(false);
2644
Assertions.assertEquals(Mapper.toYaml("""
27-
user.name: bob
28-
"""),
45+
user.name: bob
46+
"""),
2947
"""
3048
user.name: bob
3149
""");

0 commit comments

Comments
 (0)