Skip to content

Commit

Permalink
Merge pull request #1782 from The-Lum/Evol
Browse files Browse the repository at this point in the history
feat: update JSON builtin function `%json_set` to allow setting of _object_ and _subsequent object_
  • Loading branch information
arnaudroques committed May 15, 2024
2 parents 2418b65 + 6f5f2ee commit 19e5de3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/net/sourceforge/plantuml/tim/stdlib/JsonSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public TFunctionSignature getSignature() {

@Override
public boolean canCover(int nbArg, Set<String> namedArgument) {
return nbArg == 3;
return nbArg == 2 || nbArg == 3;
}

@Override
Expand All @@ -71,21 +71,35 @@ public TValue executeReturnFunction(TContext context, TMemory memory, StringLoca

if (!json.isArray() && !json.isObject())
return data;
if (json.isArray()) {
if (values.get(1).isNumber()) {
final Integer index = values.get(1).toInt();

switch (values.size()) {
case 2:
if (json.isObject()) {
final JsonValue value = values.get(1).toJsonValue();
if (value.isObject())
json.asObject().deepMerge(value.asObject());
return TValue.fromJson(json);
}
case 3:
if (json.isArray()) {
if (values.get(1).isNumber()) {
final Integer index = values.get(1).toInt();
final JsonValue value = values.get(2).toJsonValue();
if (0 <= index && index < json.asArray().size())
json.asArray().set(index, value);
}
return TValue.fromJson(json);
}
if (json.isObject()) {
final String name = values.get(1).toString();
final JsonValue value = values.get(2).toJsonValue();
if (0 <= index && index < json.asArray().size())
json.asArray().set(index, value);
json.asObject().set(name, value);
return TValue.fromJson(json);
}
return TValue.fromJson(json);
}
if (json.isObject()) {
final String name = values.get(1).toString();
final JsonValue value = values.get(2).toJsonValue();
json.asObject().set(name, value);
return TValue.fromJson(json);

default:
assert false; // Should not append because of canCover()
throw new EaterException("Error on json_set: Too many arguments", location);
}
throw new EaterException("Bad JSON type", location);
}
}
18 changes: 18 additions & 0 deletions test/net/sourceforge/plantuml/tim/stdlib/JsonSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,22 @@ void Test_with_Object_Json_add_Str(@ConvertWith(StringJsonConverter.class) JsonV
void Test_with_Object_Json_add_Int(@ConvertWith(StringJsonConverter.class) JsonValue input1, String input2, Integer input3, String expected) throws EaterException {
assertTimExpectedOutputFromInput(cut, input1, input2, input3, expected);
}

@ParameterizedTest(name = "[{index}] " + cutName + "({0}, {1}) = {2}")
@CsvSource(value = {
" {}, {\"a\":1}, {\"a\":1}",
" {\"age\" : 30}, {\"name\":123}, '{\"age\":30,\"name\":123}'",
" '{\"age\" : 30, \"name\":\"Bob\"}', {\"name\":123}, '{\"age\":30,\"name\":123}'",
" '{\"partlen\": \"2\", \"color\": {\"A\":\"red\", \"B\":\"blue\"}}', '{\"color\":{\"A\":\"black\"}}', '{\"partlen\":\"2\",\"color\":{\"A\":\"black\",\"B\":\"blue\"}}'",
" {}, \"a\", {}",
" {}, \"a b c\", {}",
" {}, 123, {}",
" {}, '[1,2]', {}",
" {}, true, {}",
" {}, false, {}",
" {}, null, {}",
})
void Test_with_Object_Json_add_Object(@ConvertWith(StringJsonConverter.class) JsonValue input1, @ConvertWith(StringJsonConverter.class) JsonValue input2, String expected) throws EaterException {
assertTimExpectedOutputFromInput(cut, input1, input2, expected);
}
}

0 comments on commit 19e5de3

Please sign in to comment.