-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(store,world): enable namespaces input #2968
Merged
Merged
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
cd53d1b
add namespaces input to store
holic ec80234
reduce full-config tests
holic 68cc089
test that only one mode is used
holic 70b5958
enforce no duplicates
holic f4196ef
add multiple namespaces to world
holic cd002c7
conditionally write table index
holic a45f1f6
Merge remote-tracking branch 'origin/main' into holic/namespaces-input
holic 7e028b6
multiple namespaces from config
holic 9d5f962
fix tests
holic e2c5e2e
Merge remote-tracking branch 'origin/main' into holic/namespaces-input
holic 329f40a
enable systems
holic 55a86ce
label -> debugLabel
holic d666c1a
resolve systems with namespaces
holic fd2b0ce
system defaults via namespace
holic 613b5ee
fix world's worldgen
holic fd420b8
Create seven-melons-kneel.md
holic 73f965b
Update seven-melons-kneel.md
holic 78ff2fa
Update seven-melons-kneel.md
holic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,23 +1,25 @@ | ||
import { defineWorld } from "@latticexyz/world"; | ||
|
||
export default defineWorld({ | ||
namespace: "game", | ||
codegen: { namespaceDirectories: true }, | ||
tables: { | ||
Health: { | ||
schema: { | ||
player: "address", | ||
value: "uint32", | ||
namespaces: { | ||
game: { | ||
tables: { | ||
Health: { | ||
schema: { | ||
player: "address", | ||
value: "uint32", | ||
}, | ||
key: ["player"], | ||
}, | ||
Position: { | ||
schema: { | ||
player: "address", | ||
x: "int32", | ||
y: "int32", | ||
}, | ||
key: ["player"], | ||
}, | ||
}, | ||
key: ["player"], | ||
}, | ||
Position: { | ||
schema: { | ||
player: "address", | ||
x: "int32", | ||
y: "int32", | ||
}, | ||
key: ["player"], | ||
}, | ||
}, | ||
}); |
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...ple-namespaces/src/systems/HealSystem.sol → ...spaces/src/namespaces/game/HealSystem.sol
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
2 changes: 1 addition & 1 deletion
2
...ple-namespaces/src/systems/MoveSystem.sol → ...spaces/src/namespaces/game/MoveSystem.sol
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
7 changes: 7 additions & 0 deletions
7
examples/multiple-namespaces/src/namespaces/game/codegen/index.sol
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
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
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
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
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
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,41 @@ | ||
import { show } from "@arktype/util"; | ||
import { Namespaces } from "./output"; | ||
|
||
type flattenNamespacedTableKeys<config> = config extends { | ||
readonly namespaces: infer namespaces; | ||
} | ||
? { | ||
[namespaceLabel in keyof namespaces]: namespaces[namespaceLabel] extends { readonly tables: infer tables } | ||
? namespaceLabel extends "" | ||
? keyof tables | ||
: `${namespaceLabel & string}__${keyof tables & string}` | ||
: never; | ||
}[keyof namespaces] | ||
: never; | ||
|
||
/** | ||
* @internal Only kept for backwards compatibility | ||
*/ | ||
export type flattenNamespacedTables<config> = config extends { readonly namespaces: Namespaces } | ||
? { | ||
readonly [key in flattenNamespacedTableKeys<config>]: key extends `${infer namespaceLabel}__${infer tableLabel}` | ||
? config["namespaces"][namespaceLabel]["tables"][tableLabel] | ||
: config["namespaces"][""]["tables"][key]; | ||
} | ||
: never; | ||
|
||
/** | ||
* @internal Only kept for backwards compatibility | ||
*/ | ||
export function flattenNamespacedTables<config extends { readonly namespaces: Namespaces }>( | ||
config: config, | ||
): show<flattenNamespacedTables<config>> { | ||
return Object.fromEntries( | ||
Object.entries(config.namespaces).flatMap(([namespaceLabel, namespace]) => | ||
Object.entries(namespace.tables).map(([tableLabel, table]) => [ | ||
namespaceLabel === "" ? tableLabel : `${namespaceLabel}__${tableLabel}`, | ||
table, | ||
]), | ||
), | ||
) as never; | ||
} |
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
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
This file was deleted.
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { describe, it } from "vitest"; | ||
import { attest } from "@arktype/attest"; | ||
import { defineNamespaces } from "./namespaces"; | ||
|
||
describe("defineNamespaces", () => { | ||
it("should throw on duplicates", () => { | ||
attest(() => | ||
defineNamespaces({ | ||
First: { namespace: "app" }, | ||
Second: { namespace: "app" }, | ||
}), | ||
).throws("Found namespaces defined more than once in config: app"); | ||
}); | ||
}); |
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,58 @@ | ||
import { show, flatMorph } from "@arktype/util"; | ||
import { isObject, mergeIfUndefined } from "./generics"; | ||
import { NamespacesInput } from "./input"; | ||
import { AbiTypeScope, Scope } from "./scope"; | ||
import { validateNamespace, resolveNamespace } from "./namespace"; | ||
import { groupBy } from "@latticexyz/common/utils"; | ||
|
||
export type validateNamespaces<namespaces, scope extends Scope = AbiTypeScope> = { | ||
[label in keyof namespaces]: validateNamespace<namespaces[label], scope>; | ||
}; | ||
|
||
export function validateNamespaces<scope extends Scope = AbiTypeScope>( | ||
namespaces: unknown, | ||
scope: scope, | ||
): asserts namespaces is NamespacesInput { | ||
if (!isObject(namespaces)) { | ||
throw new Error(`Expected namespaces, received ${JSON.stringify(namespaces)}`); | ||
} | ||
for (const namespace of Object.values(namespaces)) { | ||
validateNamespace(namespace, scope); | ||
} | ||
} | ||
|
||
export type resolveNamespaces<namespaces, scope extends Scope = AbiTypeScope> = { | ||
readonly [label in keyof namespaces]: resolveNamespace<mergeIfUndefined<namespaces[label], { label: label }>, scope>; | ||
}; | ||
|
||
export function resolveNamespaces<input extends NamespacesInput, scope extends Scope = AbiTypeScope>( | ||
input: input, | ||
scope: scope, | ||
): show<resolveNamespaces<input, scope>> { | ||
if (!isObject(input)) { | ||
throw new Error(`Expected namespaces config, received ${JSON.stringify(input)}`); | ||
} | ||
|
||
const namespaces = flatMorph(input as NamespacesInput, (label, namespace) => [ | ||
label, | ||
resolveNamespace(mergeIfUndefined(namespace, { label }), scope), | ||
]); | ||
|
||
// This should probably be in `validate`, but `namespace` gets set during the resolve step above, so it's easier to validate here. | ||
const duplicates = Array.from(groupBy(Object.values(namespaces), (namespace) => namespace.namespace).entries()) | ||
.filter(([, entries]) => entries.length > 1) | ||
.map(([namespace]) => namespace); | ||
if (duplicates.length > 0) { | ||
throw new Error(`Found namespaces defined more than once in config: ${duplicates.join(", ")}`); | ||
} | ||
|
||
return namespaces as never; | ||
} | ||
|
||
export function defineNamespaces<input, scope extends Scope = AbiTypeScope>( | ||
input: validateNamespaces<input, scope>, | ||
scope: scope = AbiTypeScope as never, | ||
): show<resolveNamespaces<input, scope>> { | ||
validateNamespaces(input, scope); | ||
return resolveNamespaces(input, scope) as never; | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooc do we know that it impacts compile times?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't measured it but it's very likely given 1) more files need to be compiled, even if not used and 2) reports from @0xhank and others on more files = worse compile times (which may be fixable upstream in forge/foundry/solc)
The main motivation to deprecate this here is that any change to tables impacts bytecode of all things import
index.sol
, even if they aren't importing the table that changed. Which means systems become less consistent in terms of bytecode.