Skip to content

feat(core/serde): add NumericValue container #1544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/neat-queens-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/core": minor
---

add numeric value container for serde
9 changes: 9 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
"import": "./dist-es/submodules/protocols/index.js",
"require": "./dist-cjs/submodules/protocols/index.js",
"types": "./dist-types/submodules/protocols/index.d.ts"
},
"./serde": {
"module": "./dist-es/submodules/serde/index.js",
"node": "./dist-cjs/submodules/serde/index.js",
"import": "./dist-es/submodules/serde/index.js",
"require": "./dist-cjs/submodules/serde/index.js",
"types": "./dist-types/submodules/serde/index.d.ts"
}
},
"author": {
Expand Down Expand Up @@ -78,6 +85,8 @@
"./cbor.js",
"./protocols.d.ts",
"./protocols.js",
"./serde.d.ts",
"./serde.js",
"dist-*/**"
],
"homepage": "https://github.com/awslabs/smithy-typescript/tree/main/packages/core",
Expand Down
7 changes: 7 additions & 0 deletions packages/core/serde.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Do not edit:
* This is a compatibility redirect for contexts that do not understand package.json exports field.
*/
declare module "@smithy/core/serde" {
export * from "@smithy/core/dist-types/submodules/serde/index.d";
}
6 changes: 6 additions & 0 deletions packages/core/serde.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

/**
* Do not edit:
* This is a compatibility redirect for contexts that do not understand package.json exports field.
*/
module.exports = require("./dist-cjs/submodules/serde/index.js");
1 change: 1 addition & 0 deletions packages/core/src/submodules/serde/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./value/NumericValue";
12 changes: 12 additions & 0 deletions packages/core/src/submodules/serde/value/NumericValue.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, expect, test as it } from "vitest";

import { NumericValue, nv } from "./NumericValue";

describe(NumericValue.name, () => {
it("holds a string representation of a numeric value", () => {
const num = nv("1.0");
expect(num).toBeInstanceOf(NumericValue);
expect(num.string).toEqual("1.0");
expect(num.type).toEqual("bigDecimal");
});
});
36 changes: 36 additions & 0 deletions packages/core/src/submodules/serde/value/NumericValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Types which may be represented by {@link NumericValue}.
*
* There is currently only one option, because BigInteger and Long should
* use JS BigInt directly, and all other numeric types can be contained in JS Number.
*
* @public
*/
export type NumericType = "bigDecimal";

/**
* Serialization container for Smithy simple types that do not have a
* direct JavaScript runtime representation.
*
* This container does not perform numeric mathematical operations.
* It is a container for discerning a value's true type.
*
* It allows storage of numeric types not representable in JS without
* making a decision on what numeric library to use.
*
* @public
*/
export class NumericValue {
public constructor(
public readonly string: string,
public readonly type: NumericType
) {}
}

/**
* Serde shortcut.
* @internal
*/
export function nv(string: string): NumericValue {
return new NumericValue(string, "bigDecimal");
}
3 changes: 2 additions & 1 deletion packages/core/tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"rootDir": "src",
"paths": {
"@smithy/core/cbor": ["./src/submodules/cbor/index.ts"],
"@smithy/core/protocols": ["./src/submodules/protocols/index.ts"]
"@smithy/core/protocols": ["./src/submodules/protocols/index.ts"],
"@smithy/core/serde": ["./src/submodules/serde/index.ts"]
}
},
"extends": "../../tsconfig.cjs.json",
Expand Down
3 changes: 2 additions & 1 deletion packages/core/tsconfig.es.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"rootDir": "src",
"paths": {
"@smithy/core/cbor": ["./src/submodules/cbor/index.ts"],
"@smithy/core/protocols": ["./src/submodules/protocols/index.ts"]
"@smithy/core/protocols": ["./src/submodules/protocols/index.ts"],
"@smithy/core/serde": ["./src/submodules/serde/index.ts"]
}
},
"extends": "../../tsconfig.es.json",
Expand Down
3 changes: 2 additions & 1 deletion packages/core/tsconfig.types.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"rootDir": "src",
"paths": {
"@smithy/core/cbor": ["./src/submodules/cbor/index.ts"],
"@smithy/core/protocols": ["./src/submodules/protocols/index.ts"]
"@smithy/core/protocols": ["./src/submodules/protocols/index.ts"],
"@smithy/core/serde": ["./src/submodules/serde/index.ts"]
}
},
"extends": "../../tsconfig.types.json",
Expand Down
Loading