-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
54 additions
and
27 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
packages/common/src/codegen/render-solidity/renderEnums.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { renderEnums } from "./renderEnums"; | ||
|
||
describe("renderEnums", () => { | ||
it("renders a Solidity file with enums", () => { | ||
expect( | ||
renderEnums({ | ||
Direction: ["left", "up", "right", "down"], | ||
}), | ||
).toMatchInlineSnapshot(` | ||
" | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.24; | ||
/* Autogenerated file. Do not edit manually. */ | ||
enum Direction { | ||
left, up, right, down | ||
} | ||
" | ||
`); | ||
}); | ||
}); |
31 changes: 17 additions & 14 deletions
31
packages/common/src/codegen/render-solidity/renderEnums.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
import { renderArguments, renderList, renderedSolidityHeader } from "./common"; | ||
import { RenderEnum } from "./types"; | ||
import { renderedSolidityHeader } from "./common"; | ||
|
||
// importing this from config or store would be a cyclic dependency :( | ||
type Enums = { | ||
readonly [name: string]: readonly [string, ...string[]]; | ||
}; | ||
|
||
/** | ||
* Render a list of enum data as solidity enum definitions | ||
*/ | ||
export function renderEnums(enums: RenderEnum[]): string { | ||
let result = renderedSolidityHeader; | ||
|
||
result += renderList( | ||
enums, | ||
({ name, memberNames }) => ` | ||
enum ${name} { | ||
${renderArguments(memberNames)} | ||
} | ||
`, | ||
); | ||
export function renderEnums(enums: Enums): string { | ||
return ` | ||
${renderedSolidityHeader} | ||
return result; | ||
${Object.entries(enums).map( | ||
([name, values]) => ` | ||
enum ${name} { | ||
${values.join(", ")} | ||
} | ||
`, | ||
)} | ||
`; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,9 @@ | ||
import { renderEnums } from "@latticexyz/common/codegen"; | ||
import { StoreConfig } from "../config"; | ||
import { Store } from "../config/v2"; | ||
|
||
/** | ||
* Renders Solidity code for enums defined in the provided config | ||
*/ | ||
export function renderTypesFromConfig(config: StoreConfig) { | ||
const enums = Object.keys(config.enums).map((name) => ({ | ||
name, | ||
memberNames: config.enums[name], | ||
})); | ||
|
||
return renderEnums(enums); | ||
export function renderTypesFromConfig(config: Store) { | ||
return renderEnums(config.enums); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters