Skip to content

Commit

Permalink
refactor(schema-type,protocol-parser): explicit internal vs external …
Browse files Browse the repository at this point in the history
…exports (#2452)
  • Loading branch information
holic authored Mar 15, 2024
1 parent 2a4b289 commit b38c096
Show file tree
Hide file tree
Showing 93 changed files with 185 additions and 127 deletions.
6 changes: 6 additions & 0 deletions .changeset/honest-singers-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@latticexyz/protocol-parser": major
"@latticexyz/schema-type": major
---

Moved all existing exports to a `/internal` import path to indicate that these are now internal-only and deprecated. We'll be replacing these types and functions with new ones that are compatible with our new, strongly-typed config.
2 changes: 1 addition & 1 deletion e2e/packages/sync-test/data/encodeTestData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mapObject } from "@latticexyz/utils";
import { encodeKey, encodeValueArgs, valueSchemaToFieldLayoutHex } from "@latticexyz/protocol-parser";
import { encodeKey, encodeValueArgs, valueSchemaToFieldLayoutHex } from "@latticexyz/protocol-parser/internal";
import { Data, EncodedData } from "./types";
import config from "../../contracts/mud.config";

Expand Down
2 changes: 1 addition & 1 deletion e2e/packages/sync-test/data/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Note: this expects the config to not use shortcuts but the full definitions for table schemas
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType } from "@latticexyz/schema-type";
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType } from "@latticexyz/schema-type/internal";
import config from "../../contracts/mud.config";
import { Hex } from "viem";
type SchemaToPrimitive<Schema> =
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/deploy/configToTables.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resourceToHex } from "@latticexyz/common";
import { KeySchema, ValueSchema } from "@latticexyz/protocol-parser";
import { SchemaAbiType, StaticAbiType } from "@latticexyz/schema-type";
import { KeySchema, ValueSchema } from "@latticexyz/protocol-parser/internal";
import { SchemaAbiType, StaticAbiType } from "@latticexyz/schema-type/internal";
import { StoreConfig, resolveUserTypes } from "@latticexyz/store";
import { Hex } from "viem";

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/deploy/ensureTables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Client, Transport, Chain, Account, Hex } from "viem";
import { Table } from "./configToTables";
import { resourceToLabel, writeContract } from "@latticexyz/common";
import { WorldDeploy, worldAbi } from "./common";
import { valueSchemaToFieldLayoutHex, keySchemaToHex, valueSchemaToHex } from "@latticexyz/protocol-parser";
import { valueSchemaToFieldLayoutHex, keySchemaToHex, valueSchemaToHex } from "@latticexyz/protocol-parser/internal";
import { debug } from "./debug";
import { getTables } from "./getTables";
import pRetry from "p-retry";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/deploy/getFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { WorldDeploy, WorldFunction, worldTables } from "./common";
import { debug } from "./debug";
import { storeSetRecordEvent } from "@latticexyz/store";
import { getLogs } from "viem/actions";
import { decodeValueArgs } from "@latticexyz/protocol-parser";
import { decodeValueArgs } from "@latticexyz/protocol-parser/internal";
import { getTableValue } from "./getTableValue";
import { hexToResource } from "@latticexyz/common";

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/deploy/getResourceAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { WorldDeploy, worldTables } from "./common";
import { debug } from "./debug";
import { storeSpliceStaticDataEvent } from "@latticexyz/store";
import { getLogs } from "viem/actions";
import { decodeKey } from "@latticexyz/protocol-parser";
import { decodeKey } from "@latticexyz/protocol-parser/internal";
import { getTableValue } from "./getTableValue";

