@@ -9,8 +9,10 @@ import {
9
9
SolidityUserDefinedType ,
10
10
} from "@latticexyz/common/codegen" ;
11
11
import { RenderTableOptions } from "./types" ;
12
- import { StoreConfig } from "../config" ;
13
12
import { getSchemaTypeInfo , importForAbiOrUserType , resolveAbiOrUserType } from "./userType" ;
13
+ import { Store as StoreConfig } from "../config/v2/output" ;
14
+ import { storeToV1 } from "../config/v2/compat" ;
15
+ import { getKeySchema , getValueSchema } from "@latticexyz/protocol-parser/internal" ;
14
16
15
17
export interface TableOptions {
16
18
/** Path where the file is expected to be written (relative to project root) */
@@ -28,87 +30,94 @@ export function getTableOptions(
28
30
config : StoreConfig ,
29
31
solidityUserTypes : Record < string , SolidityUserDefinedType > ,
30
32
) : TableOptions [ ] {
31
- const storeImportPath = config . storeImportPath ;
33
+ const configV1 = storeToV1 ( config ) ;
32
34
33
- const options = [ ] ;
34
- for ( const tableName of Object . keys ( config . tables ) ) {
35
- const tableData = config . tables [ tableName ] ;
35
+ const options = Object . values ( config . tables ) . map ( ( table ) : TableOptions => {
36
+ const keySchema = getKeySchema ( table ) ;
37
+ const valueSchema = getValueSchema ( table ) ;
36
38
37
39
// struct adds methods to get/set all values at once
38
- const withStruct = tableData . dataStruct ;
40
+ const withStruct = table . codegen . dataStruct ;
39
41
// operate on all fields at once; always render for offchain tables; for only 1 field keep them if struct is also kept
40
- const withRecordMethods = withStruct || tableData . offchainOnly || Object . keys ( tableData . valueSchema ) . length > 1 ;
42
+ const withRecordMethods = withStruct || table . type === "offchainTable" || Object . keys ( valueSchema ) . length > 1 ;
41
43
// field methods can include simply get/set if there's only 1 field and no record methods
42
- const withSuffixlessFieldMethods = ! withRecordMethods && Object . keys ( tableData . valueSchema ) . length === 1 ;
44
+ const withSuffixlessFieldMethods = ! withRecordMethods && Object . keys ( valueSchema ) . length === 1 ;
43
45
// list of any symbols that need to be imported
44
46
const imports : ImportDatum [ ] = [ ] ;
45
47
46
- const keyTuple = Object . keys ( tableData . keySchema ) . map ( ( name ) => {
47
- const abiOrUserType = tableData . keySchema [ name ] ;
48
- const { renderType } = resolveAbiOrUserType ( abiOrUserType , config , solidityUserTypes ) ;
48
+ const keyTuple = Object . entries ( keySchema ) . map ( ( [ name , field ] ) : RenderKeyTuple => {
49
+ const abiOrUserType = field . internalType ;
50
+ const { renderType } = resolveAbiOrUserType ( abiOrUserType , configV1 , solidityUserTypes ) ;
49
51
50
- const importDatum = importForAbiOrUserType ( abiOrUserType , tableData . directory , config , solidityUserTypes ) ;
52
+ const importDatum = importForAbiOrUserType (
53
+ abiOrUserType ,
54
+ table . codegen . outputDirectory ,
55
+ configV1 ,
56
+ solidityUserTypes ,
57
+ ) ;
51
58
if ( importDatum ) imports . push ( importDatum ) ;
52
59
53
- if ( renderType . isDynamic ) throw new Error ( `Parsing error: found dynamic key ${ name } in table ${ tableName } ` ) ;
54
-
55
- const keyTuple : RenderKeyTuple = {
60
+ return {
56
61
...renderType ,
57
62
name,
58
63
isDynamic : false ,
59
64
} ;
60
- return keyTuple ;
61
65
} ) ;
62
66
63
- const fields = Object . keys ( tableData . valueSchema ) . map ( ( name ) => {
64
- const abiOrUserType = tableData . valueSchema [ name ] ;
65
- const { renderType, schemaType } = resolveAbiOrUserType ( abiOrUserType , config , solidityUserTypes ) ;
67
+ const fields = Object . entries ( valueSchema ) . map ( ( [ name , field ] ) : RenderField => {
68
+ const abiOrUserType = field . internalType ;
69
+ const { renderType, schemaType } = resolveAbiOrUserType ( abiOrUserType , configV1 , solidityUserTypes ) ;
66
70
67
- const importDatum = importForAbiOrUserType ( abiOrUserType , tableData . directory , config , solidityUserTypes ) ;
71
+ const importDatum = importForAbiOrUserType (
72
+ abiOrUserType ,
73
+ table . codegen . outputDirectory ,
74
+ configV1 ,
75
+ solidityUserTypes ,
76
+ ) ;
68
77
if ( importDatum ) imports . push ( importDatum ) ;
69
78
70
79
const elementType = SchemaTypeArrayToElement [ schemaType ] ;
71
- const field : RenderField = {
80
+ return {
72
81
...renderType ,
73
82
arrayElement : elementType !== undefined ? getSchemaTypeInfo ( elementType ) : undefined ,
74
83
name,
75
84
} ;
76
- return field ;
77
85
} ) ;
78
86
79
87
const staticFields = fields . filter ( ( { isDynamic } ) => ! isDynamic ) as RenderStaticField [ ] ;
80
88
const dynamicFields = fields . filter ( ( { isDynamic } ) => isDynamic ) as RenderDynamicField [ ] ;
81
89
82
90
// With tableIdArgument: tableId is a dynamic argument for each method
83
91
// Without tableIdArgument: tableId is a file-level constant generated from `staticResourceData`
84
- const staticResourceData = tableData . tableIdArgument
92
+ const staticResourceData = table . codegen . tableIdArgument
85
93
? undefined
86
94
: {
87
- namespace : config . namespace ,
88
- name : tableData . name ,
89
- offchainOnly : tableData . offchainOnly ,
95
+ namespace : table . namespace ,
96
+ name : table . name ,
97
+ offchainOnly : table . type === "offchainTable" ,
90
98
} ;
91
99
92
- options . push ( {
93
- outputPath : path . join ( tableData . directory , `${ tableName } .sol` ) ,
94
- tableName,
100
+ return {
101
+ outputPath : path . join ( table . codegen . outputDirectory , `${ table . name } .sol` ) ,
102
+ tableName : table . name ,
95
103
renderOptions : {
96
104
imports,
97
- libraryName : tableName ,
98
- structName : withStruct ? tableName + "Data" : undefined ,
105
+ libraryName : table . name ,
106
+ structName : withStruct ? table . name + "Data" : undefined ,
99
107
staticResourceData,
100
- storeImportPath,
108
+ storeImportPath : config . codegen . storeImportPath ,
101
109
keyTuple,
102
110
fields,
103
111
staticFields,
104
112
dynamicFields,
105
- withGetters : ! tableData . offchainOnly ,
113
+ withGetters : table . type === "table" ,
106
114
withRecordMethods,
107
- withDynamicFieldMethods : ! tableData . offchainOnly ,
115
+ withDynamicFieldMethods : table . type === "table" ,
108
116
withSuffixlessFieldMethods,
109
- storeArgument : tableData . storeArgument ,
117
+ storeArgument : table . codegen . storeArgument ,
110
118
} ,
111
- } ) ;
112
- }
119
+ } ;
120
+ } ) ;
121
+
113
122
return options ;
114
123
}
0 commit comments