Skip to content

Commit

Permalink
Merge pull request #313 from Safekick/issue-309-fix
Browse files Browse the repository at this point in the history
(Hopefully) Fixed issue 309.
  • Loading branch information
neuecc authored Sep 18, 2024
2 parents 6c99ca3 + b429878 commit 7e70a28
Show file tree
Hide file tree
Showing 16 changed files with 299 additions and 17 deletions.
14 changes: 14 additions & 0 deletions sandbox/SandboxWebApp/Controllers/MemoryPackController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,18 @@ public AllConvertableType Post([FromBody] AllConvertableType value)
{
return value;
}

[Route("nullableFloat")]
[HttpPost]
public NullableFloatTest PostNullableTest([FromBody] NullableFloatTest input)
{
var ret = new NullableFloatTest
{
// If you're curious about the '* 1.0' part, DM me :-)
NullableFloat = input.NullableFloat * 1.0F,
NullableDouble = input.NullableDouble * 1.0D
};

return ret;
}
}
8 changes: 8 additions & 0 deletions sandbox/SandboxWebApp/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,11 @@ public partial class Rec
{
public required Rec Id { get; init; }
}

[MemoryPackable]
[GenerateTypeScript]
public partial class NullableFloatTest
{
public float? NullableFloat { get; set; }
public double? NullableDouble { get; set; }
}
6 changes: 6 additions & 0 deletions sandbox/SandboxWebApp/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
<script type="module">
import { test } from "./js/file.js";
import { test2 } from "./js/file.js";
import { testNullableFloatWithValues } from "./js/file.js";
import { testNullableFloatWithNulls } from "./js/file.js";
import { hoge } from "./js/file.js";
import { huga } from "./js/file.js";
//hoge();
//huga();
test();
test2();
testNullableFloatWithValues();
testNullableFloatWithNulls();
</script>

<div class="text-center">
Expand Down
42 changes: 42 additions & 0 deletions sandbox/SandboxWebApp/wwwroot/js/file.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sandbox/SandboxWebApp/wwwroot/js/file.js.map

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions sandbox/SandboxWebApp/wwwroot/js/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Subset } from "./memorypack/Subset.js";
import { SampleUnion2 } from "./memorypack/SampleUnion2.js";
import { Person } from "./memorypack/Person.js";
import { Gender } from "./memorypack/Gender.js";
import { NullableFloatTest } from "./memorypack/NullableFloatTest.js";

export async function hoge() {

Expand Down Expand Up @@ -207,6 +208,57 @@ export async function test2() {
assertObject(v, v2!);
}

export async function testNullableFloatWithValues()
{
const input = new NullableFloatTest();
input.nullableFloat = 31413.431251;
input.nullableDouble = 9932.425252;

const bin = NullableFloatTest.serialize(input);
const blob = new Blob([bin.buffer], { type: "application/x-memorypack" })
const response = await fetch("http://localhost:5260/api/nullableFloat", { method: "POST", body: blob, headers: { "Content-Type": "application/x-memorypack" } });

if (response.status != 200) {
console.log(response.status);
return;
}

const buffer = await response.arrayBuffer();
const output = NullableFloatTest.deserialize(buffer);
assertNullableFloat(input, output!);

console.log("testNullableFloatWithValues passed.");
}

export async function testNullableFloatWithNulls() {
const input = new NullableFloatTest();
input.nullableFloat = null;
input.nullableDouble = null;

const bin = NullableFloatTest.serialize(input);
const blob = new Blob([bin.buffer], { type: "application/x-memorypack" })
const response = await fetch("http://localhost:5260/api/nullableFloat", { method: "POST", body: blob, headers: { "Content-Type": "application/x-memorypack" } });

if (response.status != 200) {
console.log(response.status);
return;
}

const buffer = await response.arrayBuffer();
const output = NullableFloatTest.deserialize(buffer);

assertIsNull(output!.nullableFloat);
assertIsNull(output!.nullableDouble);

console.log("testNullableFloatWithNulls passed.");
}

function assertNullableFloat(a: NullableFloatTest, b: NullableFloatTest): void
{
ok(a.nullableFloat?.toFixed(1), b.nullableFloat?.toFixed(1))
ok(a.nullableDouble?.toFixed(1), b.nullableDouble?.toFixed(1))
}

function assertObject(v: AllConvertableType, v2: AllConvertableType): void {
ok(v.myBool, v2.myBool);
ok(v.myByte, v2.myByte);
Expand Down Expand Up @@ -280,6 +332,12 @@ function assertObject(v: AllConvertableType, v2: AllConvertableType): void {
console.log("test passed");
}

function assertIsNull(value: any) : void
{
if (value !== null)
throw new Error("Invalid: value must be null.");
}

function ok(v1: any, v2: any): void {
if (v1 === v2) return;
throw new Error("Invalid v1:" + v1 + " v2:" + v2);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export class MemoryPackReader {
}

public readNullableFloat32(): number | null {
if (this.readFloat32() == FALSE) {
if (this.readUint32() == FALSE) {
this.offset += 4;
return null;
}
Expand All @@ -217,7 +217,7 @@ export class MemoryPackReader {
}

public readNullableFloat64(): number | null {
if (this.readFloat64() == FALSE) {
if (this.readUint64() == 0n) {
this.offset += 8;
return null;
}
Expand Down Expand Up @@ -386,4 +386,4 @@ export class MemoryPackReader {
this.offset += length;
return new Uint8Array(span);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7e70a28

Please sign in to comment.