/
udwGoTypeMarshal.go
56 lines (50 loc) · 1.49 KB
/
udwGoTypeMarshal.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
package udwGoWriter
import (
"bytes"
"github.com/tachyon-protocol/udw/udwGoSource/udwGoWriter/udwGoTypeMarshal"
"path"
"reflect"
)
func MustWriteObjectToMainPackage(obj interface{}) string {
return udwGoTypeMarshal.MustWriteObjectToMainPackage(obj)
}
func (file *GoFileContext) MustWriteObject(obj interface{}) string {
buf := &bytes.Buffer{}
file.MustWriteObjectToBuf(obj, buf)
return buf.String()
}
func (file *GoFileContext) MustWriteObjectToSelfBuf(obj interface{}) {
file.MustWriteObjectToBuf(obj, &file.Buf)
return
}
func (file *GoFileContext) MustWriteObjectToBuf(obj interface{}, _buf *bytes.Buffer) {
udwGoTypeMarshal.MustGoTypeMarshalWithBuf(udwGoTypeMarshal.MustGoTypeMarshalContext{
AddTypeNameWithImportPackage: func(pkgPath string, name string, buf *bytes.Buffer) {
if file.pkgImportPath == pkgPath {
buf.WriteString(name)
return
} else {
file.AddImportPath(pkgPath)
buf.WriteString(path.Base(pkgPath) + "." + name)
return
}
},
}, obj, _buf)
return
}
func (file *GoFileContext) MustWriteTypeByReflect(typ reflect.Type) string {
buf := &bytes.Buffer{}
udwGoTypeMarshal.WriteReflectTypeNameToGoFile(udwGoTypeMarshal.MustGoTypeMarshalContext{
AddTypeNameWithImportPackage: func(pkgPath string, name string, buf *bytes.Buffer) {
if file.pkgImportPath == pkgPath {
buf.WriteString(name)
return
} else {
file.AddImportPath(pkgPath)
buf.WriteString(path.Base(pkgPath) + "." + name)
return
}
},
}, typ, buf)
return buf.String()
}