Skip to content

Commit

Permalink
add model instance func
Browse files Browse the repository at this point in the history
  • Loading branch information
peze committed May 13, 2024
1 parent 859cab7 commit b7e5e78
Show file tree
Hide file tree
Showing 3 changed files with 303 additions and 44 deletions.
98 changes: 91 additions & 7 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as querystring from 'querystring';
import { IncomingMessage, IncomingHttpHeaders, Agent as HttpAgent } from 'http';
import { Agent as HttpsAgent } from 'https';
import { Readable } from 'stream';
import { Readable, Writable } from 'stream';
import * as httpx from 'httpx';
import { parse } from 'url';
import { RetryOptions } from './retry';
import { BaseError } from './error';

type TeaDict = { [key: string]: string };
type TeaObject = { [key: string]: any };
Expand Down Expand Up @@ -183,27 +184,31 @@ function getValue(type: any, value: any): any {
return value;
}

export function toMap(value: any = undefined): any {
export function toMap(value: any = undefined, withouStream: boolean = false): any {
if (typeof value === 'undefined' || value == null) {
return null;
}

if (value instanceof Model) {
return value.toMap();
return value.toMap(withouStream);
}

// 如果是另一个版本的 tea-typescript 创建的 model,instanceof 会判断不通过
// 这里做一下处理
if (typeof value.toMap === 'function') {
return value.toMap();
return value.toMap(withouStream);
}

if (Array.isArray(value)) {
return value.map((item) => {
return toMap(item);
return toMap(item, withouStream);
})
}

if(withouStream && (value instanceof Readable || value instanceof Writable)) {
return null;
}

return value;
}

Expand All @@ -228,7 +233,15 @@ export class Model {
}));
}

toMap(): TeaObject {
validate(): void {}

copyWithoutStream<T extends Model>(): T {
const map: TeaObject = this.toMap(true);
const clz = <any>this.constructor;
return new clz(map);
}

toMap(withouStream: boolean = false): TeaObject {
const map: TeaObject = {};
const clz = <any>this.constructor;
const names = <TeaDict>clz.names();
Expand All @@ -238,10 +251,80 @@ export class Model {
if (typeof value === 'undefined' || value == null) {
return;
}
map[originName] = toMap(value);
map[originName] = toMap(value, withouStream);
}));
return map;
}

static validateRequired(key: string, value: any) {
if(value === null || typeof value === 'undefined') {
throw new BaseError({
code: 'SDK.ValidateError',
message: `${key} is required.`,
});
}
}

static validateMaxLength(key: string, value: any, max: number) {
if(value === null || typeof value === 'undefined') {
return;
}
if(value.length > max) {
throw new BaseError({
code: 'SDK.ValidateError',
message: `${key} is exceed max-length: ${max}.`,
});
}
}

static validateMinLength(key: string, value: any, min: number) {
if(value === null || typeof value === 'undefined') {
return;
}
if(value.length < min) {
throw new BaseError({
code: 'SDK.ValidateError',
message: `${key} is exceed min-length: ${min}.`,
});
}
}

static validateMaximum(key: string, value: number | undefined, max: number) {
if(value === null || typeof value === 'undefined') {
return;
}
if(value > max) {
throw new BaseError({
code: 'SDK.ValidateError',
message: `${key} cannot be greater than ${max}.`,
});
}
}

static validateMinimum(key: string, value: number | undefined, min: number) {
if(value === null || typeof value === 'undefined') {
return;
}
if(value < min) {
throw new BaseError({
code: 'SDK.ValidateError',
message: `${key} cannot be less than ${min}.`,
});
}
}

static validatePattern(key: string, value: any, val: string) {
if(value === null || typeof value === 'undefined') {
return;
}
const reg = new RegExp(val);
if(!reg.test(value)) {
throw new BaseError({
code: 'SDK.ValidateError',
message: `${key} is not match ${val}.`,
});
}
}
}


Expand Down Expand Up @@ -356,6 +439,7 @@ export function cast<T>(obj: any, t: T): T {
}
if (typeof type === 'string') {
if (type === 'Readable' ||
type === 'Writable' ||
type === 'map' ||
type === 'Buffer' ||
type === 'any' ||
Expand Down
2 changes: 1 addition & 1 deletion src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class ResponseError extends BaseError {
this.accessDeniedDetail = map.accessDeniedDetail;
if (this.data && this.data.statusCode) {
this.statusCode = Number(this.data.statusCode);
}
}
}
}

Expand Down
Loading

0 comments on commit b7e5e78

Please sign in to comment.