forked from globusdigital/deep-copy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
160 lines (126 loc) · 3.11 KB
/
main.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
package main
import (
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/globusdigital/deep-copy/deepcopy"
"golang.org/x/tools/go/packages"
)
var (
pointerReceiverF = flag.Bool("pointer-receiver", false, "the generated receiver type")
maxDepthF = flag.Int("maxdepth", 0, "max depth of deep copying")
methodF = flag.String("method", "DeepCopy", "deep copy method name")
typesF typesVal
skipsF skipsVal
outputF outputVal
)
type typesVal []string
func (f *typesVal) String() string {
return strings.Join(*f, ",")
}
func (f *typesVal) Set(v string) error {
*f = append(*f, v)
return nil
}
type skipsVal deepcopy.SkipLists
func (f *skipsVal) String() string {
parts := make([]string, 0, len(*f))
for _, m := range *f {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
parts = append(parts, strings.Join(keys, ","))
}
return strings.Join(parts, ",")
}
func (f *skipsVal) Set(v string) error {
parts := strings.Split(v, ",")
set := make(map[string]struct{}, len(parts))
for _, p := range parts {
set[p] = struct{}{}
}
*f = append(*f, set)
return nil
}
type outputVal struct {
file *os.File
name string
}
func (f *outputVal) String() string {
return f.name
}
func (f *outputVal) Set(v string) error {
if v == "-" || v == "" {
f.name = "stdout"
if f.file != nil {
_ = f.file.Close()
}
f.file = nil
return nil
}
file, err := os.OpenFile(v, os.O_RDWR|os.O_CREATE, 0o666)
if err != nil {
return fmt.Errorf("opening file: %v", v)
}
f.name = v
f.file = file
return nil
}
func (f *outputVal) Open() (io.WriteCloser, error) {
if f.file == nil {
f.file = os.Stdout
} else {
err := f.file.Truncate(0)
if err != nil {
return nil, err
}
}
return f.file, nil
}
func init() {
flag.Var(&typesF, "type", "the concrete type. Multiple flags can be specified")
flag.Var(&skipsF, "skip", "comma-separated field/slice/map selectors to shallow copy. Multiple flags can be specified")
flag.Var(&outputF, "o", "the output file to write to. Defaults to STDOUT")
}
func main() {
flag.Parse()
if len(typesF) == 0 || typesF[0] == "" {
log.Fatalln("no type given")
}
if flag.NArg() != 1 {
log.Fatalln("No package path given")
}
sl := deepcopy.SkipLists(skipsF)
generator := deepcopy.NewGenerator(*pointerReceiverF, *methodF, sl, *maxDepthF)
output, err := outputF.Open()
if err != nil {
log.Fatalln("Error initializing output file:", err)
}
err = run(generator, output, flag.Args()[0], typesF)
if err != nil {
log.Fatalln("Error generating deep copy method:", err)
}
output.Close()
}
func run(
g deepcopy.Generator, w io.Writer, path string, types typesVal,
) error {
packages, err := load(path)
if err != nil {
return fmt.Errorf("loading package: %v", err)
}
if len(packages) == 0 {
return errors.New("no package found")
}
return g.Generate(w, types, packages[0])
}
func load(patterns string) ([]*packages.Package, error) {
return packages.Load(&packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedDeps | packages.NeedImports,
}, patterns)
}