Skip to content

Commit c37ff48

Browse files
committed
feat: add $uint64AsString decorator
1 parent d097e06 commit c37ff48

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { $int64AsString } from "./int64AsString.js";
2+
export { $uint64AsString } from "./uint64AsString.js";

int64AsString.tsp renamed to index.tsp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import "./int64AsString.js";
2+
import "./uint64AsString.js";
23

34
using TypeSpec.Reflection;
45

@@ -7,3 +8,9 @@ using TypeSpec.Reflection;
78
* This decorator is equivalent to `@encode("int64", string)`.
89
*/
910
extern dec int64AsString(target: ModelProperty);
11+
12+
/**
13+
* Emit string type from uint64.
14+
* This decorator is equivalent to `@encode("uint64", string)`.
15+
*/
16+
extern dec uint64AsString(target: ModelProperty);

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
],
1717
"type": "module",
1818
"main": "index.js",
19-
"tspMain": "int64AsString.tsp",
19+
"tspMain": "index.tsp",
2020
"files": [
2121
"index.js",
22+
"index.tsp",
2223
"int64AsString.js",
23-
"int64AsString.tsp"
24+
"uint64AsString.js"
2425
],
2526
"peerDependencies": {
2627
"@typespec/compiler": "^0.67.1"

uint64AsString.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { $encode } from "@typespec/compiler";
2+
3+
/**
4+
* @param context {import("@typespec/compiler").DecoratorContext}
5+
* @param target {import("@typespec/compiler").ModelProperty}
6+
*/
7+
export function $uint64AsString(context, target) {
8+
if ("name" in target.type && target.type.name === "uint64") {
9+
$encode(context, target, "uint64");
10+
} else {
11+
throw new Error(
12+
"@uint64AsString decorator can only be used for uint64 properties.",
13+
);
14+
}
15+
}

0 commit comments

Comments
 (0)