@@ -13,6 +13,7 @@ describe('Code Generator', () => {
13
13
'$.info' ,
14
14
'$.info.contact' ,
15
15
'$.info.contact.*' ,
16
+ '$[servers,paths][*]' ,
16
17
'$.servers[*].url' ,
17
18
'$.servers[0:2]' ,
18
19
'$.servers[:5]' ,
@@ -24,36 +25,38 @@ describe('Code Generator', () => {
24
25
] ) ,
25
26
) . to . eq ( `import {Scope, isObject} from "nimma/runtime";
26
27
const zones = {
27
- "info": {
28
- "contact": {
29
- "*": {}
28
+ keys: ["info", "servers", "paths", "channels"],
29
+ zones: [{
30
+ keys: ["contact"],
31
+ zones: [{
32
+ zone: {}
33
+ }]
34
+ }, {
35
+ zone: {
36
+ keys: ["url"],
37
+ zones: [{}]
30
38
}
31
- },
32
- "servers": {
33
- "*": {
34
- "url": {}
35
- }
36
- },
37
- "paths": {
38
- "*": {
39
- "202": {},
40
- "404": {}
39
+ }, {
40
+ zone: {
41
+ keys: [404, 202],
42
+ zones: [{}, {}]
41
43
}
42
- },
43
- "channels": {
44
- "*": {
45
- "publish": {
46
- "*": {
47
- "payload": {}
44
+ }, {
45
+ zone: {
46
+ keys: ["publish", "subscribe"],
47
+ zones: [{
48
+ zone: {
49
+ keys: ["payload"],
50
+ zones: [{}]
48
51
}
49
- },
50
- "subscribe" : {
51
- "*": {
52
- "payload": {}
52
+ }, {
53
+ zone : {
54
+ keys: ["payload"],
55
+ zones: [{}]
53
56
}
54
- }
57
+ }]
55
58
}
56
- }
59
+ }]
57
60
};
58
61
const tree = {
59
62
"$.info": function (scope) {
@@ -76,6 +79,11 @@ const tree = {
76
79
if (scope.path[1] !== "contact") return;
77
80
scope.emit("$.info.contact.*", 0, false);
78
81
},
82
+ "$[servers,paths][*]": function (scope) {
83
+ if (scope.path.length !== 2) return;
84
+ if (scope.path[0] !== "servers" && scope.path[0] !== "paths") return;
85
+ scope.emit("$[servers,paths][*]", 0, false);
86
+ },
79
87
"$.servers[*].url": function (scope) {
80
88
if (scope.path.length !== 3) return;
81
89
if (scope.path[0] !== "servers") return;
@@ -148,6 +156,7 @@ export default function (input, callbacks) {
148
156
const state0 = scope.allocState();
149
157
scope.traverse(() => {
150
158
tree["$.info.contact.*"](scope);
159
+ tree["$[servers,paths][*]"](scope);
151
160
tree["$.servers[*].url"](scope);
152
161
tree["$.servers[0:2]"](scope);
153
162
tree["$.servers[:5]"](scope);
@@ -166,11 +175,13 @@ export default function (input, callbacks) {
166
175
expect ( generate ( [ '$.info~' , '$.servers[*].url~' , '$.servers[:5]~' ] ) ) . to
167
176
. eq ( `import {Scope, isObject} from "nimma/runtime";
168
177
const zones = {
169
- "servers": {
170
- "*": {
171
- "url": {}
178
+ keys: ["servers"],
179
+ zones: [{
180
+ zone: {
181
+ keys: ["url"],
182
+ zones: [{}]
172
183
}
173
- }
184
+ }]
174
185
};
175
186
const tree = {
176
187
"$.info~": function (scope) {
@@ -595,7 +606,7 @@ export default function (input, callbacks) {
595
606
expect ( generate ( [ '$[*]' , '$.*' , '$[*]^' , '$[*]~' ] ) ) . to
596
607
. eq ( `import {Scope} from "nimma/runtime";
597
608
const zones = {
598
- "*" : {}
609
+ zone : {}
599
610
};
600
611
const tree = {
601
612
"$[*]": function (scope) {
@@ -698,7 +709,7 @@ export default function (input, callbacks) {
698
709
] ) ,
699
710
) . to . eq ( `import {Scope, inBounds} from "nimma/runtime";
700
711
const zones = {
701
- "*" : {}
712
+ zone : {}
702
713
};
703
714
const tree = {
704
715
"$[0:2]": function (scope) {
@@ -816,9 +827,8 @@ export default function (input, callbacks) {
816
827
expect ( generate ( [ '$.store..[price,bar,baz]' , '$.book' ] ) ) . to
817
828
. eq ( `import {Scope, isObject} from "nimma/runtime";
818
829
const zones = {
819
- "store": {
820
- "**": null
821
- }
830
+ keys: ["store"],
831
+ zones: [null]
822
832
};
823
833
const tree = {
824
834
"$.store..[price,bar,baz]": function (scope) {
@@ -857,11 +867,12 @@ export default function (input, callbacks) {
857
867
] ) ,
858
868
) . to . eq ( `import {Scope} from "nimma/runtime";
859
869
const zones = {
860
- "paths": {
861
- "*": {
862
- "**": null
870
+ keys: ["paths"],
871
+ zones: [{
872
+ zone: {
873
+ zone: null
863
874
}
864
- }
875
+ }]
865
876
};
866
877
const tree = {
867
878
"$.paths[*][*]..content[*].examples[*]": function (scope) {
@@ -897,18 +908,15 @@ export default function (input, callbacks) {
897
908
expect ( generate ( [ '$.data[*][*][city,street]..id' ] ) ) . to
898
909
. eq ( `import {Scope} from "nimma/runtime";
899
910
const zones = {
900
- "data": {
901
- "*": {
902
- "*": {
903
- "city": {
904
- "**": null
905
- },
906
- "street": {
907
- "**": null
908
- }
911
+ keys: ["data"],
912
+ zones: [{
913
+ zone: {
914
+ zone: {
915
+ keys: ["city", "street"],
916
+ zones: [null, null]
909
917
}
910
918
}
911
- }
919
+ }]
912
920
};
913
921
const tree = {
914
922
"$.data[*][*][city,street]..id": function (scope) {
@@ -944,28 +952,28 @@ export default function (input, callbacks) {
944
952
] ) ,
945
953
) . to . eq ( `import {Scope} from "nimma/runtime";
946
954
const zones = {
947
- "paths": {
948
- "*": {
949
- "*": {
950
- "tags": {
951
- "*": {}
952
- },
953
- "operationId": {}
955
+ keys: ["paths", "abc"],
956
+ zones: [{
957
+ zone: {
958
+ zone: {
959
+ keys: ["tags", "operationId"],
960
+ zones: [{
961
+ zone: {}
962
+ }, {}]
954
963
}
955
964
}
956
- },
957
- "abc": {
958
- "*": {
959
- "*": {
960
- "*": {
961
- "*": {
962
- "baz": {},
963
- "bar": {}
965
+ }, {
966
+ zone: {
967
+ zone: {
968
+ zone: {
969
+ zone: {
970
+ keys: ["baz", "bar"],
971
+ zones: [{}, {}]
964
972
}
965
973
}
966
974
}
967
975
}
968
- }
976
+ }]
969
977
};
970
978
const tree = {
971
979
"$.paths[*][*].tags[*]": function (scope) {
@@ -1020,6 +1028,39 @@ export default function (input, callbacks) {
1020
1028
scope.destroy();
1021
1029
}
1022
1030
}
1031
+ ` ) ;
1032
+ } ) ;
1033
+
1034
+ it ( '* and ** used as member property keys' , ( ) => {
1035
+ expect ( generate ( [ '$.test[*]["*"]' ] ) ) . to
1036
+ . eq ( `import {Scope} from "nimma/runtime";
1037
+ const zones = {
1038
+ keys: ["test"],
1039
+ zones: [{
1040
+ zone: {
1041
+ keys: ["*"],
1042
+ zones: [{}]
1043
+ }
1044
+ }]
1045
+ };
1046
+ const tree = {
1047
+ "$.test[*][\\"*\\"]": function (scope) {
1048
+ if (scope.path.length !== 3) return;
1049
+ if (scope.path[0] !== "test") return;
1050
+ if (scope.path[2] !== "*") return;
1051
+ scope.emit("$.test[*][\\"*\\"]", 0, false);
1052
+ }
1053
+ };
1054
+ export default function (input, callbacks) {
1055
+ const scope = new Scope(input, callbacks);
1056
+ try {
1057
+ scope.traverse(() => {
1058
+ tree["$.test[*][\\"*\\"]"](scope);
1059
+ }, zones);
1060
+ } finally {
1061
+ scope.destroy();
1062
+ }
1063
+ }
1023
1064
` ) ;
1024
1065
} ) ;
1025
1066
} ) ;
@@ -1075,7 +1116,7 @@ export default function (input, callbacks) {
1075
1116
] ) ,
1076
1117
) . to . eq ( `import {Scope} from "nimma/runtime";
1077
1118
const zones = {
1078
- "*" : {}
1119
+ zone : {}
1079
1120
};
1080
1121
const tree = {
1081
1122
"$[?(index(@)=='key')]": function (scope) {
@@ -1181,11 +1222,13 @@ export default function (input, callbacks) {
1181
1222
} ) ,
1182
1223
) . to . eq ( `import {Scope} from "nimma/runtime";
1183
1224
const zones = {
1184
- "components": {
1185
- "schemas": {
1186
- "**": null
1187
- }
1188
- }
1225
+ keys: ["components"],
1226
+ zones: [{
1227
+ keys: ["schemas"],
1228
+ zones: [{
1229
+ zone: null
1230
+ }]
1231
+ }]
1189
1232
};
1190
1233
const tree = {
1191
1234
"$.components.schemas[*]..@@schema()": function (scope) {
0 commit comments