forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.ts
202 lines (177 loc) · 5.76 KB
/
resource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import {
$visibility,
DecoratorContext,
getKeyName,
isErrorType,
isKey,
Model,
ModelProperty,
Program,
setTypeSpecNamespace,
Type,
validateDecoratorTarget,
} from "@typespec/compiler";
import { $path } from "@typespec/http";
import { ParentResourceDecorator } from "../generated-defs/TypeSpec.Rest.js";
import { createStateSymbol, reportDiagnostic } from "./lib.js";
export interface ResourceKey {
resourceType: Model;
keyProperty: ModelProperty;
}
const resourceKeysKey = createStateSymbol("resourceKeys");
const resourceTypeForKeyParamKey = createStateSymbol("resourceTypeForKeyParam");
export function setResourceTypeKey(
program: Program,
resourceType: Model,
keyProperty: ModelProperty
): void {
program.stateMap(resourceKeysKey).set(resourceType, {
resourceType,
keyProperty,
});
}
export function getResourceTypeKey(program: Program, resourceType: Model): ResourceKey | undefined {
// Look up the key first
let resourceKey = program.stateMap(resourceKeysKey).get(resourceType);
if (resourceKey) {
return resourceKey;
}
// Try to find it in the resource type
resourceType.properties.forEach((p: ModelProperty) => {
if (isKey(program, p)) {
if (resourceKey) {
reportDiagnostic(program, {
code: "duplicate-key",
format: {
resourceName: resourceType.name,
},
target: p,
});
} else {
resourceKey = {
resourceType,
keyProperty: p,
};
// Cache the key for future queries
setResourceTypeKey(program, resourceType, resourceKey.keyProperty);
}
}
});
// if still no key, search the base model
if (resourceKey === undefined && resourceType.baseModel !== undefined) {
resourceKey = getResourceTypeKey(program, resourceType.baseModel);
if (resourceKey !== undefined) {
// Cache the key for future queries
setResourceTypeKey(program, resourceType, resourceKey.keyProperty);
}
}
return resourceKey;
}
export function $resourceTypeForKeyParam(
context: DecoratorContext,
entity: Type,
resourceType: Type
) {
if (!validateDecoratorTarget(context, entity, "@resourceTypeForKeyParam", "ModelProperty")) {
return;
}
context.program.stateMap(resourceTypeForKeyParamKey).set(entity, resourceType);
}
setTypeSpecNamespace("Private", $resourceTypeForKeyParam);
export function getResourceTypeForKeyParam(
program: Program,
param: ModelProperty
): Model | undefined {
return program.stateMap(resourceTypeForKeyParamKey).get(param);
}
function cloneKeyProperties(context: DecoratorContext, target: Model, resourceType: Model) {
const { program } = context;
// Add parent keys first
const parentType = getParentResource(program, resourceType);
if (parentType) {
cloneKeyProperties(context, target, parentType);
}
const resourceKey = getResourceTypeKey(program, resourceType);
if (resourceKey) {
const { keyProperty } = resourceKey;
const keyName = getKeyName(program, keyProperty);
const decorators = [
// Filter out the @visibility decorator because it might affect metadata
// filtering. NOTE: Check for name equality instead of function equality
// to deal with multiple copies of core being used.
...keyProperty.decorators.filter((d) => d.decorator.name !== $visibility.name),
{
decorator: $path,
args: [],
},
{
decorator: $resourceTypeForKeyParam,
args: [{ node: target.node, value: resourceType, jsValue: resourceType }],
},
];
// Clone the key property and ensure that an optional key property doesn't
// become an optional path parameter
const newProp = program.checker.cloneType(keyProperty, {
name: keyName,
decorators,
optional: false,
model: target,
sourceProperty: undefined,
});
// Add the key property to the target type
target.properties.set(keyName, newProp);
}
}
export function $copyResourceKeyParameters(
context: DecoratorContext,
entity: Model,
filter?: string
) {
const reportNoKeyError = () =>
reportDiagnostic(context.program, {
code: "not-key-type",
target: entity,
});
const templateArguments = entity.templateMapper?.args;
if (!templateArguments || templateArguments.length !== 1) {
return reportNoKeyError();
}
if ((templateArguments[0] as any).kind !== "Model") {
if (isErrorType(templateArguments[0])) {
return;
}
return reportNoKeyError();
}
const resourceType = templateArguments[0] as Model;
if (filter === "parent") {
// Only copy keys of the parent type if there is one
const parentType = getParentResource(context.program, resourceType);
if (parentType) {
cloneKeyProperties(context, entity, parentType);
}
} else {
// Copy keys of the resource type and all parents
cloneKeyProperties(context, entity, resourceType);
}
}
const parentResourceTypesKey = createStateSymbol("parentResourceTypes");
export function getParentResource(program: Program, resourceType: Model): Model | undefined {
return program.stateMap(parentResourceTypesKey).get(resourceType);
}
/**
* `@parentResource` marks a model with a reference to its parent resource type
*
* The first argument should be a reference to a model type which will be treated as the parent
* type of the target model type. This will cause the `@key` properties of all parent types of
* the target type to show up in operations of the `Resource*<T>` interfaces defined in this library.
*
* `@parentResource` can only be applied to models.
*/
export const $parentResource: ParentResourceDecorator = (
context: DecoratorContext,
entity: Type,
parentType: Model
) => {
const { program } = context;
program.stateMap(parentResourceTypesKey).set(entity, parentType);
};