Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

Commit 21b18f5

Browse files
fix: fix serialization of Point and Location types (#854)
1 parent 80656ff commit 21b18f5

File tree

4 files changed

+77
-18
lines changed

4 files changed

+77
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- fix: omit parallel_tool_calls in Go OpenAI SDK if it is set to true [#849](https://github.com/hypermodeinc/modus/pull/849)
88
- feat: use embedded postgres on Windows [#851](https://github.com/hypermodeinc/modus/pull/851)
99
- feat: add functions for parsing chat messages [#853](https://github.com/hypermodeinc/modus/pull/853)
10+
- fix: fix serialization of Point and Location types [#854](https://github.com/hypermodeinc/modus/pull/854)
1011

1112
## 2025-05-19 - Go SDK 0.18.0-alpha.2
1213

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2025 Hypermode Inc.
3+
* Licensed under the terms of the Apache License, Version 2.0
4+
* See the LICENSE file that accompanied this code for further details.
5+
*
6+
* SPDX-FileCopyrightText: 2025 Hypermode Inc. <hello@hypermode.com>
7+
* SPDX-License-Identifier: Apache-2.0
8+
*/
9+
10+
import { expect, it, run } from "as-test";
11+
import { JSON } from "json-as";
12+
import { Point, Location } from "../database";
13+
14+
it("should serialize a Point object", () => {
15+
const point = new Point(1, 2);
16+
const json = JSON.stringify(point);
17+
expect(json).toBe(`"(1.0,2.0)"`);
18+
});
19+
20+
it("should deserialize a Point object", () => {
21+
const json = `"(1.0,2.0)"`;
22+
const point = JSON.parse<Point>(json);
23+
expect(point.x).toBe(1);
24+
expect(point.y).toBe(2);
25+
});
26+
27+
it("should serialize a Location object", () => {
28+
const location = new Location(1, 2);
29+
const json = JSON.stringify(location);
30+
expect(json).toBe(`"(1.0,2.0)"`);
31+
});
32+
33+
it("should deserialize a Location object", () => {
34+
const json = `"(1.0,2.0)"`;
35+
const location = JSON.parse<Location>(json);
36+
expect(location.longitude).toBe(1);
37+
expect(location.latitude).toBe(2);
38+
});
39+
40+
run();

sdk/assemblyscript/src/assembly/database.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ export function queryScalar<T>(
171171
*
172172
* Note that this class is identical to the Location class, but uses different field names.
173173
*/
174+
@json
174175
export class Point {
175176
constructor(
176177
public x: f64,
@@ -189,27 +190,27 @@ export class Point {
189190
return new Point(p[0], p[1]);
190191
}
191192

192-
// The following methods are required for custom JSON serialization
193-
// This is used in lieu of the @json decorator, so that the class can be
194-
// serialized to a string in SQL format.
195193

196194
@serializer
197195
private serialize(self: Point): string {
198-
return self.toString();
196+
return `"${self}"`;
199197
}
200198

201199

202200
@deserializer
203-
private deserialize(data: string): Point | null {
201+
private deserialize(data: string): Point {
204202
if (
205203
data.length < 7 ||
206204
data.charAt(0) != '"' ||
207205
data.charAt(data.length - 1) != '"'
208-
)
209-
return null;
206+
) {
207+
throw new Error("Invalid Point string");
208+
}
210209

211210
const p = parsePointString(data.substring(1, data.length - 1));
212-
if (p.length == 0) return null;
211+
if (p.length == 0) {
212+
throw new Error("Invalid Point string");
213+
}
213214

214215
this.x = p[0];
215216
this.y = p[1];
@@ -223,6 +224,7 @@ export class Point {
223224
*
224225
* Note that this class is identical to the `Point` class, but uses different field names.
225226
*/
227+
@json
226228
export class Location {
227229
constructor(
228230
public longitude: f64,
@@ -233,35 +235,35 @@ export class Location {
233235
return `(${this.longitude},${this.latitude})`;
234236
}
235237

236-
public static fromString(data: string): Point | null {
238+
public static fromString(data: string): Location | null {
237239
const p = parsePointString(data);
238240
if (p.length == 0) {
239241
return null;
240242
}
241-
return new Point(p[0], p[1]);
243+
return new Location(p[0], p[1]);
242244
}
243245

244-
// The following methods are required for custom JSON serialization
245-
// This is used in lieu of the @json decorator, so that the class can be
246-
// serialized to a string in SQL format.
247246

248247
@serializer
249248
private serialize(self: Location): string {
250-
return '"' + self.toString() + '"';
249+
return `"${self}"`;
251250
}
252251

253252

254253
@deserializer
255-
private deserialize(data: string): Location | null {
254+
private deserialize(data: string): Location {
256255
if (
257256
data.length < 7 ||
258257
data.charAt(0) != '"' ||
259258
data.charAt(data.length - 1) != '"'
260-
)
261-
return null;
259+
) {
260+
throw new Error("Invalid Location string");
261+
}
262262

263263
const p = parsePointString(data.substring(1, data.length - 1));
264-
if (p.length == 0) return null;
264+
if (p.length == 0) {
265+
throw new Error("Invalid Location string");
266+
}
265267

266268
this.longitude = p[0];
267269
this.latitude = p[1];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright 2025 Hypermode Inc.
3+
* Licensed under the terms of the Apache License, Version 2.0
4+
* See the LICENSE file that accompanied this code for further details.
5+
*
6+
* SPDX-FileCopyrightText: 2025 Hypermode Inc. <hello@hypermode.com>
7+
* SPDX-License-Identifier: Apache-2.0
8+
*/
9+
10+
import { readFileSync } from "fs";
11+
import { instantiate } from "../build/database.spec.js";
12+
const binary = readFileSync("./build/database.spec.wasm");
13+
const module = new WebAssembly.Module(binary);
14+
instantiate(module, {
15+
env: {},
16+
});

0 commit comments

Comments
 (0)