export async function getResourceAccess({
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/deploy/getTableValue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SchemaToPrimitives, decodeValueArgs, encodeKey } from "@latticexyz/protocol-parser";
import { SchemaToPrimitives, decodeValueArgs, encodeKey } from "@latticexyz/protocol-parser/internal";
import { WorldDeploy, worldAbi } from "./common";
import { Client } from "viem";
import { readContract } from "viem/actions";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/deploy/getTables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { WorldDeploy, storeTables } from "./common";
import { debug } from "./debug";
import { storeSetRecordEvent } from "@latticexyz/store";
import { getLogs } from "viem/actions";
import { KeySchema, ValueSchema, decodeKey, decodeValueArgs, hexToSchema } from "@latticexyz/protocol-parser";
import { KeySchema, ValueSchema, decodeKey, decodeValueArgs, hexToSchema } from "@latticexyz/protocol-parser/internal";

export async function getTables({
client,
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/deploy/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { resolveWorldConfig } from "@latticexyz/world";
import { Config, ConfigInput, Library, Module, System, WorldFunction } from "./common";
import { resourceToHex } from "@latticexyz/common";
import { resolveWithContext } from "@latticexyz/config/library";
import { encodeField } from "@latticexyz/protocol-parser";
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType } from "@latticexyz/schema-type";
import { encodeField } from "@latticexyz/protocol-parser/internal";
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType } from "@latticexyz/schema-type/internal";
import { Hex, hexToBytes, bytesToHex, toFunctionSelector, toFunctionSignature } from "viem";
import { getExistingContracts } from "../utils/getExistingContracts";
import { defaultModuleContracts } from "../utils/defaultModuleContracts";
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/codegen/utils/loadUserTypesFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFileSync } from "fs";
import path from "path";
import { SolidityUserDefinedType, extractUserTypes } from "./extractUserTypes";
import { MUDError } from "../../errors";
import { SchemaAbiType } from "@latticexyz/schema-type";
import { SchemaAbiType } from "@latticexyz/schema-type/internal";

export type UserType = {
filePath: string;
Expand Down
6 changes: 5 additions & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
"type": "module",
"exports": {
".": "./dist/index.js",
"./internal": "./dist/internal.js",
"./library": "./dist/deprecated/library.js",
"./register": "./dist/deprecated/register.js",
"./node": "./dist/deprecated/node.js"
},
"typesVersions": {
"*": {
"index": [
"./src/index.ts"
"./src/exports/index.ts"
],
"internal": [
"./src/exports/internal.ts"
],
"library": [
"./src/deprecated/library/index.ts"
Expand Down
2 changes: 1 addition & 1 deletion packages/config/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Hex } from "viem";
import { DynamicAbiType, StaticAbiType } from "@latticexyz/schema-type";
import { DynamicAbiType, StaticAbiType } from "@latticexyz/schema-type/internal";
import { ResourceType } from "@latticexyz/common";
import { satisfy } from "@latticexyz/common/type-utils";

Expand Down
7 changes: 7 additions & 0 deletions packages/config/src/exports/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* External exports.
*
* Be sure you're ready to commit to these being supported and changes made backward compatible!
*/

export type { AbiType, StaticAbiType, DynamicAbiType, Schema, Table } from "../common";
Empty file.
1 change: 0 additions & 1 deletion packages/config/src/index.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/config/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { defineConfig } from "tsup";

export default defineConfig({
entry: {
index: "src/index.ts",
index: "src/exports/index.ts",
internal: "src/exports/internal.ts",
"deprecated/library": "src/deprecated/library/index.ts",
"deprecated/register": "src/deprecated/register/index.ts",
"deprecated/node": "src/deprecated/node/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-tools/src/zustand/FieldValue.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType } from "@latticexyz/schema-type";
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType } from "@latticexyz/schema-type/internal";
import { isHex } from "viem";
import { TruncatedHex } from "../TruncatedHex";

Expand Down
14 changes: 12 additions & 2 deletions packages/protocol-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,19 @@
"license": "MIT",
"type": "module",
"exports": {
".": "./dist/index.js"
".": "./dist/index.js",
"./internal": "./dist/internal.js"
},
"typesVersions": {
"*": {
"index": [
"./src/exports/index.ts"
],
"internal": [
"./src/exports/internal.ts"
]
}
},
"types": "src/index.ts",
"scripts": {
"build": "pnpm run build:js",
"build:js": "tsup",
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/abiTypesToSchema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DynamicAbiType, SchemaAbiType, StaticAbiType, isDynamicAbiType } from "@latticexyz/schema-type";
import { DynamicAbiType, SchemaAbiType, StaticAbiType, isDynamicAbiType } from "@latticexyz/schema-type/internal";
import { Schema } from "./common";

export function abiTypesToSchema(abiTypes: SchemaAbiType[]): Schema {
Expand Down
7 changes: 6 additions & 1 deletion packages/protocol-parser/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { DynamicAbiType, SchemaAbiType, SchemaAbiTypeToPrimitiveType, StaticAbiType } from "@latticexyz/schema-type";
import {
DynamicAbiType,
SchemaAbiType,
SchemaAbiTypeToPrimitiveType,
StaticAbiType,
} from "@latticexyz/schema-type/internal";
import { Hex } from "viem";

/** @deprecated use `KeySchema` or `ValueSchema` instead */
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/decodeDynamicField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
DynamicAbiTypeToPrimitiveType,
arrayAbiTypeToStaticAbiType,
staticAbiTypeToByteLength,
} from "@latticexyz/schema-type";
} from "@latticexyz/schema-type/internal";
import { decodeStaticField } from "./decodeStaticField";
import { InvalidHexLengthError, InvalidHexLengthForArrayFieldError } from "./errors";

Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/decodeField.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Hex } from "viem";
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType, isDynamicAbiType } from "@latticexyz/schema-type";
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType, isDynamicAbiType } from "@latticexyz/schema-type/internal";
import { decodeDynamicField } from "./decodeDynamicField";
import { decodeStaticField } from "./decodeStaticField";

Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/decodeKeyTuple.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Hex, decodeAbiParameters } from "viem";
import { StaticPrimitiveType } from "@latticexyz/schema-type";
import { StaticPrimitiveType } from "@latticexyz/schema-type/internal";
import { Schema } from "./common";

