-
Notifications
You must be signed in to change notification settings - Fork 15
/
struct.go
165 lines (140 loc) · 3.59 KB
/
struct.go
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package struct2ts
import (
"errors"
"fmt"
"io"
"reflect"
)
var zeroValues = map[string]string{
"number": "0",
"boolean": "false",
"string": "''",
"object": "{}",
"map": "{}",
"array": "[]",
}
type Struct struct {
Name string
Fields []*Field
t reflect.Type
}
func (s *Struct) RenderTo(opts *Options, w io.Writer) (err error) {
if _, err = fmt.Fprintf(w, "// struct2ts:%s.%s\n", s.t.PkgPath(), s.Name); err != nil {
return
}
if opts.InterfaceOnly {
if opts.ES6 { // no interfaces in js
return
}
if !opts.NoExports {
fmt.Fprintf(w, "export ")
}
_, err = fmt.Fprintf(w, "interface %s {\n", s.Name)
} else {
_, err = fmt.Fprintf(w, "class %s {\n", s.Name)
}
if err != nil {
return
}
if !opts.ES6 {
if err = s.RenderFields(opts, w); err != nil {
return
}
}
if err = s.RenderConstructor(opts, w); err != nil {
return
}
if err = s.RenderToObject(opts, w); err != nil {
return
}
if err = s.RenderCustom(opts, w); err != nil {
return
}
_, err = fmt.Fprint(w, "}")
return
}
type CustomTypescript interface {
RenderCustomTypescript(w io.Writer) (err error)
}
func (s *Struct) RenderCustom(opts *Options, w io.Writer) (err error) {
ww := newTabScanner(w, opts.indents[1])
ctit := reflect.TypeOf((*CustomTypescript)(nil)).Elem()
var implementingType reflect.Type = nil
if s.t.Implements(ctit) {
implementingType = ctit
}
if reflect.PtrTo(s.t).Implements(ctit) {
implementingType = reflect.PtrTo(s.t)
}
if implementingType != nil {
m, ok := implementingType.MethodByName("RenderCustomTypescript")
if !ok {
return errors.New("couldn't get method RenderCustomTypescript")
}
_, err = fmt.Fprintf(ww, "\n")
o := reflect.New(s.t)
if implementingType.Kind() != reflect.Ptr {
o = o.Elem()
}
wv := reflect.ValueOf(ww)
r := m.Func.Call([]reflect.Value{o, wv})
if len(r) > 0 && !r[0].IsNil() {
switch r0t := r[0].Interface().(type) {
case error:
return r0t
}
}
_, err = fmt.Fprintf(w, "\n")
}
return
}
func (s *Struct) RenderFields(opts *Options, w io.Writer) (err error) {
for _, f := range s.Fields {
if err = f.RenderTopLevel(w, opts); err != nil {
return
}
}
return
}
func (s *Struct) RenderConstructor(opts *Options, w io.Writer) (err error) {
if opts.NoConstructor || opts.InterfaceOnly {
return
}
if opts.ES6 {
fmt.Fprintf(w, "%sconstructor(data = null) {\n", opts.indents[1])
fmt.Fprintf(w, "%sconst d = (data && typeof data === 'object') ? ToObject(data) : {};\n", opts.indents[2])
} else {
fmt.Fprintf(w, "\n%sconstructor(data?: any) {\n", opts.indents[1])
fmt.Fprintf(w, "%sconst d: any = (data && typeof data === 'object') ? ToObject(data) : {};\n", opts.indents[2])
}
for _, f := range s.Fields {
if err = f.RenderCtor(w, opts); err != nil {
return
}
}
_, err = fmt.Fprintf(w, "%s}\n", opts.indents[1])
return
}
func (s *Struct) RenderToObject(opts *Options, w io.Writer) (err error) {
if opts.NoToObject || opts.InterfaceOnly {
return
}
if opts.ES6 {
fmt.Fprintf(w, "\n%stoObject() {\n", opts.indents[1])
fmt.Fprintf(w, "%sconst cfg = {};\n", opts.indents[2])
} else {
fmt.Fprintf(w, "\n%stoObject(): any {\n", opts.indents[1])
fmt.Fprintf(w, "%sconst cfg: any = {};\n", opts.indents[2])
}
for _, f := range s.Fields {
t := f.Type(opts, true)
switch {
case t == "Date" && f.TsType != "number":
fmt.Fprintf(w, "%scfg.%s = 'string';\n", opts.indents[2], f.Name)
case t == "number":
fmt.Fprintf(w, "%scfg.%s = 'number';\n", opts.indents[2], f.Name)
}
}
_, err = fmt.Fprintf(w, "%sreturn ToObject(this, cfg);\n%s}\n", opts.indents[2], opts.indents[1])
return
}