Skip to content

Commit

Permalink
don't render getter methods in offchain tables
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Sep 20, 2023
1 parent abce1e1 commit f618a62
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 189 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 0 additions & 66 deletions packages/cli/contracts/src/codegen/tables/Offchain.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

140 changes: 72 additions & 68 deletions packages/store/ts/codegen/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,38 @@ export function renderFieldMethods(options: RenderTableOptions) {
// For dynamic fields, compute the field index relative to the end of the static fields
const _typedFieldName = `${field.typeWithLocation} ${field.name}`;

result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
/** Get ${field.name}${_commentSuffix} */
function ${_methodNamePrefix}get${_methodNameSuffix}(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
])}) internal view returns (${_typedFieldName}) {
${_keyTupleDefinition}
${
field.isDynamic
? `bytes memory _blob = ${_store}.getDynamicField(
_tableId,
_keyTuple,
${schemaIndex - options.staticFields.length}
);`
: `bytes32 _blob = ${_store}.getStaticField(
_tableId,
_keyTuple,
${schemaIndex},
_fieldLayout
);`
if (options.withGetters) {
result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
/** Get ${field.name}${_commentSuffix} */
function ${_methodNamePrefix}get${_methodNameSuffix}(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
])}) internal view returns (${_typedFieldName}) {
${_keyTupleDefinition}
${
field.isDynamic
? `bytes memory _blob = ${_store}.getDynamicField(
_tableId,
_keyTuple,
${schemaIndex - options.staticFields.length}
);`
: `bytes32 _blob = ${_store}.getStaticField(
_tableId,
_keyTuple,
${schemaIndex},
_fieldLayout
);`
}
return ${renderDecodeFieldSingle(field)};
}
return ${renderDecodeFieldSingle(field)};
}
`
)
);
)
);
}

result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(
Expand All @@ -73,40 +75,41 @@ export function renderFieldMethods(options: RenderTableOptions) {
if (field.isDynamic) {
const portionData = fieldPortionData(field);

result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
/** Get the length of ${field.name}${_commentSuffix} */
function ${_methodNamePrefix}length${_methodNameSuffix}(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
])}) internal view returns (uint256) {
${_keyTupleDefinition}
uint256 _byteLength = ${_store}.getFieldLength(_tableId, _keyTuple, ${schemaIndex}, _fieldLayout);
unchecked {
return _byteLength / ${portionData.elementLength};
if (options.withGetters) {
result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
/** Get the length of ${field.name}${_commentSuffix} */
function ${_methodNamePrefix}length${_methodNameSuffix}(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
])}) internal view returns (uint256) {
${_keyTupleDefinition}
uint256 _byteLength = ${_store}.getFieldLength(_tableId, _keyTuple, ${schemaIndex}, _fieldLayout);
unchecked {
return _byteLength / ${portionData.elementLength};
}
}
}
`
)
);
)
);

result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
/**
* Get an item of ${field.name}${_commentSuffix}
* (unchecked, returns invalid data if index overflows)
*/
function ${_methodNamePrefix}getItem${_methodNameSuffix}(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
"uint256 _index",
])}) internal view returns (${portionData.typeWithLocation}) {
result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
/**
* Get an item of ${field.name}${_commentSuffix}
* (unchecked, returns invalid data if index overflows)
*/
function ${_methodNamePrefix}getItem${_methodNameSuffix}(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
"uint256 _index",
])}) internal view returns (${portionData.typeWithLocation}) {
${_keyTupleDefinition}
unchecked {
bytes memory _blob = ${_store}.getFieldSlice(
Expand All @@ -116,20 +119,21 @@ export function renderFieldMethods(options: RenderTableOptions) {
_fieldLayout,
_index * ${portionData.elementLength},
(_index + 1) * ${portionData.elementLength}
);
return ${portionData.decoded};
);
return ${portionData.decoded};
}
}
}
`
)
);
`
)
);
}

result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
/** Push ${portionData.title} to ${field.name}${_commentSuffix} */
function ${_methodNamePrefix}push${_methodNameSuffix}(${renderArguments([
/** Push ${portionData.title} to ${field.name}${_commentSuffix} */
function ${_methodNamePrefix}push${_methodNameSuffix}(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
Expand All @@ -138,7 +142,7 @@ export function renderFieldMethods(options: RenderTableOptions) {
${_keyTupleDefinition}
${_store}.pushToField(_tableId, _keyTuple, ${schemaIndex}, ${portionData.encoded}, _fieldLayout);
}
`
`
)
);

Expand Down
42 changes: 23 additions & 19 deletions packages/store/ts/codegen/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,30 @@ export function renderRecordMethods(options: RenderTableOptions) {
const { structName, storeArgument } = options;
const { _tableId, _typedTableId, _keyArgs, _typedKeyArgs, _keyTupleDefinition } = renderCommonData(options);

let result = renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
/** Get the full data${_commentSuffix} */
function ${_methodNamePrefix}get(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
])}) internal view returns (${renderDecodedRecord(options)}) {
${_keyTupleDefinition}
let result = "";

(
bytes memory _staticData,
PackedCounter _encodedLengths,
bytes memory _dynamicData
) = ${_store}.getRecord(_tableId, _keyTuple, _fieldLayout);
return decode(_staticData, _encodedLengths, _dynamicData);
}
`
);
if (options.withGetters) {
result += renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
/** Get the full data${_commentSuffix} */
function ${_methodNamePrefix}get(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
])}) internal view returns (${renderDecodedRecord(options)}) {
${_keyTupleDefinition}
(
bytes memory _staticData,
PackedCounter _encodedLengths,
bytes memory _dynamicData
) = ${_store}.getRecord(_tableId, _keyTuple, _fieldLayout);
return decode(_staticData, _encodedLengths, _dynamicData);
}
`
);
}

result += renderWithStore(
storeArgument,
Expand Down
1 change: 1 addition & 0 deletions packages/store/ts/codegen/tableOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export function getTableOptions(config: StoreConfig): TableOptions[] {
fields,
staticFields,
dynamicFields,
withGetters: !tableData.offchainOnly,
withRecordMethods,
withDynamicFieldMethods: !tableData.offchainOnly,
withSuffixlessFieldMethods,
Expand Down
2 changes: 2 additions & 0 deletions packages/store/ts/codegen/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface RenderTableOptions {
fields: RenderField[];
staticFields: RenderStaticField[];
dynamicFields: RenderDynamicField[];
/** Whether to render getter functions */
withGetters: boolean;
/** Whether to render dynamic field methods (push, pop, update) */
withDynamicFieldMethods: boolean;
/** Whether to render get/set methods for the whole record */
Expand Down

0 comments on commit f618a62

Please sign in to comment.