1
- import type { Type } from "@typespec/compiler" ;
1
+ import type { Entity , Type } from "@typespec/compiler" ;
2
2
3
- export type EntityPropertyConfig = "parent" | "nested" | "ref" | "value" | "skip" ;
3
+ export type PropertyRendering < T > =
4
+ | { kind : "parent" | "nested-items" | "ref" | "value" | "skip" }
5
+ | NestedPropertyRendering < T > ;
6
+ export type PropertyRenderingRaw < T > =
7
+ | "parent"
8
+ | "nested-items"
9
+ | "ref"
10
+ | "value"
11
+ | "skip"
12
+ | NestedPropertyRenderingRaw < T > ;
4
13
5
- export const TypeConfig : TypeGraphConfig = {
14
+ export type PropertiesRenderingRaw < T > = { [ K in keyof T ] : PropertyRenderingRaw < T [ K ] > } ;
15
+ export type PropertiesRendering < T > = { [ K in keyof T ] : PropertyRendering < T [ K ] > } ;
16
+
17
+ export type NestedPropertyRendering < T > = {
18
+ kind : "nested" ;
19
+ properties : PropertiesRendering < T > ;
20
+ } ;
21
+ export type NestedPropertyRenderingRaw < T > = {
22
+ kind : "nested" ;
23
+ properties : PropertiesRenderingRaw < T > ;
24
+ } ;
25
+
26
+ export const CommonPropsConfig = {
27
+ namespace : "parent" ,
28
+ name : "value" ,
29
+ } ;
30
+
31
+ const HiddenProps = [
32
+ "entityKind" ,
33
+ "kind" ,
34
+ "node" ,
35
+ "symbol" ,
36
+ "templateNode" ,
37
+ "templateArguments" ,
38
+ "templateMapper" ,
39
+ "instantiationParameters" ,
40
+ "decorators" ,
41
+ "isFinished" ,
42
+ ] as const ;
43
+
44
+ const HiddenPropsConfig = Object . fromEntries ( HiddenProps . map ( ( prop ) => [ prop , "skip" ] ) ) as Record <
45
+ HiddenPropsType ,
46
+ "skip"
47
+ > ;
48
+
49
+ export const TypeConfig : TypeGraphConfig = buildConfig ( {
6
50
Namespace : {
7
51
namespaces : "skip" ,
8
- models : "nested" ,
9
- scalars : "nested" ,
10
- interfaces : "nested" ,
11
- operations : "nested" ,
12
- unions : "nested" ,
13
- enums : "nested" ,
14
- decoratorDeclarations : "nested" ,
52
+ models : "nested-items " ,
53
+ scalars : "nested-items " ,
54
+ interfaces : "nested-items " ,
55
+ operations : "nested-items " ,
56
+ unions : "nested-items " ,
57
+ enums : "nested-items " ,
58
+ decoratorDeclarations : "nested-items " ,
15
59
} ,
16
60
Interface : {
17
- operations : "nested" ,
61
+ operations : "nested-items " ,
18
62
sourceInterfaces : "ref" ,
19
63
} ,
20
64
Operation : {
21
65
interface : "parent" ,
22
- parameters : "nested" ,
66
+ parameters : "nested-items " ,
23
67
returnType : "ref" ,
24
68
sourceOperation : "ref" ,
25
69
} ,
26
70
Model : {
27
- indexer : "skip" ,
71
+ indexer : {
72
+ kind : "nested" ,
73
+ properties : {
74
+ key : "ref" ,
75
+ value : "ref" ,
76
+ } ,
77
+ } ,
28
78
baseModel : "ref" ,
29
79
derivedModels : "ref" ,
30
- properties : "nested" ,
80
+ properties : "nested-items " ,
31
81
sourceModel : "ref" ,
32
82
sourceModels : "value" ,
33
83
} ,
34
84
Scalar : {
35
85
baseScalar : "ref" ,
36
86
derivedScalars : "ref" ,
37
- constructors : "nested" ,
87
+ constructors : "nested-items " ,
38
88
} ,
39
89
ModelProperty : {
40
90
model : "parent" ,
@@ -44,16 +94,16 @@ export const TypeConfig: TypeGraphConfig = {
44
94
defaultValue : "value" ,
45
95
} ,
46
96
Enum : {
47
- members : "nested" ,
97
+ members : "nested-items " ,
48
98
} ,
49
99
EnumMember : {
50
100
enum : "parent" ,
51
101
sourceMember : "ref" ,
52
102
value : "value" ,
53
103
} ,
54
104
Union : {
55
- expression : "skip " ,
56
- variants : "nested" ,
105
+ expression : "value " ,
106
+ variants : "nested-items " ,
57
107
} ,
58
108
UnionVariant : {
59
109
union : "parent" ,
@@ -63,13 +113,13 @@ export const TypeConfig: TypeGraphConfig = {
63
113
value : "value" ,
64
114
} ,
65
115
Decorator : {
66
- parameters : "nested" ,
116
+ parameters : "nested-items " ,
67
117
implementation : "skip" ,
68
118
target : "ref" ,
69
119
} ,
70
120
ScalarConstructor : {
71
121
scalar : "parent" ,
72
- parameters : "nested" ,
122
+ parameters : "nested-items " ,
73
123
} ,
74
124
FunctionParameter : null ,
75
125
Number : {
@@ -81,10 +131,10 @@ export const TypeConfig: TypeGraphConfig = {
81
131
value : "value" ,
82
132
} ,
83
133
Tuple : {
84
- values : "nested" ,
134
+ values : "nested-items " ,
85
135
} ,
86
136
StringTemplate : {
87
- spans : "nested" ,
137
+ spans : "nested-items " ,
88
138
stringValue : "value" ,
89
139
} ,
90
140
StringTemplateSpan : {
@@ -98,44 +148,63 @@ export const TypeConfig: TypeGraphConfig = {
98
148
99
149
// Don't want to expose those for now
100
150
Intrinsic : null ,
101
- } ;
151
+ } ) ;
102
152
103
- type PropsToDefine < T extends Type > = Exclude <
104
- keyof T ,
105
- HiddenPropsType | keyof typeof CommonPropsConfig
106
- > ;
107
- type TypeConfig < T extends Type > = Record < PropsToDefine < T > , EntityPropertyConfig > | null ;
153
+ type PropsToDefine < T extends Type > = Omit < T , HiddenPropsType | keyof typeof CommonPropsConfig > ;
154
+ type TypeRawConfig < T extends Type > = PropertiesRenderingRaw < PropsToDefine < T > > | null ;
155
+ type TypeGraphRawConfig = {
156
+ [ K in Type [ "kind" ] ] : TypeRawConfig < Extract < Type , { kind : K } > > ;
157
+ } ;
158
+ type TypeConfig < T extends Type > = PropertiesRendering < T > | null ;
108
159
type TypeGraphConfig = {
109
160
[ K in Type [ "kind" ] ] : TypeConfig < Extract < Type , { kind : K } > > ;
110
161
} ;
111
162
112
- export const CommonPropsConfig = {
113
- namespace : "parent" ,
114
- } ;
115
-
116
- const HiddenProps = [
117
- "entityKind" ,
118
- "kind" ,
119
- "name" ,
120
- "node" ,
121
- "symbol" ,
122
- "templateNode" ,
123
- "templateArguments" ,
124
- "templateMapper" ,
125
- "instantiationParameters" ,
126
- "decorators" ,
127
- "projector" ,
128
- "isFinished" ,
129
- ] as const ;
130
163
type HiddenPropsType = ( typeof HiddenProps ) [ number ] ;
131
164
132
165
export const HiddenPropsSet = new Set ( HiddenProps ) ;
133
166
134
167
export function getPropertyRendering < T extends Type , K extends keyof T > (
135
168
type : T ,
136
169
key : K ,
137
- ) : EntityPropertyConfig {
170
+ ) : PropertyRenderingRaw < T > {
138
171
const properties = ( TypeConfig as any ) [ type . kind ] ;
139
172
const action = properties ?. [ key ] ?? ( CommonPropsConfig as any ) [ key ] ;
140
173
return action ;
141
174
}
175
+ export function getRenderingConfig < T extends Entity > ( type : T ) : PropertiesRendering < T > | null {
176
+ return ( TypeConfig as any ) [ ( type as any ) . kind ] ;
177
+ }
178
+
179
+ function buildConfig ( raw : TypeGraphRawConfig ) : TypeGraphConfig {
180
+ return Object . fromEntries (
181
+ Object . entries ( raw ) . map ( ( [ kind , config ] ) => {
182
+ return [ kind , buildConfigForKind ( config as any ) ] ;
183
+ } ) ,
184
+ ) as any ;
185
+ }
186
+
187
+ function buildConfigForKind < T extends Type > ( config : TypeRawConfig < T > | null ) : TypeConfig < T > | null {
188
+ if ( config === null ) {
189
+ return null ;
190
+ }
191
+ return Object . fromEntries (
192
+ Object . entries ( { ...CommonPropsConfig , ...HiddenPropsConfig , ...config } ) . map (
193
+ ( [ key , value ] ) => {
194
+ return [ key , buildConfigForProperty ( value as any ) ] ;
195
+ } ,
196
+ ) ,
197
+ ) as any ;
198
+ }
199
+
200
+ function buildConfigForProperty < T extends Type > (
201
+ value : PropertyRenderingRaw < T > ,
202
+ ) : PropertyRendering < T > {
203
+ if ( typeof value === "string" ) {
204
+ return { kind : value } ;
205
+ }
206
+ return {
207
+ kind : "nested" ,
208
+ properties : buildConfigForKind ( value . properties ) as PropertiesRendering < T > ,
209
+ } ;
210
+ }
0 commit comments