File size: 11,422 Bytes
9afbea6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { Platform } from './models/Platform';
import { WorkItem } from './models/WorkItem';

export class TeaQLNotLoadedError extends Error {
    readonly details: Record<string, unknown>;

    constructor(
        readonly root: string,
        readonly accessPath: string,
        readonly breakPoint: string,
    ) {
        const suggestedFix = `select${breakPoint.charAt(0).toUpperCase()}${breakPoint.slice(1)}(...)`;
        const details = {
            error: 'TeaQLNotLoadedError',
            root,
            accessPath: accessPath.split('.'),
            breakPoint,
            missingPreload: [breakPoint],
            suggestedFix,
            severity: 'error',
            humanMessage: `访问 ${root}.${accessPath} 时缺少预加载。请在查询中加入 ${suggestedFix}`,
        };
        super(JSON.stringify(details));
        this.name = 'TeaQLNotLoadedError';
        this.details = details;
    }
}

function expressionPath(prefix: string, field: string): string {
    return prefix ? `${prefix}.${field}` : field;
}

export class ValueExpression<T> {
    constructor(
        private readonly value: T | null | undefined,
        private readonly present = true,
        private readonly notLoaded?: TeaQLNotLoadedError,
    ) {}

    static missing<T>(): ValueExpression<T> {
        return new ValueExpression<T>(undefined, false);
    }

    static notLoaded<T>(error: TeaQLNotLoadedError): ValueExpression<T> {
        return new ValueExpression<T>(undefined, false, error);
    }

    eval(): T | null | undefined {
        if (this.notLoaded) throw this.notLoaded;
        return this.present ? this.value : undefined;
    }

    isPresent(): boolean {
        if (this.notLoaded) throw this.notLoaded;
        return this.present;
    }

    orElse(fallback: T): T {
        const value = this.eval();
        return this.present && value != null ? value : fallback;
    }
}

export class PlatformExpression {
    constructor(
        private readonly value: Platform | null | undefined,
        private readonly root = 'Platform(null)',
        private readonly path = '',
        private readonly notLoaded?: TeaQLNotLoadedError,
    ) {}

    eval(): Platform | null | undefined {
        if (this.notLoaded) throw this.notLoaded;
        return this.value;
    }

    id(): ValueExpression<string> {
        if (this.notLoaded) return ValueExpression.notLoaded<string>(this.notLoaded);
        if (!this.value) return ValueExpression.missing<string>();
        const path = expressionPath(this.path, 'id');
        if (!this.value.isLoaded('id')) {
            return ValueExpression.notLoaded<string>(
                new TeaQLNotLoadedError(this.root, path, 'id'),
            );
        }
        return new ValueExpression<string>(this.value.id);
    }

    name(): ValueExpression<string> {
        if (this.notLoaded) return ValueExpression.notLoaded<string>(this.notLoaded);
        if (!this.value) return ValueExpression.missing<string>();
        const path = expressionPath(this.path, 'name');
        if (!this.value.isLoaded('name')) {
            return ValueExpression.notLoaded<string>(
                new TeaQLNotLoadedError(this.root, path, 'name'),
            );
        }
        return new ValueExpression<string>(this.value.name);
    }

    version(): ValueExpression<number> {
        if (this.notLoaded) return ValueExpression.notLoaded<number>(this.notLoaded);
        if (!this.value) return ValueExpression.missing<number>();
        const path = expressionPath(this.path, 'version');
        if (!this.value.isLoaded('version')) {
            return ValueExpression.notLoaded<number>(
                new TeaQLNotLoadedError(this.root, path, 'version'),
            );
        }
        return new ValueExpression<number>(this.value.version);
    }



    workItemList(): WorkItemListExpression {
        const path = expressionPath(this.path, 'workItemList');
        if (this.notLoaded) {
            return new WorkItemListExpression([], this.root, path, false, this.notLoaded);
        }
        if (!this.value) return WorkItemListExpression.missing(this.root, path);
        if (!this.value.isLoaded('workItemList')) {
            return new WorkItemListExpression([], this.root, path, false,
                new TeaQLNotLoadedError(this.root, path, 'workItemList'));
        }
        return new WorkItemListExpression(
            this.value.workItemList(), this.root, path,
        );
    }
}

export class WorkItemExpression {
    constructor(
        private readonly value: WorkItem | null | undefined,
        private readonly root = 'WorkItem(null)',
        private readonly path = '',
        private readonly notLoaded?: TeaQLNotLoadedError,
    ) {}

    eval(): WorkItem | null | undefined {
        if (this.notLoaded) throw this.notLoaded;
        return this.value;
    }

    id(): ValueExpression<string> {
        if (this.notLoaded) return ValueExpression.notLoaded<string>(this.notLoaded);
        if (!this.value) return ValueExpression.missing<string>();
        const path = expressionPath(this.path, 'id');
        if (!this.value.isLoaded('id')) {
            return ValueExpression.notLoaded<string>(
                new TeaQLNotLoadedError(this.root, path, 'id'),
            );
        }
        return new ValueExpression<string>(this.value.id);
    }

