skir-typescript-gen 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -28
- package/dist/expression_maker.js +2 -2
- package/dist/record_info.js +1 -1
- package/dist/type_speller.js +1 -1
- package/package.json +4 -4
- package/src/expression_maker.ts +2 -2
- package/src/record_info.ts +1 -1
- package/src/type_speller.ts +1 -1
package/README.md
CHANGED
|
@@ -7,20 +7,15 @@ Official plugin for generating TypeScript/JavaScript code from [.skir](https://g
|
|
|
7
7
|
|
|
8
8
|
Generated code can run Node, Deno or in the browser.
|
|
9
9
|
|
|
10
|
-
##
|
|
11
|
-
|
|
12
|
-
From your project's root directory, run `npm i --save-dev skir-typescript-gen`.
|
|
10
|
+
## Set up
|
|
13
11
|
|
|
14
12
|
In your `skir.yml` file, add the following snippet under `generators`:
|
|
15
13
|
```yaml
|
|
16
14
|
- mod: skir-typescript-gen
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
importPathExtension: ""
|
|
15
|
+
outDir: ./src/skirout
|
|
16
|
+
config: {}
|
|
20
17
|
```
|
|
21
18
|
|
|
22
|
-
The `npm run skirc` command will now generate .js and .t.ts files within the `skirout` directory.
|
|
23
|
-
|
|
24
19
|
For more information, see this TypeScript project [example](https://github.com/gepheum/skir-typescript-example).
|
|
25
20
|
|
|
26
21
|
## TypeScript generated code guide
|
|
@@ -30,7 +25,7 @@ The examples below are for the code generated from [this](https://github.com/gep
|
|
|
30
25
|
### Referring to generated symbols
|
|
31
26
|
|
|
32
27
|
```typescript
|
|
33
|
-
import { TARZAN, User, UserHistory, UserRegistry } from "../skirout/user";
|
|
28
|
+
import { TARZAN, SubscriptionStatus, User, UserHistory, UserRegistry } from "../skirout/user";
|
|
34
29
|
```
|
|
35
30
|
|
|
36
31
|
### Struct classes
|
|
@@ -162,14 +157,14 @@ enum SubscriptionStatus {
|
|
|
162
157
|
#### Making enum values
|
|
163
158
|
|
|
164
159
|
```typescript
|
|
165
|
-
const johnStatus =
|
|
166
|
-
const janeStatus =
|
|
167
|
-
const lylaStatus =
|
|
168
|
-
// ^ same as
|
|
169
|
-
const jolyStatus =
|
|
170
|
-
|
|
171
|
-
// Use create({kind: ..., value: ...}) for
|
|
172
|
-
const roniStatus =
|
|
160
|
+
const johnStatus = SubscriptionStatus.FREE;
|
|
161
|
+
const janeStatus = SubscriptionStatus.PREMIUM;
|
|
162
|
+
const lylaStatus = SubscriptionStatus.create("PREMIUM");
|
|
163
|
+
// ^ same as SubscriptionStatus.PREMIUM
|
|
164
|
+
const jolyStatus = SubscriptionStatus.UNKNOWN;
|
|
165
|
+
|
|
166
|
+
// Use create({kind: ..., value: ...}) for wrapper variants.
|
|
167
|
+
const roniStatus = SubscriptionStatus.create({
|
|
173
168
|
kind: "trial",
|
|
174
169
|
value: {
|
|
175
170
|
startTime: Timestamp.fromUnixMillis(1234),
|
|
@@ -180,18 +175,19 @@ const roniStatus = User.SubscriptionStatus.create({
|
|
|
180
175
|
#### Conditions on enums
|
|
181
176
|
|
|
182
177
|
```typescript
|
|
183
|
-
// Use
|
|
184
|
-
assert(johnStatus.kind === "FREE");
|
|
185
|
-
assert(johnStatus.value === undefined);
|
|
178
|
+
// Use 'union.kind' to check which variant the enum value holds.
|
|
179
|
+
assert(johnStatus.union.kind === "FREE");
|
|
186
180
|
|
|
187
181
|
// Use "?" for UNKNOWN.
|
|
188
|
-
assert(jolyStatus.kind === "?");
|
|
182
|
+
assert(jolyStatus.union.kind === "?");
|
|
189
183
|
|
|
190
|
-
assert(roniStatus.kind === "trial");
|
|
191
|
-
|
|
184
|
+
assert(roniStatus.union.kind === "trial");
|
|
185
|
+
// If the enum holds a wrapper variant, you can access the wrapped value through
|
|
186
|
+
// 'union.value'.
|
|
187
|
+
assert(roniStatus.union.value.startTime.unixMillis === 1234);
|
|
192
188
|
|
|
193
|
-
function getSubscriptionInfoText(status:
|
|
194
|
-
//
|
|
189
|
+
function getSubscriptionInfoText(status: SubscriptionStatus): string {
|
|
190
|
+
// Pattern matching on enum variants
|
|
195
191
|
switch (status.union.kind) {
|
|
196
192
|
case "?":
|
|
197
193
|
return "Unknown subscription status";
|
|
@@ -200,7 +196,8 @@ function getSubscriptionInfoText(status: User.SubscriptionStatus): string {
|
|
|
200
196
|
case "PREMIUM":
|
|
201
197
|
return "Premium user";
|
|
202
198
|
case "trial":
|
|
203
|
-
// Here the compiler knows that the type of union.value is
|
|
199
|
+
// Here the compiler knows that the type of union.value is
|
|
200
|
+
// SubscriptionStatus.Trial
|
|
204
201
|
return "On trial since " + status.union.value.startTime;
|
|
205
202
|
}
|
|
206
203
|
}
|
|
@@ -232,7 +229,7 @@ console.log(serializer.toJsonCode(john, "readable"));
|
|
|
232
229
|
// You should pick the readable flavor mostly for debugging purposes.
|
|
233
230
|
|
|
234
231
|
// Serialize 'john' to binary format.
|
|
235
|
-
|
|
232
|
+
const johnBytes = serializer.toBytes(john);
|
|
236
233
|
|
|
237
234
|
// The binary format is not human readable, but it is slightly more compact than
|
|
238
235
|
// JSON, and serialization/deserialization can be a bit faster in languages like
|
|
@@ -289,7 +286,7 @@ assert(jack.pets === jade.pets);
|
|
|
289
286
|
|
|
290
287
|
```typescript
|
|
291
288
|
const userRegistry = UserRegistry.create({
|
|
292
|
-
users: [john, jane, lylaMut],
|
|
289
|
+
users: [john, jane, lylaMut, evilJane],
|
|
293
290
|
});
|
|
294
291
|
|
|
295
292
|
// searchUsers() returns the user with the given key (specified in the .skir
|
|
@@ -298,6 +295,10 @@ const userRegistry = UserRegistry.create({
|
|
|
298
295
|
// time.
|
|
299
296
|
assert(userRegistry.searchUsers(42) === john);
|
|
300
297
|
assert(userRegistry.searchUsers(100) === undefined);
|
|
298
|
+
|
|
299
|
+
// If multiple elements have the same key, the search method returns the last
|
|
300
|
+
// one. Duplicates are allowed but generally discouraged.
|
|
301
|
+
assert(userRegistry.searchUsers(43) === evilJane);
|
|
301
302
|
```
|
|
302
303
|
|
|
303
304
|
### Constants
|
package/dist/expression_maker.js
CHANGED
|
@@ -81,7 +81,7 @@ export function toFrozenExpression(arg) {
|
|
|
81
81
|
primitive === "float64") {
|
|
82
82
|
defaultValue = "0";
|
|
83
83
|
}
|
|
84
|
-
else if (primitive === "int64" || primitive === "
|
|
84
|
+
else if (primitive === "int64" || primitive === "hash64") {
|
|
85
85
|
defaultValue = "BigInt(0)";
|
|
86
86
|
}
|
|
87
87
|
else if (primitive === "timestamp") {
|
|
@@ -112,7 +112,7 @@ function canBeFalsy(type) {
|
|
|
112
112
|
return (primitive === "bool" ||
|
|
113
113
|
primitive === "int32" ||
|
|
114
114
|
primitive === "int64" ||
|
|
115
|
-
primitive === "
|
|
115
|
+
primitive === "hash64" ||
|
|
116
116
|
primitive === "float32" ||
|
|
117
117
|
primitive === "float64" ||
|
|
118
118
|
primitive === "string");
|
package/dist/record_info.js
CHANGED
package/dist/type_speller.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "skir-typescript-gen",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"repository": {
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"lint:fix": "eslint src/**/*.ts --fix"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"skir-internal": "^0.0
|
|
35
|
+
"skir-internal": "^0.1.0",
|
|
36
36
|
"zod": "^4.2.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
@@ -46,8 +46,8 @@
|
|
|
46
46
|
"mocha": "^11.7.5",
|
|
47
47
|
"prettier": "^3.2.4",
|
|
48
48
|
"prettier-plugin-organize-imports": "^4.2.0",
|
|
49
|
-
"skir": "^
|
|
50
|
-
"skir-client": "^0.0
|
|
49
|
+
"skir": "^1.0.4",
|
|
50
|
+
"skir-client": "^0.1.0",
|
|
51
51
|
"ts-node": "^10.9.2",
|
|
52
52
|
"tsx": "^4.21.0",
|
|
53
53
|
"typescript": "^5.2.2",
|
package/src/expression_maker.ts
CHANGED
|
@@ -92,7 +92,7 @@ export function toFrozenExpression(arg: ToFrozenExpressionArg): string {
|
|
|
92
92
|
primitive === "float64"
|
|
93
93
|
) {
|
|
94
94
|
defaultValue = "0";
|
|
95
|
-
} else if (primitive === "int64" || primitive === "
|
|
95
|
+
} else if (primitive === "int64" || primitive === "hash64") {
|
|
96
96
|
defaultValue = "BigInt(0)";
|
|
97
97
|
} else if (primitive === "timestamp") {
|
|
98
98
|
defaultValue = "$.Timestamp.UNIX_EPOCH";
|
|
@@ -121,7 +121,7 @@ function canBeFalsy(type: ResolvedType): boolean {
|
|
|
121
121
|
primitive === "bool" ||
|
|
122
122
|
primitive === "int32" ||
|
|
123
123
|
primitive === "int64" ||
|
|
124
|
-
primitive === "
|
|
124
|
+
primitive === "hash64" ||
|
|
125
125
|
primitive === "float32" ||
|
|
126
126
|
primitive === "float64" ||
|
|
127
127
|
primitive === "string"
|
package/src/record_info.ts
CHANGED