-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathTestModels.ts
243 lines (193 loc) · 5.75 KB
/
TestModels.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
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
/* eslint prefer-arrow-callback: [ "error", { "allowUnboundThis": true } ] */
/* eslint-disable @typescript-eslint/no-invalid-this */
/* eslint-disable @typescript-eslint/no-unused-expressions */
/* eslint-disable no-console */
/* eslint-disable no-restricted-syntax */
import {
BooleanModel,
NumberModel,
ObjectModel,
StringModel,
getValue,
m,
toObject,
type IModel,
_value,
type ModelMetadata,
type ModelOwner,
_name,
_owner,
detachedModelOwner,
_key,
_members,
_meta,
_optional,
type TypeModel,
} from '../src';
interface Named {
name: string;
}
interface Address extends Named {
street: string;
premise: string;
city: string;
postalCode: string;
country: string;
}
interface Customer extends Named {
name: string;
subscriptionActive: boolean;
subscriptionTier: number;
address: Address;
}
interface OrderRow {
product: string;
qty: number;
}
interface Order {
notes?: string;
discountCodes: string[];
rows: OrderRow[];
customer?: Customer;
address: Address | undefined;
}
enum AccountType {
Active = 'Active',
Suspended = 'Suspended',
}
interface Account extends Named {
type: AccountType;
}
interface FooItem {
foo: string;
}
interface BarItem {
bar: number;
}
interface Container {
item: BarItem | FooItem;
}
interface Employee extends Named {
supervisor?: Employee;
}
interface Commentable {
comments?: string[];
}
// Class based models with static properties
/*
type ModelClass<T = unknown> = IModel<T> & {
new(...rest: any[]): {};
};
type ModelClassWithProperty<M extends ModelClass, K extends keyof any, V = unknown> = M & Readonly<Record<K, V>>;
function staticGetterMixin<M extends ModelClass, K extends keyof any, V = unknown>(superClass: ModelClass, key: K, valueGetter: (this: M) => V): ModelClassWithProperty<M, K, V> {
return class extends superClass {
static get[key](): V {
return valueGetter.call(this as M);
}
} as ModelClassWithProperty<M, K, V>;
}
class AModel {
static readonly [_name]: string = '';
static readonly [_owner]: IModel<unknown> | ModelOwner = detachedModelOwner;
static readonly [_key]: keyof any = 'model'
static readonly [_value]: unknown = undefined;
static readonly [_meta]: ModelMetadata = {};
static readonly [_optional]: boolean = false;
}
class StringClassModel extends staticGetterMixin(AModel, _value, () => '') {}
class NamedClassModel extends staticGetterMixin(AModel, _value, () => ({} as Named)) {
static override readonly name = StringClassModel;
}
class EmployeeClassModel extends NamedClassModel {
static readonly supervisor = EmployeeClassModel;
}
*/
// Simple
const NamedModel = m
.object<Named>('Named')
.meta({
jvmType: 'com.example.application.Named',
})
.property('name', StringModel)
.build();
// Inheritance
const AddressModel = m
.extend(NamedModel)
.object<Address>('Address')
.property('street', StringModel)
.property('premise', StringModel)
.property('city', StringModel)
.property('postalCode', StringModel)
.property('country', StringModel)
.build();
const address = getValue(AddressModel);
getValue(AddressModel).postalCode;
getValue(AddressModel.postalCode);
getValue(AddressModel.country).length;
// Composition
const CustomerModel = m
.extend(NamedModel)
.object<Customer>('Customer')
.property('subscriptionActive', BooleanModel)
.property('subscriptionTier', NumberModel)
.property('address', AddressModel)
.build();
// Self reference
const EmployeeModel = m.extend(NamedModel).object<Employee>('Employee').property('supervisor', m.optional).build();
EmployeeModel.supervisor.supervisor.supervisor.name;
// Array
const OrderRowModel = m
.object<OrderRow>('OrderRow')
.property('product', StringModel)
.property('qty', NumberModel)
.build();
const OrderModel = m
.object<Order>('Order')
.property('notes', m.optional(StringModel))
.property('discountCodes', m.array(StringModel))
.property('rows', m.array(OrderRowModel))
.property('customer', m.optional(CustomerModel))
.property('address', m.optional(AddressModel))
.build();
const CommentableModel: TypeModel<Commentable> = m
.object<Commentable>('Commentable')
.property('comments', () => {
const array = m.array(StringModel);
const optional = m.optional(array);
return optional;
})
.build();
// Enum
const AccountModel = m
.extend(NamedModel)
.object<Account>('Account')
.property('type', m.enum(AccountType, 'Account'))
.build();
const t: AccountType = AccountModel.type[_value];
// Union
const FooItemModel = m.object<FooItem>('FooItem').property('foo', StringModel).build();
const BarItemModel = m.object<BarItem>('BarItem').property('bar', NumberModel).build();
const ContainerModel = m.object<Container>('Container').property('item', m.union(FooItemModel, BarItemModel)).build();
const containerItem: BarItem | FooItem = ContainerModel.item[_value];
console.log('\n\nCustomerModel has:\n');
for (const key in CustomerModel) {
const value: IModel = (CustomerModel as unknown as Record<typeof key, IModel>)[key];
console.log(key, '\n ', String(value));
}
console.log('\n\nCustomerModel.address has:\n');
for (const key in CustomerModel.address) {
const value: IModel = (CustomerModel.address as unknown as Record<typeof key, IModel>)[key];
console.log(key, '\n ', String(value));
}
console.log('\n');
console.log('Empty value for CustomerModel:', CustomerModel[_value]);
console.log('\n');
console.log('AddressModel.street\n', String(AddressModel.street));
console.log('-- vs --');
console.log('CustomerModel.address.street\n', String(CustomerModel.address.street));
console.log('\n');
console.log('\n\nOrderModel has:\n');
for (const key in OrderModel) {
const value: IModel = (OrderModel as unknown as Record<typeof key, IModel>)[key];
console.log(key, '\n ', String(value));
}