-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmain.go
221 lines (186 loc) · 4.82 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
//go:build generate
// +build generate
package main
import (
_ "embed"
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
"net/http"
"os"
"sort"
"strings"
"github.com/hashicorp/aws-sdk-go-base/v2/internal/generate/common"
"github.com/hashicorp/aws-sdk-go-base/v2/internal/slices"
)
type PartitionDatum struct {
ID string
Name string
DNSSuffix string
RegionRegex string
Regions []RegionDatum
Services []ServiceDatum
}
type RegionDatum struct {
ID string
Description string
}
type ServiceDatum struct {
ID string
}
type TemplateData struct {
Partitions []PartitionDatum
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, "\tmain.go <aws-sdk-go-v2-endpoints-json-url>\n\n")
}
func main() {
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) < 1 {
flag.Usage()
os.Exit(2)
}
inputURL := args[0]
filename := `endpoints_gen.go`
target := map[string]any{}
g := common.NewGenerator()
g.Infof("Generating endpoints/%s", filename)
if err := readHTTPJSON(inputURL, &target); err != nil {
g.Fatalf("error reading JSON from %s: %s", inputURL, err)
}
td := TemplateData{}
templateFuncMap := template.FuncMap{
// IDToTitle splits a '-' or '.' separated string and returns a string with each part title cased.
"IDToTitle": func(s string) (string, error) {
parts := strings.Split(s, "-")
if len(parts) == 1 {
parts = strings.Split(s, ".")
}
return strings.Join(slices.ApplyToAll(parts, func(s string) string {
return common.FirstUpper(s)
}), ""), nil
},
}
if version, ok := target["version"].(float64); ok {
if version != 3.0 {
g.Fatalf("unsupported endpoints document version: %d", int(version))
}
} else {
g.Fatalf("can't parse endpoints document version")
}
/*
See https://github.com/aws/aws-sdk-go/blob/main/aws/endpoints/v3model.go.
e.g.
{
"partitions": [{
"partition": "aws",
"partitionName": "AWS Standard",
"regions" : {
"af-south-1" : {
"description" : "Africa (Cape Town)"
},
...
},
"services" : {
"access-analyzer" : {
"endpoints" : {
"af-south-1" : { },
...
},
},
...
},
...
}, ...]
}
*/
if partitions, ok := target["partitions"].([]any); ok {
for _, partition := range partitions {
if partition, ok := partition.(map[string]any); ok {
partitionDatum := PartitionDatum{}
if id, ok := partition["partition"].(string); ok {
partitionDatum.ID = id
}
if name, ok := partition["partitionName"].(string); ok {
partitionDatum.Name = name
}
if dnsSuffix, ok := partition["dnsSuffix"].(string); ok {
partitionDatum.DNSSuffix = dnsSuffix
}
if regionRegex, ok := partition["regionRegex"].(string); ok {
partitionDatum.RegionRegex = regionRegex
}
if regions, ok := partition["regions"].(map[string]any); ok {
for id, region := range regions {
regionDatum := RegionDatum{
ID: id,
}
if region, ok := region.(map[string]any); ok {
if description, ok := region["description"].(string); ok {
regionDatum.Description = description
}
}
partitionDatum.Regions = append(partitionDatum.Regions, regionDatum)
}
}
if services, ok := partition["services"].(map[string]any); ok {
for id := range services {
serviceDatum := ServiceDatum{
ID: id,
}
partitionDatum.Services = append(partitionDatum.Services, serviceDatum)
}
}
td.Partitions = append(td.Partitions, partitionDatum)
}
}
}
sort.SliceStable(td.Partitions, func(i, j int) bool {
return td.Partitions[i].ID < td.Partitions[j].ID
})
for i := 0; i < len(td.Partitions); i++ {
sort.SliceStable(td.Partitions[i].Regions, func(j, k int) bool {
return td.Partitions[i].Regions[j].ID < td.Partitions[i].Regions[k].ID
})
}
for i := 0; i < len(td.Partitions); i++ {
sort.SliceStable(td.Partitions[i].Services, func(j, k int) bool {
return td.Partitions[i].Services[j].ID < td.Partitions[i].Services[k].ID
})
}
d := g.NewGoFileDestination(filename)
if err := d.WriteTemplate("endpoints", tmpl, td, templateFuncMap); err != nil {
g.Fatalf("error generating endpoint resolver: %s", err)
}
if err := d.Write(); err != nil {
g.Fatalf("generating file (%s): %s", filename, err)
}
}
func readHTTPJSON(url string, to any) error {
r, err := http.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
return decodeFromReader(r.Body, to)
}
func decodeFromReader(r io.Reader, to any) error {
dec := json.NewDecoder(r)
for {
if err := dec.Decode(to); err == io.EOF {
break
} else if err != nil {
return err
}
}
return nil
}
//go:embed output.go.gtpl
var tmpl string