Skip to content

Commit b429878

Browse files
author
Mike Reynolds
committed
Addressing issue 309:
MemoryPackWriter.ts now writes Uint32/Uint64 rather than Float32/Float64 as the 'HasValue' flag for nullable float and double respectively. MemoryPackReader.ts now reads Uint32/Uint64 rather than Float32/Float64 as the 'HasValue' flag for nullable float and double respectively. Added new tests to SandboxWebApp.
1 parent 4b1e3f8 commit b429878

16 files changed

Lines changed: 299 additions & 17 deletions

sandbox/SandboxWebApp/Controllers/MemoryPackController.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,18 @@ public AllConvertableType Post([FromBody] AllConvertableType value)
1212
{
1313
return value;
1414
}
15+
16+
[Route("nullableFloat")]
17+
[HttpPost]
18+
public NullableFloatTest PostNullableTest([FromBody] NullableFloatTest input)
19+
{
20+
var ret = new NullableFloatTest
21+
{
22+
// If you're curious about the '* 1.0' part, DM me :-)
23+
NullableFloat = input.NullableFloat * 1.0F,
24+
NullableDouble = input.NullableDouble * 1.0D
25+
};
26+
27+
return ret;
28+
}
1529
}

sandbox/SandboxWebApp/Models.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,11 @@ public partial class Rec
190190
{
191191
public required Rec Id { get; init; }
192192
}
193+
194+
[MemoryPackable]
195+
[GenerateTypeScript]
196+
public partial class NullableFloatTest
197+
{
198+
public float? NullableFloat { get; set; }
199+
public double? NullableDouble { get; set; }
200+
}

sandbox/SandboxWebApp/Pages/Index.cshtml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
<script type="module">
88
import { test } from "./js/file.js";
99
import { test2 } from "./js/file.js";
10+
import { testNullableFloatWithValues } from "./js/file.js";
11+
import { testNullableFloatWithNulls } from "./js/file.js";
1012
import { hoge } from "./js/file.js";
1113
import { huga } from "./js/file.js";
1214
1315
//hoge();
1416
//huga();
1517
test();
1618
test2();
19+
20+
testNullableFloatWithValues();
21+
testNullableFloatWithNulls();
22+
1723
</script>
1824

1925
<div class="text-center">

sandbox/SandboxWebApp/wwwroot/js/file.js

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sandbox/SandboxWebApp/wwwroot/js/file.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sandbox/SandboxWebApp/wwwroot/js/file.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Subset } from "./memorypack/Subset.js";
1010
import { SampleUnion2 } from "./memorypack/SampleUnion2.js";
1111
import { Person } from "./memorypack/Person.js";
1212
import { Gender } from "./memorypack/Gender.js";
13+
import { NullableFloatTest } from "./memorypack/NullableFloatTest.js";
1314

1415
export async function hoge() {
1516

@@ -207,6 +208,57 @@ export async function test2() {
207208
assertObject(v, v2!);
208209
}
209210

211+
export async function testNullableFloatWithValues()
212+
{
213+
const input = new NullableFloatTest();
214+
input.nullableFloat = 31413.431251;
215+
input.nullableDouble = 9932.425252;
216+
217+
const bin = NullableFloatTest.serialize(input);
218+
const blob = new Blob([bin.buffer], { type: "application/x-memorypack" })
219+
const response = await fetch("http://localhost:5260/api/nullableFloat", { method: "POST", body: blob, headers: { "Content-Type": "application/x-memorypack" } });
220+
221+
if (response.status != 200) {
222+
console.log(response.status);
223+
return;
224+
}
225+
226+
const buffer = await response.arrayBuffer();
227+
const output = NullableFloatTest.deserialize(buffer);
228+
assertNullableFloat(input, output!);
229+
230+
console.log("testNullableFloatWithValues passed.");
231+
}
232+
233+
export async function testNullableFloatWithNulls() {
234+
const input = new NullableFloatTest();
235+
input.nullableFloat = null;
236+
input.nullableDouble = null;
237+
238+
const bin = NullableFloatTest.serialize(input);
239+
const blob = new Blob([bin.buffer], { type: "application/x-memorypack" })
240+
const response = await fetch("http://localhost:5260/api/nullableFloat", { method: "POST", body: blob, headers: { "Content-Type": "application/x-memorypack" } });
241+
242+
if (response.status != 200) {
243+
console.log(response.status);
244+
return;
245+
}
246+
247+
const buffer = await response.arrayBuffer();
248+
const output = NullableFloatTest.deserialize(buffer);
249+
250+
assertIsNull(output!.nullableFloat);
251+
assertIsNull(output!.nullableDouble);
252+
253+
console.log("testNullableFloatWithNulls passed.");
254+
}
255+
256+
function assertNullableFloat(a: NullableFloatTest, b: NullableFloatTest): void
257+
{
258+
ok(a.nullableFloat?.toFixed(1), b.nullableFloat?.toFixed(1))
259+
ok(a.nullableDouble?.toFixed(1), b.nullableDouble?.toFixed(1))
260+
}
261+
210262
function assertObject(v: AllConvertableType, v2: AllConvertableType): void {
211263
ok(v.myBool, v2.myBool);
212264
ok(v.myByte, v2.myByte);
@@ -280,6 +332,12 @@ function assertObject(v: AllConvertableType, v2: AllConvertableType): void {
280332
console.log("test passed");
281333
}
282334

335+
function assertIsNull(value: any) : void
336+
{
337+
if (value !== null)
338+
throw new Error("Invalid: value must be null.");
339+
}
340+
283341
function ok(v1: any, v2: any): void {
284342
if (v1 === v2) return;
285343
throw new Error("Invalid v1:" + v1 + " v2:" + v2);

sandbox/SandboxWebApp/wwwroot/js/memorypack/MemoryPackReader.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sandbox/SandboxWebApp/wwwroot/js/memorypack/MemoryPackReader.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sandbox/SandboxWebApp/wwwroot/js/memorypack/MemoryPackReader.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export class MemoryPackReader {
202202
}
203203

204204
public readNullableFloat32(): number | null {
205-
if (this.readFloat32() == FALSE) {
205+
if (this.readUint32() == FALSE) {
206206
this.offset += 4;
207207
return null;
208208
}
@@ -217,7 +217,7 @@ export class MemoryPackReader {
217217
}
218218

219219
public readNullableFloat64(): number | null {
220-
if (this.readFloat64() == FALSE) {
220+
if (this.readUint64() == 0n) {
221221
this.offset += 8;
222222
return null;
223223
}
@@ -386,4 +386,4 @@ export class MemoryPackReader {
386386
this.offset += length;
387387
return new Uint8Array(span);
388388
}
389-
}
389+
}

sandbox/SandboxWebApp/wwwroot/js/memorypack/MemoryPackWriter.js

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)