File: load.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.49.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312,636 kB
  • sloc: makefile: 120
file content (276 lines) | stat: -rw-r--r-- 6,967 bytes parent folder | download | duplicates (2)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//go:build codegen
// +build codegen

package api

import (
	"encoding/json"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"sort"
	"strings"
)

// APIs provides a set of API models loaded by API package name.
type APIs map[string]*API

// Loader provides the loading of APIs from files.
type Loader struct {
	// The base Go import path the loaded models will be appended to.
	BaseImport string

	// Allows ignoring API models that are unsupported by the SDK without
	// failing the load of other supported APIs.
	IgnoreUnsupportedAPIs bool

	// Set to true to strictly enforce usage of the serviceId for the package naming
	StrictServiceId bool
}

// Load loads the API model files from disk returning the map of API package.
// Returns error if multiple API model resolve to the same package name.
func (l Loader) Load(modelPaths []string) (APIs, error) {
	apis := APIs{}
	for _, modelPath := range modelPaths {
		a, err := loadAPI(modelPath, l.BaseImport, func(a *API) {
			a.IgnoreUnsupportedAPIs = l.IgnoreUnsupportedAPIs
			a.StrictServiceId = l.StrictServiceId
		})
		if err != nil {
			return nil, fmt.Errorf("failed to load API, %v, %v", modelPath, err)
		}

		if len(a.Operations) == 0 {
			if l.IgnoreUnsupportedAPIs {
				fmt.Fprintf(os.Stderr, "API has no operations, ignoring model %s, %v\n",
					modelPath, a.ImportPath())
				continue
			}
		}

		importPath := a.ImportPath()
		if _, ok := apis[importPath]; ok {
			return nil, fmt.Errorf(
				"package names must be unique attempted to load %v twice. Second model file: %v",
				importPath, modelPath)
		}
		apis[importPath] = a
	}

	return apis, nil
}

// attempts to load a model from disk into the import specified. Additional API
// options are invoked before to the API's Setup being called.
func loadAPI(modelPath, baseImport string, opts ...func(*API)) (*API, error) {
	a := &API{
		BaseImportPath:   baseImport,
		BaseCrosslinkURL: "https://docs.aws.amazon.com",
	}

	for _, opt := range opts {
		opt(a)
	}

	modelFile := filepath.Base(modelPath)
	modelDir := filepath.Dir(modelPath)
	err := attachModelFiles(modelDir,
		modelLoader{modelFile, a.Attach, true},
		modelLoader{"docs-2.json", a.AttachDocs, false},
		modelLoader{"paginators-1.json", a.AttachPaginators, false},
		modelLoader{"waiters-2.json", a.AttachWaiters, false},
		modelLoader{"examples-1.json", a.AttachExamples, false},
		modelLoader{"smoke.json", a.AttachSmokeTests, false},
	)
	if err != nil {
		return nil, err
	}

	if err = a.Setup(); err != nil {
		return nil, err
	}

	return a, nil
}

type modelLoader struct {
	Filename string
	Loader   func(string) error
	Required bool
}

func attachModelFiles(modelPath string, modelFiles ...modelLoader) error {
	for _, m := range modelFiles {
		filepath := filepath.Join(modelPath, m.Filename)
		_, err := os.Stat(filepath)
		if os.IsNotExist(err) && !m.Required {
			continue
		} else if err != nil {
			return fmt.Errorf("failed to load model file %v, %v", m.Filename, err)
		}

		if err = m.Loader(filepath); err != nil {
			return fmt.Errorf("model load failed, %s, %v", modelPath, err)
		}
	}

	return nil
}

// ExpandModelGlobPath returns a slice of model paths expanded from the glob
// pattern passed in. Returns the path of the model file to be loaded. Includes
// all versions of a service model.
//
//   e.g:
//   models/apis/*/*/api-2.json
//
//   Or with specific model file:
//   models/apis/service/version/api-2.json
func ExpandModelGlobPath(globs ...string) ([]string, error) {
	modelPaths := []string{}

	for _, g := range globs {
		filepaths, err := filepath.Glob(g)
		if err != nil {
			return nil, err
		}
		for _, p := range filepaths {
			modelPaths = append(modelPaths, p)
		}
	}

	return modelPaths, nil
}

