Skip to content

Emi float64 data type

Example schema:

name: MyFloatClass
fields:
- name: floatWithDefault
default: 3.14
type: float64
- name: plainFloat
type: float64
  • float64 maps to JavaScript number with double precision.
  • Default is 0.0 if not specified.
  • float64? allows null and undefined, defaulting to undefined.
import { type PartialDeep } from "./sdk/common/fetchx";
/**
* The base class definition for myFloatClassDto
**/
export class MyFloatClassDto {
/**
*
* @type {number}
**/
#floatWithDefault: number = 3.14;
/**
*
* @returns {number}
**/
get floatWithDefault() {
return this.#floatWithDefault;
}
/**
*
* @type {number}
**/
set floatWithDefault(value: number) {
this.#floatWithDefault = value;
}
setFloatWithDefault(value: number) {
this.floatWithDefault = value;
return this;
}
/**
*
* @type {number}
**/
#plainFloat: number = 0.0;
/**
*
* @returns {number}
**/
get plainFloat() {
return this.#plainFloat;
}
/**
*
* @type {number}
**/
set plainFloat(value: number) {
this.#plainFloat = value;
}
setPlainFloat(value: number) {
this.plainFloat = value;
return this;
}
constructor(data: unknown = undefined) {
if (data === null || data === undefined) {
return;
}
if (typeof data === "string") {
this.applyFromObject(JSON.parse(data));
} else if (this.#isJsonAppliable(data)) {
this.applyFromObject(data);
} else {
throw new Error(
"Instance cannot be created on an unknown value, check the content being passed. got: " +
typeof data,
);
}
}
#isJsonAppliable(obj: unknown) {
const g = globalThis as unknown as { Buffer: any; Blob: any };
const isBuffer =
typeof g.Buffer !== "undefined" &&
typeof g.Buffer.isBuffer === "function" &&
g.Buffer.isBuffer(obj);
const isBlob = typeof g.Blob !== "undefined" && obj instanceof g.Blob;
return (
obj &&
typeof obj === "object" &&
!Array.isArray(obj) &&
!isBuffer &&
!(obj instanceof ArrayBuffer) &&
!isBlob
);
}
/**
* casts the fields of a javascript object into the class properties one by one
**/
applyFromObject(data = {}) {
const d = data as Partial<MyFloatClassDto>;
if (d.floatWithDefault !== undefined) {
this.floatWithDefault = d.floatWithDefault;
}
if (d.plainFloat !== undefined) {
this.plainFloat = d.plainFloat;
}
}
/**
* Special toJSON override, since the field are private,
* Json stringify won't see them unless we mention it explicitly. Each
* field goes through #toPlainJSON rather than a bare this.#field: a
* nested dto instance, or an emigo Array/One/Collection wrapper, has its
* own toJSON that JSON.stringify would only reach on a *second* pass -
* calling it here means toJSON()'s own return value is already the same
* plain shape a consumer choosing the exported type instead of this
* class gets, rather than a shallow object still holding class instances.
**/
toJSON(): MyFloatClassDtoType {
return {
floatWithDefault: this.#toPlainJSON(this.#floatWithDefault),
plainFloat: this.#toPlainJSON(this.#plainFloat),
} as MyFloatClassDtoType;
}
/**
* Resolves value into a plain, JSON-serializable value: recurses into
* arrays, and - the case JSON.stringify's own recursion would only get to
* after this method already returned - calls a nested value's own
* toJSON() (a dto instance, or an emigo Array/One/Collection wrapper)
* rather than leaving it as-is. A plain value (string, number, a map's
* own plain object, ...) is returned unchanged.
**/
#toPlainJSON(value: unknown): unknown {
if (value === null || value === undefined) {
return value;
}
if (Array.isArray(value)) {
return value.map((item) => this.#toPlainJSON(item));
}
const asAny = value as any;
if (typeof asAny.toJSON === "function") {
return this.#toPlainJSON(asAny.toJSON());
}
return value;
}
toString() {
return JSON.stringify(this);
}
static get Fields() {
return {
floatWithDefault: "floatWithDefault",
plainFloat: "plainFloat",
};
}
/**
* Creates an instance of MyFloatClassDto, and possibleDtoObject
* needs to satisfy the type requirement fully, otherwise typescript compile would
* be complaining.
**/
static from(possibleDtoObject: MyFloatClassDtoType) {
return new MyFloatClassDto(possibleDtoObject);
}
/**
* Creates an instance of MyFloatClassDto, and partialDtoObject
* needs to satisfy the type, but partially, and rest of the content would
* be constructed according to data types and nullability.
**/
static with(partialDtoObject: PartialDeep<MyFloatClassDtoType>) {
return new MyFloatClassDto(partialDtoObject);
}
copyWith(
partial: PartialDeep<MyFloatClassDtoType>,
): InstanceType<typeof MyFloatClassDto> {
return new MyFloatClassDto({ ...this.toJSON(), ...partial });
}
clone(): InstanceType<typeof MyFloatClassDto> {
return new MyFloatClassDto(this.toJSON());
}
}
export abstract class MyFloatClassDtoFactory {
abstract create(data: unknown): MyFloatClassDto;
}
/**
* The base type definition for myFloatClassDto
**/
export type MyFloatClassDtoType = {
/**
*
* @type {number}
**/
floatWithDefault: number;
/**
*
* @type {number}
**/
plainFloat: number;
};
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace MyFloatClassDtoType {}