    title(): ValueExpression<string> {
        if (this.notLoaded) return ValueExpression.notLoaded<string>(this.notLoaded);
        if (!this.value) return ValueExpression.missing<string>();
        const path = expressionPath(this.path, 'title');
        if (!this.value.isLoaded('title')) {
            return ValueExpression.notLoaded<string>(
                new TeaQLNotLoadedError(this.root, path, 'title'),
            );
        }
        return new ValueExpression<string>(this.value.title);
    }

    description(): ValueExpression<string> {
        if (this.notLoaded) return ValueExpression.notLoaded<string>(this.notLoaded);
        if (!this.value) return ValueExpression.missing<string>();
        const path = expressionPath(this.path, 'description');
        if (!this.value.isLoaded('description')) {
            return ValueExpression.notLoaded<string>(
                new TeaQLNotLoadedError(this.root, path, 'description'),
            );
        }
        return new ValueExpression<string>(this.value.description);
    }

    version(): ValueExpression<number> {
        if (this.notLoaded) return ValueExpression.notLoaded<number>(this.notLoaded);
        if (!this.value) return ValueExpression.missing<number>();
        const path = expressionPath(this.path, 'version');
        if (!this.value.isLoaded('version')) {
            return ValueExpression.notLoaded<number>(
                new TeaQLNotLoadedError(this.root, path, 'version'),
            );
        }
        return new ValueExpression<number>(this.value.version);
    }

    platformId(): ValueExpression<string | number> {
        if (this.notLoaded) return ValueExpression.notLoaded<string | number>(this.notLoaded);
        if (!this.value) return ValueExpression.missing<string | number>();
        const path = expressionPath(this.path, 'platform');
        if (!this.value.isLoaded('platform')) {
            return ValueExpression.notLoaded<string | number>(
                new TeaQLNotLoadedError(this.root, path, 'platform'),
            );
        }
        const reference = this.value.platform;
        return new ValueExpression<string | number>(
            reference instanceof Platform
                ? reference.id : reference as string | number | undefined,
        );
    }

    platform(): PlatformExpression {
        const path = expressionPath(this.path, 'platform');
        if (this.notLoaded) {
            return new PlatformExpression(undefined, this.root, path, this.notLoaded);
        }
        if (!this.value) return new PlatformExpression(undefined, this.root, path);
        if (!this.value.isLoaded('platform')) {
            return new PlatformExpression(undefined, this.root, path,
                new TeaQLNotLoadedError(this.root, path, 'platform'));
        }
        const relation = this.value.platform;
        const target = relation == null
            ? undefined
            : relation instanceof Platform
                ? relation
                : Platform.fromRecord({ id: relation });
        return new PlatformExpression(target, this.root, path);
    }

}

export class PlatformListExpression {
    constructor(
        private readonly items: readonly Platform[],
        private readonly root = 'Platform(null)',
        private readonly path = '',
        private readonly present = true,
        private readonly notLoaded?: TeaQLNotLoadedError,
    ) {}

    static missing(root?: string, path = ''): PlatformListExpression {
        return new PlatformListExpression([], root, path, false);
    }

    size(): ValueExpression<number> {
        if (this.notLoaded) return ValueExpression.notLoaded<number>(this.notLoaded);
        return this.present
            ? new ValueExpression<number>(this.items.length)
            : ValueExpression.missing<number>();
    }

    first(): PlatformExpression {
        return this.get(0);
    }

    get(index: number): PlatformExpression {
        const itemPath = expressionPath(this.path, `get(${index})`);
        if (this.notLoaded) {
            return new PlatformExpression(undefined, this.root, itemPath, this.notLoaded);
        }
        return !this.present || index < 0 || index >= this.items.length
            ? new PlatformExpression(undefined, this.root, itemPath)
            : new PlatformExpression(this.items[index], this.root, itemPath);
    }
}

export class WorkItemListExpression {
    constructor(
        private readonly items: readonly WorkItem[],
        private readonly root = 'WorkItem(null)',
        private readonly path = '',
        private readonly present = true,
        private readonly notLoaded?: TeaQLNotLoadedError,
    ) {}

    static missing(root?: string, path = ''): WorkItemListExpression {
        return new WorkItemListExpression([], root, path, false);
    }

    size(): ValueExpression<number> {
        if (this.notLoaded) return ValueExpression.notLoaded<number>(this.notLoaded);
        return this.present
            ? new ValueExpression<number>(this.items.length)
            : ValueExpression.missing<number>();
    }

    first(): WorkItemExpression {
        return this.get(0);
    }

    get(index: number): WorkItemExpression {
        const itemPath = expressionPath(this.path, `get(${index})`);
        if (this.notLoaded) {
            return new WorkItemExpression(undefined, this.root, itemPath, this.notLoaded);
        }
        return !this.present || index < 0 || index >= this.items.length
            ? new WorkItemExpression(undefined, this.root, itemPath)
            : new WorkItemExpression(this.items[index], this.root, itemPath);
    }
}

export class E {
    static platform(
        value: Platform | null | undefined,
    ): PlatformExpression {
        return new PlatformExpression(
            value, `Platform(id=${value?.id ?? 'null'})`,
        );
    }

    static workItem(
        value: WorkItem | null | undefined,
    ): WorkItemExpression {
        return new WorkItemExpression(
            value, `WorkItem(id=${value?.id ?? 'null'})`,
        );
    }
}