// TrimModelServiceVersions sorts the model paths by service version then
// returns recent model versions, and model version excluded.
//
// Uses the third from last path element to determine unique service. Only one
// service version will be included.
//
//   models/apis/service/version/api-2.json
func TrimModelServiceVersions(modelPaths []string) (include, exclude []string) {
	sort.Strings(modelPaths)

	// Remove old API versions from list
	m := map[string]struct{}{}
	for i := len(modelPaths) - 1; i >= 0; i-- {
		// service name is 2nd-to-last component
		parts := strings.Split(modelPaths[i], string(filepath.Separator))
		svc := parts[len(parts)-3]

		if _, ok := m[svc]; ok {
			// Removed unused service version
			exclude = append(exclude, modelPaths[i])
			continue
		}
		include = append(include, modelPaths[i])
		m[svc] = struct{}{}
	}

	return include, exclude
}

// Attach opens a file by name, and unmarshal its JSON data.
// Will proceed to setup the API if not already done so.
func (a *API) Attach(filename string) error {
	a.path = filepath.Dir(filename)
	f, err := os.Open(filename)
	if err != nil {
		return err
	}
	defer f.Close()

	if err := json.NewDecoder(f).Decode(a); err != nil {
		return fmt.Errorf("failed to decode %s, err: %v", filename, err)
	}

	return nil
}

// AttachString will unmarshal a raw JSON string, and setup the
// API if not already done so.
func (a *API) AttachString(str string) error {
	json.Unmarshal([]byte(str), a)

	if a.initialized {
		return nil
	}

	return a.Setup()
}

// Setup initializes the API.
func (a *API) Setup() error {
	if err := a.validateNoDocumentShapes(); err != nil {
		return err
	}
	if !a.NoRemoveUnsupportedJSONValue {
		if err := removeUnsupportedJSONValue(a); err != nil {
			return fmt.Errorf("failed to remove unsupported JSONValue from API, %v", err)
		}
	}
	a.setServiceAliaseName()
	a.setMetadataEndpointsKey()
	a.writeShapeNames()
	a.resolveReferences()
	a.backfillErrorMembers()
	a.backfillSigningName()

	if !a.NoRemoveUnusedShapes {
		a.removeUnusedShapes()
	}

	a.fixStutterNames()
	if err := a.validateShapeNames(); err != nil {
		log.Fatalf(err.Error())
	}
	a.renameExportable()
	a.applyShapeNameAliases()
	a.renameIOSuffixedShapeNames()
	a.createInputOutputShapes()
	a.writeInputOutputLocationName()
	a.renameAPIPayloadShapes()
	a.renameCollidingFields()
	a.updateTopLevelShapeReferences()
	if err := a.setupEventStreams(); err != nil {
		return err
	}

	a.findEndpointDiscoveryOp()
	a.injectUnboundedOutputStreaming()

	// Enables generated types for APIs using REST-JSON and JSONRPC protocols.
	// Other protocols will be added as supported.
	a.enableGeneratedTypedErrors()

	if err := a.customizationPasses(); err != nil {
		return err
	}

	a.addHeaderMapDocumentation()

	if !a.NoRemoveUnusedShapes {
		a.removeUnusedShapes()
	}

	if !a.NoValidataShapeMethods {
		a.addShapeValidations()
	}

	a.initialized = true

	return nil
}

// UnsupportedAPIModelError provides wrapping of an error causing the API to
// fail to load because the SDK does not support the API service defined.
type UnsupportedAPIModelError struct {
	Err error
}

func (e UnsupportedAPIModelError) Error() string {
	return fmt.Sprintf("service API is not supported, %v", e.Err)
}