Skip to content

Commit

Permalink
Support for List of Enums in Headers (#4166)
Browse files Browse the repository at this point in the history
Adds support for list of enums in headers.
  • Loading branch information
skmcgrail authored Mar 15, 2022
1 parent 8b5f83e commit 254f0c0
Show file tree
Hide file tree
Showing 137 changed files with 662 additions and 527 deletions.
2 changes: 2 additions & 0 deletions private/model/api/shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,8 @@ func (ref *ShapeRef) GoTags(toplevel bool, isRequired bool) string {
}
if ref.Shape.IsEnum() {
tags = append(tags, ShapeTag{"enum", ref.ShapeName})
} else if ref.Shape.Type == "list" && ref.Shape.MemberRef.Shape.IsEnum() {
tags = append(tags, ShapeTag{"enum", ref.Shape.MemberRef.ShapeName})
}

if toplevel {
Expand Down
4 changes: 2 additions & 2 deletions private/protocol/ec2query/build_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion private/protocol/ec2query/unmarshal_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions private/protocol/jsonrpc/build_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion private/protocol/jsonrpc/unmarshal_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions private/protocol/query/build_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion private/protocol/query/unmarshal_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions private/protocol/rest/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,25 @@ func convertType(v reflect.Value, tag reflect.StructTag) (str string, err error)
value = base64.StdEncoding.EncodeToString([]byte(value))
}
str = value
case []*string:
if tag.Get("location") != "header" || tag.Get("enum") == "" {
return "", fmt.Errorf("%T is only supported with location header and enum shapes", value)
}
buff := &bytes.Buffer{}
for i, sv := range value {
if sv == nil || len(*sv) == 0 {
continue
}
if i != 0 {
buff.WriteRune(',')
}
item := *sv
if strings.Index(item, `,`) != -1 || strings.Index(item, `"`) != -1 {
item = strconv.Quote(item)
}
buff.WriteString(item)
}
str = string(buff.Bytes())
case []byte:
str = base64.StdEncoding.EncodeToString(value)
case bool:
Expand Down
114 changes: 114 additions & 0 deletions private/protocol/rest/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rest
import (
"net/http"
"net/url"
"reflect"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -61,3 +62,116 @@ func TestMarshalPath(t *testing.T) {
}

}

func TestListOfEnums(t *testing.T) {
cases := []struct {
Input interface{}
Expected http.Header
WantErr bool
}{
{
Input: func() interface{} {
type v struct {
List []*string `type:"list" location:"header" locationName:"x-amz-test-header"`
}
return &v{
List: []*string{aws.String("foo")},
}
}(),
WantErr: true,
},
{
Input: func() interface{} {
type v struct {
List []*string `type:"list" location:"uri" locationName:"someList"`
}
return &v{
List: []*string{aws.String("foo")},
}
}(),
WantErr: true,
},
{
Input: func() interface{} {
type v struct {
List map[string][]*string `type:"map" location:"headers" locationName:"x-amz-metadata"`
}
return &v{
List: map[string][]*string{
"foo": {aws.String("foo")},
},
}
}(),
WantErr: true,
},
{
Input: func() interface{} {
type v struct {
List []*string `type:"list" location:"header" locationName:"x-amz-test-header" enum:"FooBar"`
}
return &v{
List: []*string{aws.String("foo")},
}
}(),
Expected: http.Header{
"X-Amz-Test-Header": {"foo"},
},
},
{
Input: func() interface{} {
type v struct {
List []*string `type:"list" location:"header" locationName:"x-amz-test-header" enum:"FooBar"`
}
return &v{
List: []*string{aws.String("foo"), nil, aws.String(""), nil, aws.String("bar")},
}
}(),
Expected: http.Header{
"X-Amz-Test-Header": {"foo,bar"},
},
},
{
Input: func() interface{} {
type v struct {
List []*string `type:"list" location:"header" locationName:"x-amz-test-header" enum:"FooBar"`
}
return &v{
List: []*string{aws.String("f,o,o"), nil, aws.String(""), nil, aws.String(`"bar"`)},
}
}(),
Expected: http.Header{
"X-Amz-Test-Header": {`"f,o,o","\"bar\""`},
},
},
}

for i, tt := range cases {
req := &request.Request{
HTTPRequest: &http.Request{
URL: func() *url.URL {
u, err := url.Parse("https://foo.amazonaws.com")
if err != nil {
panic(err)
}
return u
}(),
Header: map[string][]string{},
},
Params: tt.Input,
}

Build(req)

if (req.Error != nil) != (tt.WantErr) {
t.Fatalf("(%d) WantErr(%t) got %v", i, tt.WantErr, req.Error)
}

if tt.WantErr {
continue
}

if e, a := tt.Expected, req.HTTPRequest.Header; !reflect.DeepEqual(e, a) {
t.Errorf("(%d) expect %v, got %v", i, e, a)
}
}
}
8 changes: 4 additions & 4 deletions private/protocol/restjson/build_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions private/protocol/restjson/unmarshal_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions private/protocol/restxml/build_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions private/protocol/restxml/unmarshal_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/accessanalyzer/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions service/acm/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 254f0c0

Please sign in to comment.