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 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
- ## Installation
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
- config:
18
- # Possible values: "" (CommonJS), ".js" (ES Modules), ".ts" (Deno)
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 = User.SubscriptionStatus.FREE;
166
- const janeStatus = User.SubscriptionStatus.PREMIUM;
167
- const lylaStatus = User.SubscriptionStatus.create("PREMIUM");
168
- // ^ same as User.SubscriptionStatus.PREMIUM
169
- const jolyStatus = User.SubscriptionStatus.UNKNOWN;
170
-
171
- // Use create({kind: ..., value: ...}) for data variants.
172
- const roniStatus = User.SubscriptionStatus.create({
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 e.kind === "CONSTANT_NAME" to check if the enum value is a constant.
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
- assert(roniStatus.value!.startTime.unixMillis === 1234);
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: User.SubscriptionStatus): string {
194
- // Use the 'union' getter for typesafe switches on enums.
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 'User.Trial'.
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
- console.log(serializer.toBytes(john));
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
@@ -81,7 +81,7 @@ export function toFrozenExpression(arg) {
81
81
  primitive === "float64") {
82
82
  defaultValue = "0";
83
83
  }
84
- else if (primitive === "int64" || primitive === "uint64") {
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 === "uint64" ||
115
+ primitive === "hash64" ||
116
116
  primitive === "float32" ||
117
117
  primitive === "float64" ||
118
118
  primitive === "string");
@@ -235,7 +235,7 @@ class RecordInfoCreator {
235
235
  case "string":
236
236
  return "k";
237
237
  case "int64":
238
- case "uint64":
238
+ case "hash64":
239
239
  // BigInt is not hashable.
240
240
  return "k.toString()";
241
241
  case "timestamp":
@@ -101,7 +101,7 @@ export class TypeSpeller {
101
101
  case "float64":
102
102
  return TsType.NUMBER;
103
103
  case "int64":
104
- case "uint64":
104
+ case "hash64":
105
105
  return TsType.BIGINT;
106
106
  case "timestamp":
107
107
  return TsType.TIMESTAMP;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "skir-typescript-gen",
3
- "version": "1.0.1",
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.8",
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": "^0.0.7",
50
- "skir-client": "^0.0.10",
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",
@@ -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 === "uint64") {
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 === "uint64" ||
124
+ primitive === "hash64" ||
125
125
  primitive === "float32" ||
126
126
  primitive === "float64" ||
127
127
  primitive === "string"
@@ -409,7 +409,7 @@ class RecordInfoCreator {
409
409
  case "string":
410
410
  return "k";
411
411
  case "int64":
412
- case "uint64":
412
+ case "hash64":
413
413
  // BigInt is not hashable.
414
414
  return "k.toString()";
415
415
  case "timestamp":
@@ -124,7 +124,7 @@ export class TypeSpeller {
124
124
  case "float64":
125
125
  return TsType.NUMBER;
126
126
  case "int64":
127
- case "uint64":
127
+ case "hash64":
128
128
  return TsType.BIGINT;
129
129
  case "timestamp":
130
130
  return TsType.TIMESTAMP;