This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
forked from istio/istio
/
generator.go
119 lines (99 loc) · 3.24 KB
/
generator.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
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package inventory is used to generate the mixer adapter inventory source
// file.
package inventory // import "istio.io/istio/mixer/tools/codegen/pkg/inventory"
import (
"bytes"
"fmt"
"go/format"
"io"
"sort"
"text/template"
"golang.org/x/tools/imports"
)
var inventoryTmpl = `// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// THIS FILE IS AUTOMATICALLY GENERATED.
package adapter
import(
{{range .}}{{.Package}} "{{.Path}}"; {{end}}
adptr "istio.io/istio/mixer/pkg/adapter"
)
// Inventory returns the inventory of all available adapters.
func Inventory() []adptr.InfoFn {
return []adptr.InfoFn{
{{range .}}{{.Package}}.GetInfo,
{{end}}
}
}
`
type model []Import
// Import holds the full import path and short package name
// of an adapter that will be part of the generated inventory.
type Import struct {
Path string
Package string
}
// Len is part of sort.Interface.
func (m model) Len() int {
return len(m)
}
// Swap is part of sort.Interface.
func (m model) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
// Less is part of sort.Interface.
func (m model) Less(i, j int) bool {
return m[i].Package < m[j].Package
}
// Generate takes a map (package -> import path) and generates the
// source code to use as the adapter inventory for Mixer.
func Generate(packageMap map[string]string, out io.Writer) error {
tmpl, err := template.New("InventoryTmpl").Parse(inventoryTmpl)
if err != nil {
return fmt.Errorf("could not load template: %v", err)
}
m := make(model, 0, len(packageMap))
for pkg, path := range packageMap {
m = append(m, Import{path, pkg})
}
sort.Sort(m)
buf := new(bytes.Buffer)
if err = tmpl.Execute(buf, m); err != nil {
return fmt.Errorf("could not generate inventory code: %v (%s)", err, buf.Bytes())
}
fmtd, err := format.Source(buf.Bytes())
if err != nil {
return fmt.Errorf("could not format generated code: %v (%s)", err, buf.Bytes())
}
imports.LocalPrefix = "istio.io"
imptd, err := imports.Process("", fmtd, &imports.Options{FormatOnly: true, Comments: true})
if err != nil {
return fmt.Errorf("could not fix imports for generated code: %v", err)
}
_, err = out.Write(imptd)
return err
}