// key tuples are encoded in the same way as abi.encode, so we can decode them with viem
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/decodeRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
DynamicPrimitiveType,
staticAbiTypeToByteLength,
dynamicAbiTypeToDefaultValue,
} from "@latticexyz/schema-type";
} from "@latticexyz/schema-type/internal";
import { Hex } from "viem";
import { Schema } from "./common";
import { decodeDynamicField } from "./decodeDynamicField";
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/decodeStaticField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
StaticAbiTypeToPrimitiveType,
staticAbiTypeToByteLength,
staticAbiTypeToDefaultValue,
} from "@latticexyz/schema-type";
} from "@latticexyz/schema-type/internal";
import { InvalidHexLengthError, InvalidHexLengthForStaticFieldError } from "./errors";

export function decodeStaticField<
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/decodeValue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isStaticAbiType, isDynamicAbiType } from "@latticexyz/schema-type";
import { isStaticAbiType, isDynamicAbiType } from "@latticexyz/schema-type/internal";
import { Hex } from "viem";
import { SchemaToPrimitives, ValueSchema } from "./common";
import { decodeRecord } from "./decodeRecord";
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/decodeValueArgs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { concatHex } from "viem";
import { isStaticAbiType } from "@latticexyz/schema-type";
import { isStaticAbiType } from "@latticexyz/schema-type/internal";
import { SchemaToPrimitives, ValueArgs, ValueSchema } from "./common";
import { decodeValue } from "./decodeValue";
import { staticDataLength } from "./staticDataLength";
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/encodeField.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SchemaAbiType, arrayAbiTypeToStaticAbiType, isArrayAbiType } from "@latticexyz/schema-type";
import { SchemaAbiType, arrayAbiTypeToStaticAbiType, isArrayAbiType } from "@latticexyz/schema-type/internal";
import { AbiParameterToPrimitiveType } from "abitype";
import { Hex, encodePacked } from "viem";

Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/encodeKey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isStaticAbiType } from "@latticexyz/schema-type";
import { isStaticAbiType } from "@latticexyz/schema-type/internal";
import { Hex } from "viem";
import { SchemaToPrimitives, KeySchema } from "./common";
import { encodeKeyTuple } from "./encodeKeyTuple";
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/encodeKeyTuple.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StaticPrimitiveType } from "@latticexyz/schema-type";
import { StaticPrimitiveType } from "@latticexyz/schema-type/internal";
import { Hex, encodeAbiParameters } from "viem";
import { Schema } from "./common";

Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/encodeRecord.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StaticPrimitiveType, DynamicPrimitiveType } from "@latticexyz/schema-type";
import { StaticPrimitiveType, DynamicPrimitiveType } from "@latticexyz/schema-type/internal";
import { Hex } from "viem";
import { encodeField } from "./encodeField";
import { Schema } from "./common";
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/encodeValueArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
isDynamicAbiType,
StaticAbiType,
DynamicAbiType,
} from "@latticexyz/schema-type";
} from "@latticexyz/schema-type/internal";
import { concatHex } from "viem";
import { encodeField } from "./encodeField";
import { SchemaToPrimitives, ValueArgs, ValueSchema } from "./common";
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Hex } from "viem";
import { MUDError } from "@latticexyz/common/errors";
import { StaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type";
import { StaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type/internal";

export class InvalidHexLengthError extends MUDError {
override name = "InvalidHexValueError";
Expand Down
5 changes: 5 additions & 0 deletions packages/protocol-parser/src/exports/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* External exports.
*
* Be sure you're ready to commit to these being supported and changes made backward compatible!
*/
27 changes: 27 additions & 0 deletions packages/protocol-parser/src/exports/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export * from "../abiTypesToSchema";
export * from "../common";
export * from "../decodeDynamicField";
export * from "../decodeField";
export * from "../decodeKey";
export * from "../decodeKeyTuple";
export * from "../decodeRecord";
export * from "../decodeStaticField";
export * from "../decodeValue";
export * from "../decodeValueArgs";
export * from "../encodeField";
export * from "../encodeKey";
export * from "../encodeKeyTuple";
export * from "../encodeRecord";
export * from "../encodeValue";
export * from "../encodeValueArgs";
export * from "../errors";
export * from "../fieldLayoutToHex";
export * from "../hexToPackedCounter";
export * from "../hexToSchema";
export * from "../hexToTableSchema";
export * from "../keySchemaToHex";
export * from "../schemaIndexToAbiType";
export * from "../schemaToHex";
export * from "../staticDataLength";
export * from "../valueSchemaToFieldLayoutHex";
export * from "../valueSchemaToHex";
7 changes: 6 additions & 1 deletion packages/protocol-parser/src/hexToSchema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { StaticAbiType, DynamicAbiType, schemaAbiTypes, staticAbiTypeToByteLength } from "@latticexyz/schema-type";
import {
StaticAbiType,
DynamicAbiType,
schemaAbiTypes,
staticAbiTypeToByteLength,
} from "@latticexyz/schema-type/internal";
import { Hex, hexToNumber, sliceHex } from "viem";
import { Schema } from "./common";
import { InvalidHexLengthForSchemaError, SchemaStaticLengthMismatchError } from "./errors";
Expand Down
27 changes: 0 additions & 27 deletions packages/protocol-parser/src/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/protocol-parser/src/keySchemaToHex.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isStaticAbiType } from "@latticexyz/schema-type";
import { isStaticAbiType } from "@latticexyz/schema-type/internal";
import { Hex } from "viem";
import { KeySchema } from "./common";
import { schemaToHex } from "./schemaToHex";
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/schemaIndexToAbiType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SchemaAbiType } from "@latticexyz/schema-type";
import { SchemaAbiType } from "@latticexyz/schema-type/internal";
import { Schema } from "./common";

export function schemaIndexToAbiType(schema: Schema, schemaIndex: number): SchemaAbiType {
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/schemaToHex.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { schemaAbiTypes } from "@latticexyz/schema-type";
import { schemaAbiTypes } from "@latticexyz/schema-type/internal";
import { Hex } from "viem";
import { Schema } from "./common";
import { staticDataLength } from "./staticDataLength";
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/staticDataLength.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type";
import { StaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type/internal";

export function staticDataLength(staticFields: readonly StaticAbiType[]): number {
return staticFields.reduce((length, fieldType) => length + staticAbiTypeToByteLength[fieldType], 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Hex } from "viem";
import { ValueSchema } from "./common";
import { isDynamicAbiType, isStaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type";
import { isDynamicAbiType, isStaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type/internal";

// TODO: add tests once we have corresponding tests for FieldLayout.sol (bytes32 -> FieldLayout and vice versa)
export function valueSchemaToFieldLayoutHex(valueSchema: ValueSchema): Hex {
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-parser/src/valueSchemaToHex.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDynamicAbiType, isStaticAbiType } from "@latticexyz/schema-type";
import { isDynamicAbiType, isStaticAbiType } from "@latticexyz/schema-type/internal";
import { Hex } from "viem";
import { ValueSchema } from "./common";
import { schemaToHex } from "./schemaToHex";
Expand Down
Loading

0 comments on commit b38c096

Please sign in to comment.