Skip to content

Commit

Permalink
feat!: remove Results from json setters (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
vdrg authored Sep 5, 2023
1 parent 57ac4e4 commit 6a601ae
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 77 deletions.
106 changes: 30 additions & 76 deletions src/_modules/Json.sol
Original file line number Diff line number Diff line change
Expand Up @@ -200,65 +200,49 @@ library json {
/// @param key The key that will hold the `value`.
/// @param value The value of `key`.
/// @return res The modified JsonObject struct.
function set(JsonObject memory obj, string memory key, bool value) internal returns (JsonResult memory res) {
if (obj.isImmutable()) {
return JsonError.immutableJson();
}

function set(JsonObject memory obj, string memory key, bool value) internal returns (JsonObject memory res) {
obj.serialized = vulcan.hevm.serializeBool(obj.id, key, value);
return Ok(obj);
return obj;
}

/// @dev Serializes and sets the key and value for the provided json object.
/// @param obj The json object to modify.
/// @param key The key that will hold the `value`.
/// @param value The value of `key`.
/// @return res The modified JsonObject struct.
function set(JsonObject memory obj, string memory key, uint256 value) internal returns (JsonResult memory res) {
if (obj.isImmutable()) {
return JsonError.immutableJson();
}
function set(JsonObject memory obj, string memory key, uint256 value) internal returns (JsonObject memory res) {
obj.serialized = vulcan.hevm.serializeUint(obj.id, key, value);
return Ok(obj);
return obj;
}

/// @dev Serializes and sets the key and value for the provided json object.
/// @param obj The json object to modify.
/// @param key The key that will hold the `value`.
/// @param value The value of `key`.
/// @return res The modified JsonObject struct.
function set(JsonObject memory obj, string memory key, int256 value) internal returns (JsonResult memory res) {
if (obj.isImmutable()) {
return JsonError.immutableJson();
}
function set(JsonObject memory obj, string memory key, int256 value) internal returns (JsonObject memory res) {
obj.serialized = vulcan.hevm.serializeInt(obj.id, key, value);
return Ok(obj);
return obj;
}

/// @dev Serializes and sets the key and value for the provided json object.
/// @param obj The json object to modify.
/// @param key The key that will hold the `value`.
/// @param value The value of `key`.
/// @return res The modified JsonObject struct.
function set(JsonObject memory obj, string memory key, address value) internal returns (JsonResult memory res) {
if (obj.isImmutable()) {
return JsonError.immutableJson();
}
function set(JsonObject memory obj, string memory key, address value) internal returns (JsonObject memory res) {
obj.serialized = vulcan.hevm.serializeAddress(obj.id, key, value);
return Ok(obj);
return obj;
}

/// @dev Serializes and sets the key and value for the provided json object.
/// @param obj The json object to modify.
/// @param key The key that will hold the `value`.
/// @param value The value of `key`.
/// @return res The modified JsonObject struct.
function set(JsonObject memory obj, string memory key, bytes32 value) internal returns (JsonResult memory res) {
if (obj.isImmutable()) {
return JsonError.immutableJson();
}
function set(JsonObject memory obj, string memory key, bytes32 value) internal returns (JsonObject memory res) {
obj.serialized = vulcan.hevm.serializeBytes32(obj.id, key, value);
return Ok(obj);
return obj;
}

/// @dev Serializes and sets the key and value for the provided json object.
Expand All @@ -268,13 +252,10 @@ library json {
/// @return res The modified JsonObject struct.
function set(JsonObject memory obj, string memory key, string memory value)
internal
returns (JsonResult memory res)
returns (JsonObject memory res)
{
if (obj.isImmutable()) {
return JsonError.immutableJson();
}
obj.serialized = vulcan.hevm.serializeString(obj.id, key, value);
return Ok(obj);
return obj;
}

/// @dev Serializes and sets the key and value for the provided json object.
Expand All @@ -284,13 +265,10 @@ library json {
/// @return res The modified JsonObject struct.
function set(JsonObject memory obj, string memory key, bytes memory value)
internal
returns (JsonResult memory res)
returns (JsonObject memory res)
{
if (obj.isImmutable()) {
return JsonError.immutableJson();
}
obj.serialized = vulcan.hevm.serializeBytes(obj.id, key, value);
return Ok(obj);
return obj;
}

/// @dev Serializes and sets the key and value for the provided json object.
Expand All @@ -300,13 +278,10 @@ library json {
/// @return res The modified JsonObject struct.
function set(JsonObject memory obj, string memory key, bool[] memory values)
internal
returns (JsonResult memory res)
returns (JsonObject memory res)
{
if (obj.isImmutable()) {
return JsonError.immutableJson();
}
obj.serialized = vulcan.hevm.serializeBool(obj.id, key, values);
return Ok(obj);
return obj;
}

/// @dev Serializes and sets the key and value for the provided json object.
Expand All @@ -316,13 +291,10 @@ library json {
/// @return res The modified JsonObject struct.
function set(JsonObject memory obj, string memory key, uint256[] memory values)
internal
returns (JsonResult memory res)
returns (JsonObject memory res)
{
if (obj.isImmutable()) {
return JsonError.immutableJson();
}
obj.serialized = vulcan.hevm.serializeUint(obj.id, key, values);
return Ok(obj);
return obj;
}

/// @dev Serializes and sets the key and value for the provided json object.
Expand All @@ -332,13 +304,10 @@ library json {
/// @return res The modified JsonObject struct.
function set(JsonObject memory obj, string memory key, int256[] memory values)
internal
returns (JsonResult memory res)
returns (JsonObject memory res)
{
if (obj.isImmutable()) {
return JsonError.immutableJson();
}
obj.serialized = vulcan.hevm.serializeInt(obj.id, key, values);
return Ok(obj);
return obj;
}

/// @dev Serializes and sets the key and value for the provided json object.
Expand All @@ -348,13 +317,10 @@ library json {
/// @return res The modified JsonObject struct.
function set(JsonObject memory obj, string memory key, address[] memory values)
internal
returns (JsonResult memory res)
returns (JsonObject memory res)
{
if (obj.isImmutable()) {
return JsonError.immutableJson();
}
obj.serialized = vulcan.hevm.serializeAddress(obj.id, key, values);
return Ok(obj);
return obj;
}

/// @dev Serializes and sets the key and value for the provided json object.
Expand All @@ -364,13 +330,10 @@ library json {
/// @return res The modified JsonObject struct.
function set(JsonObject memory obj, string memory key, bytes32[] memory values)
internal
returns (JsonResult memory res)
returns (JsonObject memory res)
{
if (obj.isImmutable()) {
return JsonError.immutableJson();
}
obj.serialized = vulcan.hevm.serializeBytes32(obj.id, key, values);
return Ok(obj);
return obj;
}

/// @dev Serializes and sets the key and value for the provided json object.
Expand All @@ -380,13 +343,10 @@ library json {
/// @return res The modified JsonObject struct.
function set(JsonObject memory obj, string memory key, string[] memory values)
internal
returns (JsonResult memory res)
returns (JsonObject memory res)
{
if (obj.isImmutable()) {
return JsonError.immutableJson();
}
obj.serialized = vulcan.hevm.serializeString(obj.id, key, values);
return Ok(obj);
return obj;
}

/// @dev Serializes and sets the key and value for the provided json object.
Expand All @@ -396,13 +356,10 @@ library json {
/// @return res The modified JsonObject struct.
function set(JsonObject memory obj, string memory key, bytes[] memory values)
internal
returns (JsonResult memory res)
returns (JsonObject memory res)
{
if (obj.isImmutable()) {
return JsonError.immutableJson();
}
obj.serialized = vulcan.hevm.serializeBytes(obj.id, key, values);
return Ok(obj);
return obj;
}

/// @dev Serializes and sets the key and value for the provided json object.
Expand All @@ -412,13 +369,10 @@ library json {
/// @return res The modified JsonObject struct.
function set(JsonObject memory obj, string memory key, JsonObject memory value)
internal
returns (JsonResult memory res)
returns (JsonObject memory res)
{
if (obj.isImmutable()) {
return JsonError.immutableJson();
}
obj.serialized = vulcan.hevm.serializeString(obj.id, key, value.serialized);
return Ok(obj);
return obj;
}

/// @dev Writes a JsonObject struct to a file.
Expand Down
2 changes: 1 addition & 1 deletion test/_modules/Json.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract JsonTest is Test {
}

function testParse() external {
JsonObject memory jsonObject = json.create().set("foo", string("bar")).unwrap();
JsonObject memory jsonObject = json.create().set("foo", string("bar"));
Foo memory obj = abi.decode(jsonObject.parse(), (Foo));
expect(obj.foo).toEqual("bar");
}
Expand Down

0 comments on commit 6a601ae

Please sign in to comment.