Skip to content

Commit

Permalink
style: improve readability of codegen code (#1474)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Sep 14, 2023
1 parent 4e0200f commit 6bff860
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 253 deletions.
5 changes: 3 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# suppress diffs for codegen in PRs
**/abi/**/* linguist-generated=true
**/codegen/**/* linguist-generated=true
**/abi/**/*.json linguist-generated=true
**/abi/**/*.json.d.ts linguist-generated=true
**/codegen/**/*.sol linguist-generated=true
54 changes: 27 additions & 27 deletions packages/store/ts/codegen/ephemeral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,41 @@ export function renderEphemeralMethods(options: RenderTableOptions) {
let result = renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix) => `
/** Emit the ephemeral event using individual values${_commentSuffix} */
function emitEphemeral(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
renderArguments(options.fields.map(({ name, typeWithLocation }) => `${typeWithLocation} ${name}`)),
])}) internal {
bytes memory _data = encode(${renderArguments(options.fields.map(({ name }) => name))});
/** Emit the ephemeral event using individual values${_commentSuffix} */
function emitEphemeral(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
renderArguments(options.fields.map(({ name, typeWithLocation }) => `${typeWithLocation} ${name}`)),
])}) internal {
bytes memory _data = encode(${renderArguments(options.fields.map(({ name }) => name))});
${_keyTupleDefinition}
${_keyTupleDefinition}
${_store}.emitEphemeralRecord(_tableId, _keyTuple, _data, getFieldLayout());
}
`
${_store}.emitEphemeralRecord(_tableId, _keyTuple, _data, getFieldLayout());
}
`
);

if (structName !== undefined) {
result += renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore) => `
/** Emit the ephemeral event using the data struct${_commentSuffix} */
function emitEphemeral(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
`${structName} memory _table`,
])}) internal {
emitEphemeral(${renderArguments([
_untypedStore,
_tableId,
_keyArgs,
renderArguments(options.fields.map(({ name }) => `_table.${name}`)),
])});
}
`
/** Emit the ephemeral event using the data struct${_commentSuffix} */
function emitEphemeral(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
`${structName} memory _table`,
])}) internal {
emitEphemeral(${renderArguments([
_untypedStore,
_tableId,
_keyArgs,
renderArguments(options.fields.map(({ name }) => `_table.${name}`)),
])});
}
`
);
}

Expand Down
76 changes: 38 additions & 38 deletions packages/store/ts/codegen/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,57 @@ export function renderRecordMethods(options: RenderTableOptions) {
let result = renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix) => `
/** Get the full data${_commentSuffix} */
function get(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
])}) internal view returns (${renderDecodedRecord(options)}) {
${_keyTupleDefinition}
bytes memory _blob = ${_store}.getRecord(_tableId, _keyTuple, getFieldLayout());
return decode(_blob);
}
`
/** Get the full data${_commentSuffix} */
function get(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
])}) internal view returns (${renderDecodedRecord(options)}) {
${_keyTupleDefinition}
bytes memory _blob = ${_store}.getRecord(_tableId, _keyTuple, getFieldLayout());
return decode(_blob);
}
`
);

result += renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix) => `
/** Set the full data using individual values${_commentSuffix} */
function set(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
renderArguments(options.fields.map(({ name, typeWithLocation }) => `${typeWithLocation} ${name}`)),
])}) internal {
bytes memory _data = encode(${renderArguments(options.fields.map(({ name }) => name))});
/** Set the full data using individual values${_commentSuffix} */
function set(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
renderArguments(options.fields.map(({ name, typeWithLocation }) => `${typeWithLocation} ${name}`)),
])}) internal {
bytes memory _data = encode(${renderArguments(options.fields.map(({ name }) => name))});
${_keyTupleDefinition}
${_keyTupleDefinition}
${_store}.setRecord(_tableId, _keyTuple, _data, getFieldLayout());
}
`
${_store}.setRecord(_tableId, _keyTuple, _data, getFieldLayout());
}
`
);

if (structName !== undefined) {
result += renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore) => `
/** Set the full data using the data struct${_commentSuffix} */
function set(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
`${structName} memory _table`,
])}) internal {
set(${renderArguments([
_untypedStore,
_tableId,
_keyArgs,
renderArguments(options.fields.map(({ name }) => `_table.${name}`)),
])});
}
`
/** Set the full data using the data struct${_commentSuffix} */
function set(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
`${structName} memory _table`,
])}) internal {
set(${renderArguments([
_untypedStore,
_tableId,
_keyArgs,
renderArguments(options.fields.map(({ name }) => `_table.${name}`)),
])});
}
`
);
}

Expand Down
Loading

0 comments on commit 6bff860

Please sign in to comment.