-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathwritefile.go
122 lines (102 loc) · 3.18 KB
/
writefile.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
package typegen
import (
"bytes"
"fmt"
"go/format"
"os"
"golang.org/x/xerrors"
)
// WriteTupleFileEncodersToFile is a convenience wrapper around Gen.WriteTupleEncodersToFile using
// default options.
func WriteTupleEncodersToFile(fname, pkg string, types ...interface{}) error {
return Gen{}.WriteTupleEncodersToFile(fname, pkg, types...)
}
// WriteTupleFileEncodersToFile generates array backed MarshalCBOR and UnmarshalCBOR implementations for the
// given types in the specified file, with the specified package name.
//
// The MarshalCBOR and UnmarshalCBOR implementations will marshal/unmarshal each type's fields as a
// fixed-length CBOR array of field values.
func (g Gen) WriteTupleEncodersToFile(fname, pkg string, types ...interface{}) error {
if g.SortTypeNames {
types = sortTypeNames(types)
}
buf := new(bytes.Buffer)
typeInfos := make([]*GenTypeInfo, len(types))
for i, t := range types {
gti, err := ParseTypeInfo(t)
if err != nil {
return xerrors.Errorf("failed to parse type info: %w", err)
}
typeInfos[i] = gti
}
if err := g.PrintHeaderAndUtilityMethods(buf, pkg, typeInfos); err != nil {
return xerrors.Errorf("failed to write header: %w", err)
}
for _, t := range typeInfos {
if err := g.GenTupleEncodersForType(t, buf); err != nil {
return xerrors.Errorf("failed to generate encoders: %w", err)
}
}
data, err := format.Source(buf.Bytes())
if err != nil {
return fmt.Errorf("failed to format: %w", err)
}
fi, err := os.Create(fname)
if err != nil {
return xerrors.Errorf("failed to open file: %w", err)
}
_, err = fi.Write(data)
if err != nil {
_ = fi.Close()
return err
}
_ = fi.Close()
return nil
}
// WriteMapFileEncodersToFile is a convenience wrapper around Gen.WriteMapEncodersToFile using
// default options.
func WriteMapEncodersToFile(fname, pkg string, types ...interface{}) error {
return Gen{}.WriteMapEncodersToFile(fname, pkg, types...)
}
// WriteMapFileEncodersToFile generates map backed MarshalCBOR and UnmarshalCBOR implementations for
// the given types in the specified file, with the specified package name.
//
// The MarshalCBOR and UnmarshalCBOR implementations will marshal/unmarshal each type's fields as a
// map of field names to field values.
func (g Gen) WriteMapEncodersToFile(fname, pkg string, types ...interface{}) error {
if g.SortTypeNames {
types = sortTypeNames(types)
}
buf := new(bytes.Buffer)
typeInfos := make([]*GenTypeInfo, len(types))
for i, t := range types {
gti, err := ParseTypeInfo(t)
if err != nil {
return xerrors.Errorf("failed to parse type info: %w", err)
}
typeInfos[i] = gti
}
if err := g.PrintHeaderAndUtilityMethods(buf, pkg, typeInfos); err != nil {
return xerrors.Errorf("failed to write header: %w", err)
}
for i, t := range typeInfos {
if err := g.GenMapEncodersForType(t, buf); err != nil {
return xerrors.Errorf("%T (%s) failed to generate encoders: %w", types[i], t.Name, err)
}
}
data, err := format.Source(buf.Bytes())
if err != nil {
return err
}
fi, err := os.Create(fname)
if err != nil {
return xerrors.Errorf("failed to open file: %w", err)
}
_, err = fi.Write(data)
if err != nil {
_ = fi.Close()
return err
}
_ = fi.Close()
return nil
}