Skip to content

Commit 39ee151

Browse files
authored
private/protocol: adding generated marshalers (#110)
* adding generated marshalers Regenerating services Adding generated marshalers adding docs to enum template benchmarks * regenerating services
1 parent aba69a2 commit 39ee151

File tree

163 files changed

+98171
-545
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

163 files changed

+98171
-545
lines changed

internal/awsutil/copy_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import (
1515
func ExampleCopy() {
1616
type Foo struct {
1717
A int
18-
B []*string
18+
B []string
1919
}
2020

2121
// Create the initial value
2222
str1 := "hello"
2323
str2 := "bye bye"
24-
f1 := &Foo{A: 1, B: []*string{&str1, &str2}}
24+
f1 := &Foo{A: 1, B: []string{str1, str2}}
2525

2626
// Do the copy
2727
var f2 Foo
@@ -46,8 +46,8 @@ func TestCopy1(t *testing.T) {
4646
}
4747
type Foo struct {
4848
A int
49-
B []*string
50-
C map[string]*int
49+
B []string
50+
C map[string]int
5151
D *time.Time
5252
E *Bar
5353
}
@@ -62,10 +62,10 @@ func TestCopy1(t *testing.T) {
6262
now := time.Now()
6363
f1 := &Foo{
6464
A: 1,
65-
B: []*string{&str1, &str2},
66-
C: map[string]*int{
67-
"A": &int1,
68-
"B": &int2,
65+
B: []string{str1, str2},
66+
C: map[string]int{
67+
"A": int1,
68+
"B": int2,
6969
},
7070
D: &now,
7171
E: &Bar{
@@ -104,8 +104,8 @@ func TestCopy1(t *testing.T) {
104104
str3 := "nothello"
105105
int3 := 57
106106
f2.A = 100
107-
*f2.B[0] = str3
108-
*f2.C["B"] = int3
107+
f2.B[0] = str3
108+
f2.C["B"] = int3
109109
*f2.D = time.Now()
110110
f2.E.a = &int3
111111
*f2.E.B = int3
@@ -252,17 +252,17 @@ func TestCopyReader(t *testing.T) {
252252
func TestCopyDifferentStructs(t *testing.T) {
253253
type SrcFoo struct {
254254
A int
255-
B []*string
256-
C map[string]*int
255+
B []string
256+
C map[string]int
257257
SrcUnique string
258258
SameNameDiffType int
259259
unexportedPtr *int
260260
ExportedPtr *int
261261
}
262262
type DstFoo struct {
263263
A int
264-
B []*string
265-
C map[string]*int
264+
B []string
265+
C map[string]int
266266
DstUnique int
267267
SameNameDiffType string
268268
unexportedPtr *int
@@ -276,10 +276,10 @@ func TestCopyDifferentStructs(t *testing.T) {
276276
int2 := 2
277277
f1 := &SrcFoo{
278278
A: 1,
279-
B: []*string{&str1, &str2},
280-
C: map[string]*int{
281-
"A": &int1,
282-
"B": &int2,
279+
B: []string{str1, str2},
280+
C: map[string]int{
281+
"A": int1,
282+
"B": int2,
283283
},
284284
SrcUnique: "unique",
285285
SameNameDiffType: 1,
@@ -330,13 +330,13 @@ func TestCopyDifferentStructs(t *testing.T) {
330330
func ExampleCopyOf() {
331331
type Foo struct {
332332
A int
333-
B []*string
333+
B []string
334334
}
335335

336336
// Create the initial value
337337
str1 := "hello"
338338
str2 := "bye bye"
339-
f1 := &Foo{A: 1, B: []*string{&str1, &str2}}
339+
f1 := &Foo{A: 1, B: []string{str1, str2}}
340340

341341
// Do the copy
342342
v := awsutil.CopyOf(f1)

models/protocol_tests/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ func generateTestSuite(filename string) string {
397397

398398
for i, suite := range suites {
399399
svcPrefix := inout + "Service" + strconv.Itoa(i+1)
400+
suite.API.EnableSelectGeneratedMarshalers()
400401
suite.API.Metadata.ServiceAbbreviation = svcPrefix + "ProtocolTest"
401402
suite.API.Operations = map[string]*api.Operation{}
402403
for idx, c := range suite.Cases {

models/protocol_tests/input/ec2.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@
222222
2.718,
223223
3.14
224224
],
225+
"ListIntegers": [
226+
0,
227+
1,
228+
2
229+
],
225230
"ListIntegers": [
226231
0,
227232
1,

private/model/api/api.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ type API struct {
4646
// Set to true to not generate struct field accessors
4747
NoGenStructFieldAccessors bool
4848

49+
// Set to true to not generate (un)marshalers
50+
NoGenMarshalers bool
51+
NoGenUnmarshalers bool
52+
4953
SvcClientImportPath string
5054

5155
initialized bool
@@ -311,6 +315,9 @@ func (a *API) APIGoCode() string {
311315
a.imports["github.com/aws/aws-sdk-go-v2/private/protocol/"+a.ProtocolPackage()] = true
312316
a.imports["github.com/aws/aws-sdk-go-v2/private/protocol"] = true
313317
}
318+
if !a.NoGenMarshalers || !a.NoGenUnmarshalers {
319+
a.imports["github.com/aws/aws-sdk-go-v2/private/protocol"] = true
320+
}
314321

315322
var buf bytes.Buffer
316323
err := tplAPI.Execute(&buf, a)

private/model/api/customization_passes.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ var mergeServices = map[string]service{
2727
},
2828
}
2929

30+
func (a *API) EnableSelectGeneratedMarshalers() {
31+
// Selectivily enable generated marshalers as available
32+
a.NoGenMarshalers = true
33+
a.NoGenUnmarshalers = true
34+
35+
// Enable generated marshalers
36+
switch a.Metadata.Protocol {
37+
case "rest-xml", "rest-json":
38+
a.NoGenMarshalers = false
39+
}
40+
}
41+
3042
// customizationPasses Executes customization logic for the API by package name.
3143
func (a *API) customizationPasses() {
3244
var svcCustomizations = map[string]func(*API){
@@ -42,6 +54,8 @@ func (a *API) customizationPasses() {
4254
if fn := svcCustomizations[a.PackageName()]; fn != nil {
4355
fn(a)
4456
}
57+
58+
a.EnableSelectGeneratedMarshalers()
4559
}
4660

4761
// s3Customizations customizes the API generation to replace values specific to S3.

private/model/api/passes.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ func (a *API) resolveReferences() {
6060
o.ErrorRefs[i].Shape.IsError = true
6161
}
6262
}
63+
64+
for _, s := range a.Shapes {
65+
switch s.Type {
66+
case "list":
67+
s.MemberRef.Shape.UsedInList = true
68+
case "map":
69+
s.ValueRef.Shape.UsedInMap = true
70+
}
71+
}
6372
}
6473

6574
// A referenceResolver provides a way to resolve shape references to
@@ -189,6 +198,13 @@ func (a *API) renameExportable() {
189198

190199
for mName, member := range s.MemberRefs {
191200
newName := a.ExportableName(mName)
201+
202+
// if no location name is set on the member ref, but is set on the shape,
203+
// we will take that name and place it on the reference.
204+
if member.LocationName == "" && member.Shape.LocationName != "" {
205+
member.LocationName = member.Shape.LocationName
206+
}
207+
192208
if newName != mName {
193209
delete(s.MemberRefs, mName)
194210
s.MemberRefs[newName] = member
@@ -216,6 +232,9 @@ func (a *API) renameExportable() {
216232
for i, n := range s.Required {
217233
s.Required[i] = a.ExportableName(n)
218234
}
235+
236+
// remove location name
237+
s.LocationName = ""
219238
}
220239

221240
for _, s := range a.Shapes {

private/model/api/shape.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ type Shape struct {
7878

7979
OrigShapeName string `json:"-"`
8080

81+
UsedInMap bool
82+
UsedInList bool
83+
8184
// Defines if the shape is a placeholder and should not be used directly
8285
Placeholder bool
8386

@@ -192,6 +195,17 @@ func (s *Shape) GoStructValueType(name string, ref *ShapeRef) string {
192195
return v
193196
}
194197

198+
// IsRefPayload will return whether or a not the field name is a payload.
199+
func (s *Shape) IsRefPayload(name string) bool {
200+
return s.Payload == name
201+
}
202+
203+
// IsRefPayloadReader will whether or not the shape ref is a payload and that it is
204+
// a streaming reference.
205+
func (s *Shape) IsRefPayloadReader(name string, ref *ShapeRef) bool {
206+
return (ref.Streaming || ref.Shape.Streaming) && s.IsRefPayload(name)
207+
}
208+
195209
// GoStructType returns the type of a struct field based on the API
196210
// model definition.
197211
func (s *Shape) GoStructType(name string, ref *ShapeRef) string {
@@ -545,7 +559,9 @@ func (s *Shape) NestedShape() *Shape {
545559
}
546560

547561
var structShapeTmpl = template.Must(template.New("StructShape").Funcs(template.FuncMap{
548-
"GetCrosslinkURL": GetCrosslinkURL,
562+
"GetCrosslinkURL": GetCrosslinkURL,
563+
"MarshalShapeGoCode": MarshalShapeGoCode,
564+
"UnmarshalShapeGoCode": UnmarshalShapeGoCode,
549565
}).Parse(`
550566
{{ .Docstring }}
551567
{{ if ne $.OrigShapeName "" -}}
@@ -630,9 +646,18 @@ func (s *{{ $builderShapeName }}) get{{ $name }}() (v {{ $context.GoStructValueT
630646
631647
{{ end }}
632648
{{ end }}
649+
650+
{{ if not $.API.NoGenMarshalers -}}
651+
{{ MarshalShapeGoCode $ }}
652+
{{- end }}
653+
{{ if not $.API.NoGenUnmarshalers -}}
654+
{{ UnmarshalShapeGoCode $ }}
655+
{{- end }}
633656
`))
634657

635-
var enumShapeTmpl = template.Must(template.New("EnumShape").Parse(`
658+
var enumShapeTmpl = template.Must(template.New("EnumShape").Funcs(template.FuncMap{
659+
"MarshalEnumGoCode": marshalEnumGoCode,
660+
}).Parse(`
636661
{{ .Docstring }}
637662
type {{ $.EnumType }} string
638663
@@ -644,6 +669,8 @@ const (
644669
{{ $name }} {{ $.EnumType }} = "{{ $elem }}"
645670
{{ end -}}
646671
)
672+
673+
{{ MarshalEnumGoCode $ }}
647674
`))
648675

649676
// GoCode returns the rendered Go code for the Shape.

0 commit comments

Comments
 (0)