Skip to content

Emi integer data type

Example schema:

name: MyIntClass
fields:
- name: intWithDefault
default: 42
type: int
- name: plainInt
type: int
  • int maps to JavaScript number, limited to safe 32-bit values.
  • If no default provided, it defaults to 0.
  • int? allows null and undefined, with undefined as default.
import { type PartialDeep } from "./sdk/common/fetchx";
/**
* The base class definition for myIntClassDto
**/
export class MyIntClassDto {
/**
*
* @type {number}
**/
#intWithDefault: number = 42;
/**
*
* @returns {number}
**/
get intWithDefault() {
return this.#intWithDefault;
}
/**
*
* @type {number}
**/
set intWithDefault(value: number) {
const correctType = typeof value === "number";
const parsedValue = correctType ? value : Number(value);
if (!Number.isNaN(parsedValue)) {
this.#intWithDefault = parsedValue;
}
}
setIntWithDefault(value: number) {
this.intWithDefault = value;
return this;
}
/**
*
* @type {number}
**/
#plainInt: number = 0;
/**
*
* @returns {number}
**/
get plainInt() {
return this.#plainInt;
}
/**
*
* @type {number}
**/
set plainInt(value: number) {
const correctType = typeof value === "number";
const parsedValue = correctType ? value : Number(value);
if (!Number.isNaN(parsedValue)) {
this.#plainInt = parsedValue;
}
}
setPlainInt(value: number) {
this.plainInt = 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<MyIntClassDto>;
if (d.intWithDefault !== undefined) {
this.intWithDefault = d.intWithDefault;
}
if (d.plainInt !== undefined) {
this.plainInt = d.plainInt;
}
}
/**
* 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(): MyIntClassDtoType {
return {
intWithDefault: this.#toPlainJSON(this.#intWithDefault),
plainInt: this.#toPlainJSON(this.#plainInt),
} as MyIntClassDtoType;
}
/**
* 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 {
intWithDefault: "intWithDefault",
plainInt: "plainInt",
};
}
/**
* Creates an instance of MyIntClassDto, and possibleDtoObject
* needs to satisfy the type requirement fully, otherwise typescript compile would
* be complaining.
**/
static from(possibleDtoObject: MyIntClassDtoType) {
return new MyIntClassDto(possibleDtoObject);
}
/**
* Creates an instance of MyIntClassDto, 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<MyIntClassDtoType>) {
return new MyIntClassDto(partialDtoObject);
}
copyWith(
partial: PartialDeep<MyIntClassDtoType>,
): InstanceType<typeof MyIntClassDto> {
return new MyIntClassDto({ ...this.toJSON(), ...partial });
}
clone(): InstanceType<typeof MyIntClassDto> {
return new MyIntClassDto(this.toJSON());
}
}
export abstract class MyIntClassDtoFactory {
abstract create(data: unknown): MyIntClassDto;
}
/**
* The base type definition for myIntClassDto
**/
export type MyIntClassDtoType = {
/**
*
* @type {number}
**/
intWithDefault: number;
/**
*
* @type {number}
**/
plainInt: number;
};
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace MyIntClassDtoType {}

Nullable version:

name: MyIntClass
fields:
- name: nullableIntWithDefault
default: 7
type: int?
- name: nullableIntWithoutDefault
type: int?

Defaults to undefined, but you can assign 0, any number, or null.

import { type PartialDeep } from "./sdk/common/fetchx";
/**
* The base class definition for myIntClassDto
**/
export class MyIntClassDto {
/**
*
* @type {number}
**/
#nullableIntWithDefault?: number | null | undefined = 7;
/**
*
* @returns {number}
**/
get nullableIntWithDefault() {
return this.#nullableIntWithDefault;
}
/**
*
* @type {number}
**/
set nullableIntWithDefault(value: number | null | undefined) {
const correctType =
typeof value === "number" || value === undefined || value === null;
const parsedValue = correctType ? value : Number(value);
if (!Number.isNaN(parsedValue)) {
this.#nullableIntWithDefault = parsedValue;
}
}
setNullableIntWithDefault(value: number | null | undefined) {
this.nullableIntWithDefault = value;
return this;
}
/**
*
* @type {number}
**/
#nullableIntWithoutDefault?: number | null | undefined = undefined;
/**
*
* @returns {number}
**/
get nullableIntWithoutDefault() {
return this.#nullableIntWithoutDefault;
}
/**
*
* @type {number}
**/
set nullableIntWithoutDefault(value: number | null | undefined) {
const correctType =
typeof value === "number" || value === undefined || value === null;
const parsedValue = correctType ? value : Number(value);
if (!Number.isNaN(parsedValue)) {
this.#nullableIntWithoutDefault = parsedValue;
}
}
setNullableIntWithoutDefault(value: number | null | undefined) {
this.nullableIntWithoutDefault = 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<MyIntClassDto>;
if (d.nullableIntWithDefault !== undefined) {
this.nullableIntWithDefault = d.nullableIntWithDefault;
}
if (d.nullableIntWithoutDefault !== undefined) {
this.nullableIntWithoutDefault = d.nullableIntWithoutDefault;
}
}
/**
* 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(): MyIntClassDtoType {
return {
nullableIntWithDefault: this.#toPlainJSON(this.#nullableIntWithDefault),
nullableIntWithoutDefault: this.#toPlainJSON(
this.#nullableIntWithoutDefault,
),
} as MyIntClassDtoType;
}
/**
* 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 {
nullableIntWithDefault: "nullableIntWithDefault",
nullableIntWithoutDefault: "nullableIntWithoutDefault",
};
}
/**
* Creates an instance of MyIntClassDto, and possibleDtoObject
* needs to satisfy the type requirement fully, otherwise typescript compile would
* be complaining.
**/
static from(possibleDtoObject: MyIntClassDtoType) {
return new MyIntClassDto(possibleDtoObject);
}
/**
* Creates an instance of MyIntClassDto, 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<MyIntClassDtoType>) {
return new MyIntClassDto(partialDtoObject);
}
copyWith(
partial: PartialDeep<MyIntClassDtoType>,
): InstanceType<typeof MyIntClassDto> {
return new MyIntClassDto({ ...this.toJSON(), ...partial });
}
clone(): InstanceType<typeof MyIntClassDto> {
return new MyIntClassDto(this.toJSON());
}
}
export abstract class MyIntClassDtoFactory {
abstract create(data: unknown): MyIntClassDto;
}
/**
* The base type definition for myIntClassDto
**/
export type MyIntClassDtoType = {
/**
*
* @type {number}
**/
nullableIntWithDefault?: number;
/**
*
* @type {number}
**/
nullableIntWithoutDefault?: number;
};
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace MyIntClassDtoType {}

Besides int and int?, Emi also supports int32, int32?, int64, and int64? types.