-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_json_modif.d
309 lines (231 loc) · 7.49 KB
/
lib_json_modif.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
module d_glat.lib_json_modif;
public import d_glat.core_json;
public import d_glat.lib_json_manip;
import d_glat.core_assoc_array;
import d_glat.core_assert;
import d_glat.core_profile_acc;
import std.algorithm : canFind;
import std.array : appender, Appender, array, split;
import std.conv : to;
import std.exception : assumeUnique;
import std.stdio : writeln;
import std.string : splitLines;
/*
Represent a series of modifications to a JSON. Provide a tool to
apply such a series, forbidding or permitting overwrites.
By Guillaume Lathoud - glat@glat.info
Boost license, as described in the file ./LICENSE
*/
// -------------------- Representation --------------------
struct JsonModif
{
const Jsonplace where;
const JSONValue what;
};
alias JsonModifMany = JsonModifManyPO!false;
class JsonModifManyPO( bool permits_overwrite ) : ProfileMemC
{
// Inner representation
private Appender!(JsonModif[]) jm_app;
static if (!permits_overwrite) private ModifiedSofar moso;
// API
JsonModif[] jm_arr() pure const @safe nothrow
{
return jm_app.data.dup;
}
this() { jm_app = appender!(JsonModif[]); }
this( in JsonModif[] jm_arr ) { this(); push( jm_arr ); }
void clear() pure nothrow
{
jm_app.clear;
static if (!permits_overwrite) moso.clear;
}
bool isEmpty() const pure nothrow @safe
{
return jm_app.data.length < 1;
}
void push( in JsonModifManyPO!permits_overwrite other )
{
push( other.jm_arr );
}
void push( in JsonModif[] jm_arr )
{
foreach (jm; jm_arr)
push( jm );
}
void push( in JsonModif jm )
{
push( jm.where, jm.what );
}
void push(T)( in string dot_where, in T what )
{
push( dot_where.split( '.' ).array, what );
}
void push(T)( in Jsonplace where, in T what )
{
static if (!is(T == JSONValue))
{
push( where, JSONValue( what ) ); // Convenience wrapper
}
else
{
// Core implementation for the above wrappers
static if (!permits_overwrite) moso.check_not_yet_and_set( where );
jm_app.put( JsonModif( where, what ) );
}
}
size_t size() const pure @nogc @safe
{
return jm_app.data.length;
}
// Serialization
override string toString() const
{
auto c_app = appender!(char[]);
toString( c => c_app.put( c ) );
return assumeUnique( c_app.data );
}
void toString(scope void delegate(const(char)[]) sink) const
{
foreach (jm; jm_arr)
{
auto where_str = to!string( jm.where );
auto what_str = jm.what.toString( JSONOptions.specialFloatLiterals );
mixin(alwaysAssertStderr(`!where_str.canFind('\n')`));
mixin(alwaysAssertStderr(`!what_str.canFind('\n')`));
sink( where_str~'\n' ); // single line
sink( what_str~"\n\n" ); // single line + extra empty line for human readability for debugging
}
}
}
static auto jmm_fromString(bool permits_overwrite = false)( in string s )
{
auto line_arr = s.splitLines;
immutable n = line_arr.length;
mixin(alwaysAssertStderr(`0 == n%3`));
auto jmm = new JsonModifManyPO!permits_overwrite;
for (size_t i = 0; i < n;)
{
// read
auto where_str = line_arr[ i++ ];
auto what_str = line_arr[ i++ ];
i++; // human readability for debugging
// parse
auto where = to!Jsonplace( where_str );
auto what = parseJSON( what_str, -1, JSONOptions.specialFloatLiterals );
// store
jmm.push( where, what );
}
return jmm;
}
// -------------------- Application --------------------
JSONValue json_modify(bool permits_overwrite)( in JsonModifManyPO!permits_overwrite jmm, in JSONValue j )
{
auto ret = json_deep_copy( j );
json_modify_inplace!permits_overwrite( jmm, ret );
return ret;
}
void json_modify_inplace(bool permits_overwrite)( in JsonModifManyPO!permits_overwrite jmm, ref JSONValue j )
{
foreach (jm; jmm.jm_arr)
{
static if (false)
{
debug
{
import std.stdio;
writeln("xxx j:", j.toString( JSONOptions.specialFloatLiterals ) );
writeln("xxx jm.where:", jm.where);
writeln("xxx jm.what:", jm.what.toString( JSONOptions.specialFloatLiterals ));
}
}
json_set_place( j, jm.where, jm.what );
}
}
// -------------------- private core --------------------
private struct ModifiedSofar
{
ModifiedSofar[string] subset;
void clear() pure nothrow
{
subset.clear;
}
void check_not_yet_and_set( in Jsonplace where )
{
immutable w0 = where[ 0 ];
if (1 < where.length)
{
auto sub_moso = subset.aa_getInit( w0 );
sub_moso.check_not_yet_and_set( where[ 1..$ ] );
return;
}
if (w0 in subset)
{
stderr.writeln( "lib_json_modif: error: w0: ", w0 );
stderr.writeln( "lib_json_modif: error: subset: ", subset );
stderr.flush;
}
mixin(alwaysAssertStderr(`w0 !in subset`));
subset[ w0 ] = ModifiedSofar(); // leaf
}
};
unittest
{
import std.exception;
import std.path;
import std.stdio;
import d_glat.core_array;
writeln;
writeln( "unittest starts: ", baseName( __FILE__ ) );
immutable verbose = false;
immutable string _ici = `__FILE__ ~ "@line:" ~ to!string( __LINE__ )`;
import core.exception : AssertError;
{
// Wtf#0
auto j = parseJSON( `{"a":456}` );
json_set_place( j, "a", parseJSON( `{"abc":123}`) );
if (verbose) writeln( mixin(_ici), ": j: ", j.toString );
assert( json_equals( j, `{"a":{"abc":123}}` ) );
}
{
// Wtf#0
auto j = parseJSON( `{}` );
json_set_place( j, "a", parseJSON( `{"abc":123}`) );
if (verbose) writeln( mixin(_ici), ": j: ", j.toString );
assert( json_equals( j, `{"a":{"abc":123}}` ) );
}
{
auto jmm = new JsonModifMany;
jmm.push( ["a","b","c"], parseJSON( `{"xyz":123}` ) );
jmm.push( "a.b.d", parseJSON( `{"tuv":456}` ) );
jmm.push( "e.f.g", parseJSON( `{"rst":789}`) );
auto j = parseJSON( `{}` );
auto j2 = json_modify( jmm, j );
immutable jstr_expected = `{"a":{"b":{"c":{"xyz":123},"d":{"tuv":456}}},"e":{"f":{"g":{"rst":789}}}}`;
if (verbose) writeln( mixin(_ici), ": j: ", j.toString );
if (verbose) writeln( mixin(_ici), ": j2: ", j2.toString );
assert( json_equals( j, parseJSON( `{}` ) ) ); // `j` not modified
assert( json_equals( j2, parseJSON( jstr_expected ) ) ); // `j2` the copied, modified version of `j`
json_modify_inplace( jmm, j );
assert( json_equals( j, parseJSON( jstr_expected ) ) ); // `j` modified
assert( json_equals( j2, parseJSON( jstr_expected ) ) ); // `j2` remained the same
}
{
auto jmm = new JsonModifMany;
jmm.push( ["a","b","c"], parseJSON( `{"xyz":123}` ) );
jmm.push( "a.b.d", parseJSON( `{"tuv":456}` ) );
assertThrown!(Exception)( jmm.push( "a.b", parseJSON( `{"some":"overwrite"}` ) ) );
}
{
auto jmm = new JsonModifManyPO!(/*permit_overwrite:*/true);
jmm.push( ["a","b","c"], parseJSON( `{"xyz":123}` ) );
jmm.push( "a.b.d", parseJSON( `{"tuv":456}` ) );
jmm.push( "a.b", parseJSON( `{"some":"overwrite"}` ) );
auto j = parseJSON( `{"x":{"y":"z"}}` );
json_modify_inplace( jmm, j );
if (verbose) writeln( mixin(_ici), ": j:", j );
assert( json_equals( j, `{"x":{"y":"z"},"a":{"b":{"some":"overwrite"}}}` ) );
}
writeln;
writeln( "unittest passed: ", baseName( __FILE__ ) );
}