Skip to content

Commit 5a802d7

Browse files
authored
feat: add keyExists and default value support to stdJson and stdToml (#605)
* feat: add keyExists to stdJson and stdToml * feat: add 'or' methods for reading defaults
1 parent beb836e commit 5a802d7

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed

src/StdJson.sol

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import {VmSafe} from "./Vm.sol";
2525
library stdJson {
2626
VmSafe private constant vm = VmSafe(address(uint160(uint256(keccak256("hevm cheat code")))));
2727

28+
function keyExists(string memory json, string memory key) internal view returns (bool) {
29+
return vm.keyExistsJson(json, key);
30+
}
31+
2832
function parseRaw(string memory json, string memory key) internal pure returns (bytes memory) {
2933
return vm.parseJson(json, key);
3034
}
@@ -85,6 +89,106 @@ library stdJson {
8589
return vm.parseJsonBytesArray(json, key);
8690
}
8791

92+
function readUintOr(string memory json, string memory key, uint256 defaultValue) internal view returns (uint256) {
93+
return keyExists(json, key) ? readUint(json, key) : defaultValue;
94+
}
95+
96+
function readUintArrayOr(string memory json, string memory key, uint256[] memory defaultValue)
97+
internal
98+
view
99+
returns (uint256[] memory)
100+
{
101+
return keyExists(json, key) ? readUintArray(json, key) : defaultValue;
102+
}
103+
104+
function readIntOr(string memory json, string memory key, int256 defaultValue) internal view returns (int256) {
105+
return keyExists(json, key) ? readInt(json, key) : defaultValue;
106+
}
107+
108+
function readIntArrayOr(string memory json, string memory key, int256[] memory defaultValue)
109+
internal
110+
view
111+
returns (int256[] memory)
112+
{
113+
return keyExists(json, key) ? readIntArray(json, key) : defaultValue;
114+
}
115+
116+
function readBytes32Or(string memory json, string memory key, bytes32 defaultValue)
117+
internal
118+
view
119+
returns (bytes32)
120+
{
121+
return keyExists(json, key) ? readBytes32(json, key) : defaultValue;
122+
}
123+
124+
function readBytes32ArrayOr(string memory json, string memory key, bytes32[] memory defaultValue)
125+
internal
126+
view
127+
returns (bytes32[] memory)
128+
{
129+
return keyExists(json, key) ? readBytes32Array(json, key) : defaultValue;
130+
}
131+
132+
function readStringOr(string memory json, string memory key, string memory defaultValue)
133+
internal
134+
view
135+
returns (string memory)
136+
{
137+
return keyExists(json, key) ? readString(json, key) : defaultValue;
138+
}
139+
140+
function readStringArrayOr(string memory json, string memory key, string[] memory defaultValue)
141+
internal
142+
view
143+
returns (string[] memory)
144+
{
145+
return keyExists(json, key) ? readStringArray(json, key) : defaultValue;
146+
}
147+
148+
function readAddressOr(string memory json, string memory key, address defaultValue)
149+
internal
150+
view
151+
returns (address)
152+
{
153+
return keyExists(json, key) ? readAddress(json, key) : defaultValue;
154+
}
155+
156+
function readAddressArrayOr(string memory json, string memory key, address[] memory defaultValue)
157+
internal
158+
view
159+
returns (address[] memory)
160+
{
161+
return keyExists(json, key) ? readAddressArray(json, key) : defaultValue;
162+
}
163+
164+
function readBoolOr(string memory json, string memory key, bool defaultValue) internal view returns (bool) {
165+
return keyExists(json, key) ? readBool(json, key) : defaultValue;
166+
}
167+
168+
function readBoolArrayOr(string memory json, string memory key, bool[] memory defaultValue)
169+
internal
170+
view
171+
returns (bool[] memory)
172+
{
173+
return keyExists(json, key) ? readBoolArray(json, key) : defaultValue;
174+
}
175+
176+
function readBytesOr(string memory json, string memory key, bytes memory defaultValue)
177+
internal
178+
view
179+
returns (bytes memory)
180+
{
181+
return keyExists(json, key) ? readBytes(json, key) : defaultValue;
182+
}
183+
184+
function readBytesArrayOr(string memory json, string memory key, bytes[] memory defaultValue)
185+
internal
186+
view
187+
returns (bytes[] memory)
188+
{
189+
return keyExists(json, key) ? readBytesArray(json, key) : defaultValue;
190+
}
191+
88192
function serialize(string memory jsonKey, string memory rootObject) internal returns (string memory) {
89193
return vm.serializeJson(jsonKey, rootObject);
90194
}

src/StdToml.sol

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import {VmSafe} from "./Vm.sol";
2525
library stdToml {
2626
VmSafe private constant vm = VmSafe(address(uint160(uint256(keccak256("hevm cheat code")))));
2727

28+
function keyExists(string memory toml, string memory key) internal view returns (bool) {
29+
return vm.keyExistsToml(toml, key);
30+
}
31+
2832
function parseRaw(string memory toml, string memory key) internal pure returns (bytes memory) {
2933
return vm.parseToml(toml, key);
3034
}
@@ -85,6 +89,106 @@ library stdToml {
8589
return vm.parseTomlBytesArray(toml, key);
8690
}
8791

92+
function readUintOr(string memory toml, string memory key, uint256 defaultValue) internal view returns (uint256) {
93+
return keyExists(toml, key) ? readUint(toml, key) : defaultValue;
94+
}
95+
96+
function readUintArrayOr(string memory toml, string memory key, uint256[] memory defaultValue)
97+
internal
98+
view
99+
returns (uint256[] memory)
100+
{
101+
return keyExists(toml, key) ? readUintArray(toml, key) : defaultValue;
102+
}
103+
104+
function readIntOr(string memory toml, string memory key, int256 defaultValue) internal view returns (int256) {
105+
return keyExists(toml, key) ? readInt(toml, key) : defaultValue;
106+
}
107+
108+
function readIntArrayOr(string memory toml, string memory key, int256[] memory defaultValue)
109+
internal
110+
view
111+
returns (int256[] memory)
112+
{
113+
return keyExists(toml, key) ? readIntArray(toml, key) : defaultValue;
114+
}
115+
116+
function readBytes32Or(string memory toml, string memory key, bytes32 defaultValue)
117+
internal
118+
view
119+
returns (bytes32)
120+
{
121+
return keyExists(toml, key) ? readBytes32(toml, key) : defaultValue;
122+
}
123+
124+
function readBytes32ArrayOr(string memory toml, string memory key, bytes32[] memory defaultValue)
125+
internal
126+
view
127+
returns (bytes32[] memory)
128+
{
129+
return keyExists(toml, key) ? readBytes32Array(toml, key) : defaultValue;
130+
}
131+
132+
function readStringOr(string memory toml, string memory key, string memory defaultValue)
133+
internal
134+
view
135+
returns (string memory)
136+
{
137+
return keyExists(toml, key) ? readString(toml, key) : defaultValue;
138+
}
139+
140+
function readStringArrayOr(string memory toml, string memory key, string[] memory defaultValue)
141+
internal
142+
view
143+
returns (string[] memory)
144+
{
145+
return keyExists(toml, key) ? readStringArray(toml, key) : defaultValue;
146+
}
147+
148+
function readAddressOr(string memory toml, string memory key, address defaultValue)
149+
internal
150+
view
151+
returns (address)
152+
{
153+
return keyExists(toml, key) ? readAddress(toml, key) : defaultValue;
154+
}
155+
156+
function readAddressArrayOr(string memory toml, string memory key, address[] memory defaultValue)
157+
internal
158+
view
159+
returns (address[] memory)
160+
{
161+
return keyExists(toml, key) ? readAddressArray(toml, key) : defaultValue;
162+
}
163+
164+
function readBoolOr(string memory toml, string memory key, bool defaultValue) internal view returns (bool) {
165+
return keyExists(toml, key) ? readBool(toml, key) : defaultValue;
166+
}
167+
168+
function readBoolArrayOr(string memory toml, string memory key, bool[] memory defaultValue)
169+
internal
170+
view
171+
returns (bool[] memory)
172+
{
173+
return keyExists(toml, key) ? readBoolArray(toml, key) : defaultValue;
174+
}
175+
176+
function readBytesOr(string memory toml, string memory key, bytes memory defaultValue)
177+
internal
178+
view
179+
returns (bytes memory)
180+
{
181+
return keyExists(toml, key) ? readBytes(toml, key) : defaultValue;
182+
}
183+
184+
function readBytesArrayOr(string memory toml, string memory key, bytes[] memory defaultValue)
185+
internal
186+
view
187+
returns (bytes[] memory)
188+
{
189+
return keyExists(toml, key) ? readBytesArray(toml, key) : defaultValue;
190+
}
191+
88192
function serialize(string memory jsonKey, string memory rootObject) internal returns (string memory) {
89193
return vm.serializeJson(jsonKey, rootObject);
90194
}

0 commit comments

Comments
 (0)