forked from OneOfOne/struct2ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2ts_test.go
More file actions
137 lines (131 loc) · 3.71 KB
/
s2ts_test.go
File metadata and controls
137 lines (131 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package struct2ts_test
import (
"os"
"time"
"github.com/jonau/struct2ts"
)
type OtherStruct struct {
T time.Time `json:"t,omitempty"`
}
type ComplexStruct struct {
S string `json:"s,omitempty"`
I int `json:"i,omitempty"`
F float64 `json:"f,omitempty"`
TS *int64 `json:"ts,omitempty" ts:"date,null"`
T time.Time `json:"t,omitempty"` // automatically handled
NullOther *OtherStruct `json:"o,omitempty"`
NoNullOther *OtherStruct `json:"nno,omitempty" ts:",no-null"`
}
func ExampleComplexStruct() {
s2ts := struct2ts.New(nil)
s2ts.Add(ComplexStruct{})
s2ts.RenderTo(os.Stdout)
// Output:
// // helpers
// const maxUnixTSInSeconds = 9999999999;
//
// function ParseDate(d: Date | number | string): Date {
// if (d instanceof Date) return d;
// if (typeof d === 'number') {
// if (d > maxUnixTSInSeconds) return new Date(d);
// return new Date(d * 1000); // go ts
// }
// return new Date(d);
// }
//
// function ParseNumber(v: number | string, isInt = false): number {
// if (!v) return 0;
// if (typeof v === 'number') return v;
// return (isInt ? parseInt(v) : parseFloat(v)) || 0;
// }
//
// function FromArray<T>(Ctor: { new(v: any): T }, data?: any[] | any, def = null): T[] | null {
// if (!data || !Object.keys(data).length) return def;
// const d = Array.isArray(data) ? data : [data];
// return d.map((v: any) => new Ctor(v));
// }
//
// function ToObject(o: any, typeOrCfg: any = {}, child = false): any {
// if (!o) return null;
// if (typeof o.toObject === 'function' && child) return o.toObject();
//
// switch (typeof o) {
// case 'string':
// return typeOrCfg === 'number' ? ParseNumber(o) : o;
// case 'boolean':
// case 'number':
// return o;
// }
//
// if (o instanceof Date) {
// return typeOrCfg === 'string' ? o.toISOString() : Math.floor(o.getTime() / 1000);
// }
//
// if (Array.isArray(o)) return o.map((v: any) => ToObject(v, typeOrCfg, true));
//
// const d: any = {};
//
// for (const k of Object.keys(o)) {
// const v: any = o[k];
// if (!v) continue;
// d[k] = ToObject(v, typeOrCfg[k] || '', true);
// }
//
// return d;
// }
//
// // classes
// // struct2ts:github.com/OneOfOne/struct2ts_test.ComplexStructOtherStruct
// class ComplexStructOtherStruct {
// t: Date;
//
// constructor(data?: any) {
// const d: any = (data && typeof data === 'object') ? ToObject(data) : {};
// this.t = ('t' in d) ? ParseDate(d.t) : new Date();
// }
//
// toObject(): any {
// const cfg: any = {};
// cfg.t = 'string';
// return ToObject(this, cfg);
// }
// }
//
// // struct2ts:github.com/OneOfOne/struct2ts_test.ComplexStruct
// class ComplexStruct {
// s: string;
// i: number;
// f: number;
// ts: Date | null;
// t: Date;
// o: ComplexStructOtherStruct | null;
// nno: ComplexStructOtherStruct;
//
// constructor(data?: any) {
// const d: any = (data && typeof data === 'object') ? ToObject(data) : {};
// this.s = ('s' in d) ? d.s as string : '';
// this.i = ('i' in d) ? d.i as number : 0;
// this.f = ('f' in d) ? d.f as number : 0;
// this.ts = ('ts' in d) ? ParseDate(d.ts) : null;
// this.t = ('t' in d) ? ParseDate(d.t) : new Date();
// this.o = ('o' in d) ? new ComplexStructOtherStruct(d.o) : null;
// this.nno = new ComplexStructOtherStruct(d.nno);
// }
//
// toObject(): any {
// const cfg: any = {};
// cfg.i = 'number';
// cfg.f = 'number';
// cfg.t = 'string';
// return ToObject(this, cfg);
// }
// }
//
// // exports
// export ComplexStructOtherStruct;
// export ComplexStruct;
// export ParseDate;
// export ParseNumber;
// export FromArray;
// export ToObject;
}