1// +build go1.5,deprecated
2
3package main
4
5import (
6	"bytes"
7	"go/format"
8	"io"
9	"os"
10	"path/filepath"
11	"sort"
12	"strings"
13	"text/template"
14
15	"github.com/aws/aws-sdk-go/private/model/api"
16)
17
18type pkg struct {
19	oldAPI     *api.API
20	newAPI     *api.API
21	shapes     map[string]*shapentry
22	operations map[string]*opentry
23}
24
25type shapentry struct {
26	oldShape *api.Shape
27	newShape *api.Shape
28}
29
30type opentry struct {
31	oldName string
32	newName string
33}
34
35type packageRenames struct {
36	Shapes     map[string]string
37	Operations map[string]string
38	Fields     map[string]string
39}
40
41var exportMap = map[string]*packageRenames{}
42
43func generateRenames(w io.Writer) error {
44	tmpl, err := template.New("renames").Parse(t)
45	if err != nil {
46		return err
47	}
48
49	out := bytes.NewBuffer(nil)
50	if err = tmpl.Execute(out, exportMap); err != nil {
51		return err
52	}
53
54	b, err := format.Source(bytes.TrimSpace(out.Bytes()))
55	if err != nil {
56		return err
57	}
58
59	_, err = io.Copy(w, bytes.NewReader(b))
60	return err
61}
62
63const t = `
64package rename
65
66// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
67
68var renamedPackages = map[string]*packageRenames{
69	{{ range $key, $entry := . }}"{{ $key }}": &packageRenames{
70		operations: map[string]string{
71			{{ range $old, $new := $entry.Operations }}"{{ $old }}": "{{ $new }}",
72			{{ end }}
73		},
74		shapes: map[string]string{
75			{{ range $old, $new := $entry.Shapes }}"{{ $old }}": "{{ $new }}",
76			{{ end }}
77		},
78		fields: map[string]string{
79			{{ range $old, $new := $entry.Fields }}"{{ $old }}": "{{ $new }}",
80			{{ end }}
81		},
82	},
83	{{ end }}
84}
85`
86
87func (p *pkg) buildRenames() {
88	pkgName := "github.com/aws/aws-sdk-go/service/" + p.oldAPI.PackageName()
89	if exportMap[pkgName] == nil {
90		exportMap[pkgName] = &packageRenames{map[string]string{}, map[string]string{}, map[string]string{}}
91	}
92	ifacename := "github.com/aws/aws-sdk-go/service/" + p.oldAPI.PackageName() + "/" +
93		p.oldAPI.InterfacePackageName()
94	if exportMap[ifacename] == nil {
95		exportMap[ifacename] = &packageRenames{map[string]string{}, map[string]string{}, map[string]string{}}
96	}
97
98	for _, entry := range p.operations {
99		if entry.oldName != entry.newName {
100			pkgNames := []string{pkgName, ifacename}
101			for _, p := range pkgNames {
102				exportMap[p].Operations[entry.oldName] = entry.newName
103				exportMap[p].Operations[entry.oldName+"Request"] = entry.newName + "Request"
104				exportMap[p].Operations[entry.oldName+"Pages"] = entry.newName + "Pages"
105			}
106		}
107	}
108
109	for _, entry := range p.shapes {
110		if entry.oldShape.Type == "structure" {
111			if entry.oldShape.ShapeName != entry.newShape.ShapeName {
112				exportMap[pkgName].Shapes[entry.oldShape.ShapeName] = entry.newShape.ShapeName
113			}
114
115			for _, n := range entry.oldShape.MemberNames() {
116				for _, m := range entry.newShape.MemberNames() {
117					if n != m && strings.EqualFold(n, m) {
118						exportMap[pkgName].Fields[n] = m
119					}
120				}
121			}
122		}
123	}
124}
125
126func load(file string) *pkg {
127	p := &pkg{&api.API{}, &api.API{}, map[string]*shapentry{}, map[string]*opentry{}}
128
129	p.oldAPI.Attach(file)
130	p.oldAPI.Setup()
131
132	p.newAPI.Attach(file)
133	p.newAPI.Setup()
134
135	for _, name := range p.oldAPI.OperationNames() {
136		p.operations[strings.ToLower(name)] = &opentry{oldName: name}
137	}
138
139	for _, name := range p.newAPI.OperationNames() {
140		p.operations[strings.ToLower(name)].newName = name
141	}
142
143	for _, shape := range p.oldAPI.ShapeList() {
144		p.shapes[strings.ToLower(shape.ShapeName)] = &shapentry{oldShape: shape}
145	}
146
147	for _, shape := range p.newAPI.ShapeList() {
148		if _, ok := p.shapes[strings.ToLower(shape.ShapeName)]; !ok {
149			panic("missing shape " + shape.ShapeName)
150		}
151		p.shapes[strings.ToLower(shape.ShapeName)].newShape = shape
152	}
153
154	return p
155}
156
157var excludeServices = map[string]struct{}{
158	"simpledb":     {},
159	"importexport": {},
160}
161
162func main() {
163	files, _ := filepath.Glob("../../apis/*/*/api-2.json")
164
165	sort.Strings(files)
166
167	// Remove old API versions from list
168	m := map[string]bool{}
169	for i := range files {
170		idx := len(files) - 1 - i
171		parts := strings.Split(files[idx], string(filepath.Separator))
172		svc := parts[len(parts)-3] // service name is 2nd-to-last component
173
174		if m[svc] {
175			files[idx] = "" // wipe this one out if we already saw the service
176		}
177		m[svc] = true
178	}
179
180	for i := range files {
181		file := files[i]
182		if file == "" { // empty file
183			continue
184		}
185
186		if g := load(file); g != nil {
187			if _, ok := excludeServices[g.oldAPI.PackageName()]; !ok {
188				g.buildRenames()
189			}
190		}
191	}
192
193	outfile, err := os.Create("rename/renames.go")
194	if err != nil {
195		panic(err)
196	}
197	if err := generateRenames(outfile); err != nil {
198		panic(err)
199	}
200}
201