@@ -278,18 +278,8 @@ else if (isReference(field)) {
278
278
mode .apply (hash , field , newValue );
279
279
}
280
280
else {
281
- if (!hash .containsField (field )) {
282
- final Value value = Arrays .asList (ReservedField .$prepend .name (), ReservedField .$append .name ())
283
- .contains (tail (path )[0 ]) ? Value .newArray () : Value .newHash ();
284
- hash .put (field , value .withPathSet (newValue .getPath ()));
285
- }
286
- else {
287
- final Value value = hash .get (field );
288
- if (value .isString ()) {
289
- hash .put (field , Value .newArray (a -> a .add (value )));
290
- }
291
- }
292
- insertInto (hash .get (field ), mode , newValue , field , tail (path ));
281
+ final String [] tail = tail (path );
282
+ insertInto (getContainerValue (hash , field , newValue .getPath (), tail [0 ]), mode , newValue , field , tail );
293
283
}
294
284
295
285
return new Value (hash );
@@ -309,6 +299,23 @@ private Value insertInto(final Value value, final InsertMode mode, final Value n
309
299
}
310
300
}
311
301
302
+ private Value getContainerValue (final Hash hash , final String field , final String newPath , final String nextField ) {
303
+ Value result = hash .get (field );
304
+ if (result == null ) {
305
+ result = (nextField .equals (ReservedField .$prepend .name ()) || nextField .equals (ReservedField .$append .name ()) ?
306
+ Value .newArray () : Value .newHash ()).withPathSet (newPath );
307
+ hash .put (field , result );
308
+ }
309
+ else {
310
+ if (result .isString ()) {
311
+ final Value value = result ;
312
+ result = Value .newArray (a -> a .add (value ));
313
+ hash .put (field , result );
314
+ }
315
+ }
316
+ return result ;
317
+ }
318
+
312
319
private String [] tail (final String [] fields ) {
313
320
return Arrays .copyOfRange (fields , 1 , fields .length );
314
321
}
0 commit comments