/
multi.go
112 lines (96 loc) · 3.49 KB
/
multi.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
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package plugin
import (
"fmt"
"strings"
"sync"
"go.uber.org/thriftrw/internal/concurrent"
"go.uber.org/thriftrw/plugin/api"
)
// MultiHandle wraps a collection of handles into a single handle.
type MultiHandle []Handle
// Name is the combined name of this plugin
func (mh MultiHandle) Name() string {
names := make([]string, 0, len(mh))
for _, h := range mh {
names = append(names, h.Name())
}
return fmt.Sprintf("MultiHandle{%v}", strings.Join(names, ", "))
}
// Close closes all Handles associated with this MultiHandle.
func (mh MultiHandle) Close() error {
return concurrent.Range(mh, func(_ int, h Handle) error {
return h.Close()
})
}
// ServiceGenerator returns a ServiceGenerator which calls into the
// ServiceGenerators of all plugins associated with this MultiHandle and
// consolidates their results.
func (mh MultiHandle) ServiceGenerator() ServiceGenerator {
msg := make(MultiServiceGenerator, 0, len(mh))
for _, h := range mh {
if sg := h.ServiceGenerator(); sg != nil {
msg = append(msg, sg)
}
}
return msg
}
// MultiServiceGenerator wraps a collection of ServiceGenerators into a single
// ServiceGenerator.
type MultiServiceGenerator []ServiceGenerator
// Handle returns a reference to the Handle that owns this ServiceGenerator.
func (msg MultiServiceGenerator) Handle() Handle {
mh := make(MultiHandle, len(msg))
for i, sg := range msg {
mh[i] = sg.Handle()
}
return mh
}
// Generate calls all the service generators associated with this plugin and
// consolidates their output.
//
// Any conflicts in the generated files will result in a failure.
func (msg MultiServiceGenerator) Generate(req *api.GenerateServiceRequest) (*api.GenerateServiceResponse, error) {
var (
lock sync.Mutex
files = make(map[string][]byte)
usedPaths = make(map[string]string) // path -> plugin name
)
err := concurrent.Range(msg, func(_ int, sg ServiceGenerator) error {
res, err := sg.Generate(req)
if err != nil {
return err
}
lock.Lock()
defer lock.Unlock()
pluginName := sg.Handle().Name()
for path, contents := range res.Files {
if takenBy, taken := usedPaths[path]; taken {
return fmt.Errorf("plugin conflict: cannot write file %q for plugin %q: "+
"plugin %q already wrote to that file", path, pluginName, takenBy)
}
usedPaths[path] = pluginName
files[path] = contents
}
return nil
})
return &api.GenerateServiceResponse{Files: files}, err
}