Skip to content

Commit 6dbfda7

Browse files
authored
add crosslink script generator (#2616)
1 parent 22b7ca9 commit 6dbfda7

File tree

1 file changed

+197
-0
lines changed

1 file changed

+197
-0
lines changed
+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
// package main is the cmd
2+
package main
3+
4+
import (
5+
"encoding/json"
6+
"flag"
7+
"fmt"
8+
"io"
9+
"io/ioutil"
10+
"os"
11+
"path"
12+
"strings"
13+
)
14+
15+
type shapeType string
16+
17+
const (
18+
shapeTypeService shapeType = "service"
19+
// ...
20+
)
21+
22+
type smithyModel struct {
23+
Shapes map[string]smithyShape `json:"shapes"`
24+
}
25+
26+
func (m *smithyModel) Service() *serviceShape {
27+
for _, s := range m.Shapes {
28+
if s.typ == shapeTypeService {
29+
return s.asService()
30+
}
31+
}
32+
panic("no service shape found")
33+
}
34+
35+
type smithyShape struct {
36+
typ shapeType
37+
raw []byte
38+
}
39+
40+
func (s *smithyShape) UnmarshalJSON(p []byte) error {
41+
var head = struct {
42+
Type shapeType `json:"type"`
43+
}{}
44+
if err := json.Unmarshal(p, &head); err != nil {
45+
return nil
46+
}
47+
48+
s.typ = head.Type
49+
s.raw = p
50+
return nil
51+
}
52+
53+
func (s *smithyShape) asService() *serviceShape {
54+
var shape serviceShape
55+
if err := json.Unmarshal(s.raw, &shape); err != nil {
56+
panic(err)
57+
}
58+
59+
return &shape
60+
}
61+
62+
type serviceShape struct {
63+
Version string `json:"version"`
64+
Traits smithyTraits `json:"traits"`
65+
}
66+
67+
type smithyTraits map[string]json.RawMessage
68+
69+
func (ts smithyTraits) ServiceTrait() (*serviceTrait, bool) {
70+
const traitID = "aws.api#service"
71+
72+
raw, ok := ts[traitID]
73+
if !ok {
74+
return nil, false
75+
}
76+
77+
var v serviceTrait
78+
if err := json.Unmarshal(raw, &v); err != nil {
79+
panic(err)
80+
}
81+
82+
return &v, true
83+
}
84+
85+
type serviceTrait struct {
86+
SdkID string `json:"sdkId"`
87+
DocID string `json:"docId"`
88+
}
89+
90+
func loadModels(dir string) ([]smithyModel, error) {
91+
files, err := ioutil.ReadDir(dir)
92+
if err != nil {
93+
return nil, fmt.Errorf("read models dir: %w", err)
94+
}
95+
96+
var models []smithyModel
97+
for _, file := range files {
98+
f, err := os.Open(path.Join(dir, file.Name()))
99+
if err != nil {
100+
return nil, fmt.Errorf("open %s: %w", file.Name(), err)
101+
}
102+
103+
p, err := io.ReadAll(f)
104+
if err != nil {
105+
return nil, fmt.Errorf("read %s: %w", file.Name(), err)
106+
}
107+
108+
var model smithyModel
109+
if err := json.Unmarshal(p, &model); err != nil {
110+
return nil, fmt.Errorf("unmarshal %s: %w", file.Name(), err)
111+
}
112+
113+
models = append(models, model)
114+
}
115+
return models, nil
116+
}
117+
118+
func mtoc(model *smithyModel) (docID, pkgName string) {
119+
service := model.Service()
120+
serviceTrait, ok := service.Traits.ServiceTrait()
121+
if !ok {
122+
panic("no service trait")
123+
}
124+
125+
pkgName = topkg(serviceTrait.SdkID)
126+
docID = serviceTrait.DocID
127+
if docID == "" {
128+
docID = fmt.Sprintf("%s-%s", normalize2(serviceTrait.SdkID), service.Version)
129+
}
130+
return
131+
}
132+
133+
func topkg(sdkID string) (pkg string) {
134+
pkg = strings.ToLower(sdkID)
135+
pkg = strings.Replace(pkg, " ", "", -1)
136+
pkg = strings.Replace(pkg, "-", "", -1)
137+
return
138+
}
139+
140+
func normalize2(sdkID string) (v string) {
141+
v = strings.ToLower(sdkID)
142+
v = strings.Replace(v, " ", "-", -1)
143+
return
144+
}
145+
146+
func main() {
147+
var modelsDir string
148+
flag.StringVar(&modelsDir, "modelsDir", "", "full path to aws-models directory")
149+
flag.Parse()
150+
if modelsDir == "" {
151+
panic("modelsDir not set")
152+
}
153+
154+
models, err := loadModels(modelsDir)
155+
if err != nil {
156+
panic(err)
157+
}
158+
159+
docToPkg := map[string]string{}
160+
for _, model := range models {
161+
doc, pkg := mtoc(&model)
162+
docToPkg[doc] = pkg
163+
}
164+
165+
var mapping string
166+
for k, v := range docToPkg {
167+
mapping += fmt.Sprintf(" '%s': '%s',\n", k, v)
168+
}
169+
170+
page := `<!DOCTYPE html>
171+
<html>
172+
<script type="text/javascript">
173+
(function() {
174+
var docToPkg = {
175+
` + mapping + ` };
176+
177+
var query = new URL(window.location.href).searchParams;
178+
var docId = query.get('doc');
179+
var operation = query.get('operation');
180+
if (docId === null || operation === null) {
181+
window.location = 'https://pkg.go.dev/github.com/aws/aws-sdk-go-v2';
182+
return;
183+
}
184+
185+
var service = docToPkg[docId];
186+
if (service === undefined) {
187+
window.location = 'https://pkg.go.dev/github.com/aws/aws-sdk-go-v2';
188+
return;
189+
}
190+
191+
window.location = 'https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/' + service + '#Client.' + operation;
192+
})();
193+
</script>
194+
</html>`
195+
196+
fmt.Println(page)
197+
}

0 commit comments

Comments
 (0)