Nullable version:

name: MyFloatClass
fields:
- name: nullableFloatWithDefault
default: 1.23
type: float64?
- name: nullableFloatWithoutDefault
type: float64?

Defaults to undefined, but you can assign any float value or null.

import { type PartialDeep } from "./sdk/common/fetchx";
/**
* The base class definition for myFloatClassDto
**/
export class MyFloatClassDto {
/**
*
* @type {number}
**/
#nullableFloatWithDefault?: number | null | undefined = 1.23;
/**
*
* @returns {number}
**/
get nullableFloatWithDefault() {
return this.#nullableFloatWithDefault;
}
/**
*
* @type {number}
**/
set nullableFloatWithDefault(value: number | null | undefined) {
const correctType =
typeof value === "number" || value === undefined || value === null;
const parsedValue = correctType ? value : Number(value);
if (!Number.isNaN(parsedValue)) {
this.#nullableFloatWithDefault = parsedValue;
}
}
setNullableFloatWithDefault(value: number | null | undefined) {
this.nullableFloatWithDefault = value;
return this;
}
/**
*
* @type {number}
**/
#nullableFloatWithoutDefault?: number | null | undefined = undefined;
/**
*
* @returns {number}
**/
get nullableFloatWithoutDefault() {
return this.#nullableFloatWithoutDefault;
}
/**
*
* @type {number}
**/
set nullableFloatWithoutDefault(value: number | null | undefined) {
const correctType =
typeof value === "number" || value === undefined || value === null;
const parsedValue = correctType ? value : Number(value);
if (!Number.isNaN(parsedValue)) {
this.#nullableFloatWithoutDefault = parsedValue;
}
}
setNullableFloatWithoutDefault(value: number | null | undefined) {
this.nullableFloatWithoutDefault = value;
return this;
}
constructor(data: unknown = undefined) {
if (data === null || data === undefined) {
return;
}
if (typeof data === "string") {
this.applyFromObject(JSON.parse(data));
} else if (this.#isJsonAppliable(data)) {
this.applyFromObject(data);
} else {
throw new Error(
"Instance cannot be created on an unknown value, check the content being passed. got: " +
typeof data,
);
}
}
#isJsonAppliable(obj: unknown) {
const g = globalThis as unknown as { Buffer: any; Blob: any };
const isBuffer =
typeof g.Buffer !== "undefined" &&
typeof g.Buffer.isBuffer === "function" &&
g.Buffer.isBuffer(obj);
const isBlob = typeof g.Blob !== "undefined" && obj instanceof g.Blob;
return (
obj &&
typeof obj === "object" &&
!Array.isArray(obj) &&
!isBuffer &&
!(obj instanceof ArrayBuffer) &&
!isBlob
);
}
/**
* casts the fields of a javascript object into the class properties one by one
**/
applyFromObject(data = {}) {
const d = data as Partial<MyFloatClassDto>;
if (d.nullableFloatWithDefault !== undefined) {
this.nullableFloatWithDefault = d.nullableFloatWithDefault;
}
if (d.nullableFloatWithoutDefault !== undefined) {
this.nullableFloatWithoutDefault = d.nullableFloatWithoutDefault;
}
}
/**
* Special toJSON override, since the field are private,
* Json stringify won't see them unless we mention it explicitly. Each
* field goes through #toPlainJSON rather than a bare this.#field: a
* nested dto instance, or an emigo Array/One/Collection wrapper, has its
* own toJSON that JSON.stringify would only reach on a *second* pass -
* calling it here means toJSON()'s own return value is already the same
* plain shape a consumer choosing the exported type instead of this
* class gets, rather than a shallow object still holding class instances.
**/
toJSON(): MyFloatClassDtoType {
return {
nullableFloatWithDefault: this.#toPlainJSON(
this.#nullableFloatWithDefault,
),
nullableFloatWithoutDefault: this.#toPlainJSON(
this.#nullableFloatWithoutDefault,
),
} as MyFloatClassDtoType;
}
/**
* Resolves value into a plain, JSON-serializable value: recurses into
* arrays, and - the case JSON.stringify's own recursion would only get to
* after this method already returned - calls a nested value's own
* toJSON() (a dto instance, or an emigo Array/One/Collection wrapper)
* rather than leaving it as-is. A plain value (string, number, a map's
* own plain object, ...) is returned unchanged.
**/
#toPlainJSON(value: unknown): unknown {
if (value === null || value === undefined) {
return value;
}
if (Array.isArray(value)) {
return value.map((item) => this.#toPlainJSON(item));
}
const asAny = value as any;
if (typeof asAny.toJSON === "function") {
return this.#toPlainJSON(asAny.toJSON());
}
return value;
}
toString() {
return JSON.stringify(this);
}
static get Fields() {
return {
nullableFloatWithDefault: "nullableFloatWithDefault",
nullableFloatWithoutDefault: "nullableFloatWithoutDefault",
};
}
/**
* Creates an instance of MyFloatClassDto, and possibleDtoObject
* needs to satisfy the type requirement fully, otherwise typescript compile would
* be complaining.
**/
static from(possibleDtoObject: MyFloatClassDtoType) {
return new MyFloatClassDto(possibleDtoObject);
}
/**
* Creates an instance of MyFloatClassDto, and partialDtoObject
* needs to satisfy the type, but partially, and rest of the content would
* be constructed according to data types and nullability.
**/
static with(partialDtoObject: PartialDeep<MyFloatClassDtoType>) {
return new MyFloatClassDto(partialDtoObject);
}
copyWith(
partial: PartialDeep<MyFloatClassDtoType>,
): InstanceType<typeof MyFloatClassDto> {
return new MyFloatClassDto({ ...this.toJSON(), ...partial });
}
clone(): InstanceType<typeof MyFloatClassDto> {
return new MyFloatClassDto(this.toJSON());
}
}
export abstract class MyFloatClassDtoFactory {
abstract create(data: unknown): MyFloatClassDto;
}
/**
* The base type definition for myFloatClassDto
**/
export type MyFloatClassDtoType = {
/**
*
* @type {number}
**/
nullableFloatWithDefault?: number;
/**
*
* @type {number}
**/
nullableFloatWithoutDefault?: number;
};
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace MyFloatClassDtoType {}

Emi also supports float32, but float64 is the default for decimals.