Skip to content

Commit

Permalink
refactor(common,store): clean up codegen naming [N-04] (#2115)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Jan 11, 2024
1 parent 063daf8 commit 73f8dca
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 36 deletions.
4 changes: 2 additions & 2 deletions packages/common/src/codegen/render-solidity/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export function renderWithStore(
_store: string;
_commentSuffix: string;
_methodNamePrefix: string;
_internal?: boolean;
_useExplicitFieldLayout?: boolean;
}) => string
): string {
let result = "";
Expand All @@ -135,7 +135,7 @@ export function renderWithStore(
_store: "StoreCore",
_commentSuffix: "",
_methodNamePrefix: "_",
_internal: true,
_useExplicitFieldLayout: true,
});

if (storeArgument) {
Expand Down
64 changes: 36 additions & 28 deletions packages/store/ts/codegen/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,21 @@ export function renderRecordMethods(options: RenderTableOptions) {
);
}

result += renderWithStore(storeArgument, ({ _typedStore, _store, _commentSuffix, _methodNamePrefix, _internal }) => {
const externalArguments = renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
renderArguments(options.fields.map(({ name, typeWithLocation }) => `${typeWithLocation} ${name}`)),
]);

const internalArguments =
"_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData" + (_internal ? ", _fieldLayout" : "");

return `
result += renderWithStore(
storeArgument,
({ _typedStore, _store, _commentSuffix, _methodNamePrefix, _useExplicitFieldLayout }) => {
const externalArguments = renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
renderArguments(options.fields.map(({ name, typeWithLocation }) => `${typeWithLocation} ${name}`)),
]);

const internalArguments =
"_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData" +
(_useExplicitFieldLayout ? ", _fieldLayout" : "");

return `
/**
* @notice Set the full data using individual values${_commentSuffix}.
*/
Expand All @@ -62,12 +65,13 @@ export function renderRecordMethods(options: RenderTableOptions) {
${_store}.setRecord(${internalArguments});
}
`;
});
}
);

if (structName !== undefined) {
result += renderWithStore(
storeArgument,
({ _typedStore, _store, _commentSuffix, _methodNamePrefix, _internal }) => {
({ _typedStore, _store, _commentSuffix, _methodNamePrefix, _useExplicitFieldLayout }) => {
const externalArguments = renderArguments([
_typedStore,
_typedTableId,
Expand All @@ -76,7 +80,8 @@ export function renderRecordMethods(options: RenderTableOptions) {
]);

const internalArguments =
"_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData" + (_internal ? ", _fieldLayout" : "");
"_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData" +
(_useExplicitFieldLayout ? ", _fieldLayout" : "");

return `
/**
Expand Down Expand Up @@ -134,20 +139,23 @@ export function renderDeleteRecordMethods(options: RenderTableOptions) {
const { storeArgument } = options;
const { _typedTableId, _typedKeyArgs, _keyTupleDefinition } = renderCommonData(options);

return renderWithStore(storeArgument, ({ _typedStore, _store, _commentSuffix, _methodNamePrefix, _internal }) => {
const externalArguments = renderArguments([_typedStore, _typedTableId, _typedKeyArgs]);
const internalArguments = "_tableId, _keyTuple" + (_internal ? ", _fieldLayout" : "");
return renderWithStore(
storeArgument,
({ _typedStore, _store, _commentSuffix, _methodNamePrefix, _useExplicitFieldLayout }) => {
const externalArguments = renderArguments([_typedStore, _typedTableId, _typedKeyArgs]);
const internalArguments = "_tableId, _keyTuple" + (_useExplicitFieldLayout ? ", _fieldLayout" : "");

return `
/**
* @notice Delete all data for given keys${_commentSuffix}.
*/
function ${_methodNamePrefix}deleteRecord(${externalArguments}) internal {
${_keyTupleDefinition}
${_store}.deleteRecord(${internalArguments});
}
`;
});
return `
/**
* @notice Delete all data for given keys${_commentSuffix}.
*/
function ${_methodNamePrefix}deleteRecord(${externalArguments}) internal {
${_keyTupleDefinition}
${_store}.deleteRecord(${internalArguments});
}
`;
}
);
}

// Renders the `decode` function that parses a bytes blob into the table data
Expand Down
4 changes: 2 additions & 2 deletions packages/store/ts/codegen/renderTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function renderTable(options: RenderTableOptions) {
${renderEncodeStatic(staticFields)}
${renderEncodedLengths(dynamicFields)}
${renderEncodeLengths(dynamicFields)}
${renderEncodeDynamic(dynamicFields)}
Expand Down Expand Up @@ -191,7 +191,7 @@ function renderEncodeStatic(staticFields: RenderStaticField[]) {
`;
}

function renderEncodedLengths(dynamicFields: RenderDynamicField[]) {
function renderEncodeLengths(dynamicFields: RenderDynamicField[]) {
if (dynamicFields.length === 0) return "";

return `
Expand Down
2 changes: 1 addition & 1 deletion packages/world/ts/node/render-solidity/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./renderSystemInterface";
export * from "./renderWorld";
export * from "./renderWorldInterface";
export * from "./types";
export * from "./worldgen";
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "@latticexyz/common/codegen";
import type { RenderWorldOptions } from "./types";

export function renderWorld(options: RenderWorldOptions) {
export function renderWorldInterface(options: RenderWorldOptions) {
const { interfaceName, storeImportPath, worldImportPath, imports } = options;
const baseImports: AbsoluteImportDatum[] =
interfaceName === "IBaseWorld"
Expand Down
4 changes: 2 additions & 2 deletions packages/world/ts/node/render-solidity/worldgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from "path";
import { formatAndWriteSolidity, contractToInterface, type RelativeImportDatum } from "@latticexyz/common/codegen";
import { StoreConfig } from "@latticexyz/store";
import { renderSystemInterface } from "./renderSystemInterface";
import { renderWorld } from "./renderWorld";
import { renderWorldInterface } from "./renderWorldInterface";
import { resolveWorldConfig } from "../../config/resolveWorldConfig";
import { WorldConfig } from "../../config/types";

Expand Down Expand Up @@ -64,7 +64,7 @@ export async function worldgen(
}

// render IWorld
const output = renderWorld({
const output = renderWorldInterface({
interfaceName: config.worldInterfaceName,
imports: systemInterfaceImports,
storeImportPath: config.storeImportPath,
Expand Down

0 comments on commit 73f8dca

Please sign in to comment.