|
| 1 | +package json_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 8 | + "github.com/aws/aws-sdk-go-v2/aws/protocol/json" |
| 9 | + "github.com/aws/aws-sdk-go-v2/private/protocol" |
| 10 | + v1Encoder "github.com/aws/aws-sdk-go-v2/private/protocol/json" |
| 11 | + reflectEncoder "github.com/aws/aws-sdk-go-v2/private/protocol/json/jsonutil" |
| 12 | +) |
| 13 | + |
| 14 | +type testEnum string |
| 15 | + |
| 16 | +type testOperationInput struct { |
| 17 | + _ struct{} `type:"structure"` |
| 18 | + StringValue *string `locationName:"stringValue" type:"string"` |
| 19 | + IntegerValue *int64 `locationName:"integerValue" type:"integer"` |
| 20 | + EnumValue testEnum `locationName:"enumValue" type:"string" enum:"true"` |
| 21 | + FloatValue *float64 `locationName:"floatValue" type:"double"` |
| 22 | + ListValue []nestedShape `locationName:"listValue" type:"list"` |
| 23 | + ShapeValue *nestedShape `locationName:"shapeValue" type:"structure"` |
| 24 | + MapValue map[string]string `locationName:"mapValue" type:"map"` |
| 25 | + ByteSlice []byte `locationName:"byteSlice" type:"blob"` |
| 26 | +} |
| 27 | + |
| 28 | +func (v *testOperationInput) MarshalFields(encoder protocol.FieldEncoder) error { |
| 29 | + meta := protocol.Metadata{} |
| 30 | + |
| 31 | + if v.StringValue != nil { |
| 32 | + encoder.SetValue(protocol.BodyTarget, "stringValue", protocol.QuotedValue{ValueMarshaler: protocol.StringValue(*v.StringValue)}, meta) |
| 33 | + } |
| 34 | + |
| 35 | + if v.IntegerValue != nil { |
| 36 | + encoder.SetValue(protocol.BodyTarget, "integerValue", protocol.Int64Value(*v.IntegerValue), meta) |
| 37 | + } |
| 38 | + |
| 39 | + if len(v.EnumValue) > 0 { |
| 40 | + encoder.SetValue(protocol.BodyTarget, "enumValue", protocol.QuotedValue{ValueMarshaler: protocol.StringValue(v.EnumValue)}, meta) |
| 41 | + } |
| 42 | + |
| 43 | + if v.FloatValue != nil { |
| 44 | + encoder.SetValue(protocol.BodyTarget, "floatValue", protocol.Float64Value(*v.FloatValue), meta) |
| 45 | + } |
| 46 | + |
| 47 | + if v.ListValue != nil { |
| 48 | + listEncoder := encoder.List(protocol.BodyTarget, "listValue", meta) |
| 49 | + listEncoder.Start() |
| 50 | + for i := range v.ListValue { |
| 51 | + listEncoder.ListAddFields(&v.ListValue[i]) |
| 52 | + } |
| 53 | + listEncoder.End() |
| 54 | + } |
| 55 | + |
| 56 | + if v.ShapeValue != nil { |
| 57 | + encoder.SetFields(protocol.BodyTarget, "shapeValue", v.ShapeValue, meta) |
| 58 | + } |
| 59 | + |
| 60 | + if v.MapValue != nil { |
| 61 | + mapEncoder := encoder.Map(protocol.BodyTarget, "mapValue", meta) |
| 62 | + mapEncoder.Start() |
| 63 | + for k := range v.MapValue { |
| 64 | + mapEncoder.MapSetValue(k, protocol.QuotedValue{ValueMarshaler: protocol.StringValue(v.MapValue[k])}) |
| 65 | + } |
| 66 | + mapEncoder.End() |
| 67 | + } |
| 68 | + |
| 69 | + if v.ByteSlice != nil { |
| 70 | + encoder.SetValue(protocol.BodyTarget, "byteSlice", protocol.QuotedValue{ValueMarshaler: protocol.BytesValue(v.ByteSlice)}, meta) |
| 71 | + } |
| 72 | + |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +type nestedShape struct { |
| 77 | + _ struct{} `type:"structure"` |
| 78 | + StringValue *string `locationName:"stringValue" type:"string"` |
| 79 | +} |
| 80 | + |
| 81 | +func (v *nestedShape) MarshalFields(encoder protocol.FieldEncoder) error { |
| 82 | + meta := protocol.Metadata{} |
| 83 | + |
| 84 | + if v.StringValue != nil { |
| 85 | + encoder.SetValue(protocol.BodyTarget, "stringValue", protocol.QuotedValue{ValueMarshaler: protocol.StringValue(*v.StringValue)}, meta) |
| 86 | + } |
| 87 | + |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func MarshalTestOperationInputAWSJSON(v *testOperationInput, e *json.Encoder) []byte { |
| 92 | + marshalTestOperationInputAWSJSON(v, &e.Value) |
| 93 | + |
| 94 | + return e.Bytes() |
| 95 | +} |
| 96 | + |
| 97 | +func marshalTestOperationInputAWSJSON(v *testOperationInput, j *json.Value) { |
| 98 | + object := j.Object() |
| 99 | + defer object.Close() |
| 100 | + |
| 101 | + if v.StringValue != nil { |
| 102 | + object.Key("stringValue").String(*v.StringValue) |
| 103 | + } |
| 104 | + |
| 105 | + if v.IntegerValue != nil { |
| 106 | + object.Key("integerValue").Integer(*v.IntegerValue) |
| 107 | + } |
| 108 | + |
| 109 | + if len(v.EnumValue) > 0 { |
| 110 | + object.Key("enumValue").String(string(v.EnumValue)) |
| 111 | + } |
| 112 | + |
| 113 | + if v.FloatValue != nil { |
| 114 | + object.Key("floatValue").Float(*v.FloatValue) |
| 115 | + } |
| 116 | + |
| 117 | + if v.ListValue != nil { |
| 118 | + value := object.Key("listValue") |
| 119 | + marshalListValueShapeAWSREST(v.ListValue, &value) |
| 120 | + } |
| 121 | + |
| 122 | + if v.ShapeValue != nil { |
| 123 | + value := object.Key("shapeValue") |
| 124 | + marshalNestedShapeAWSREST(v.ShapeValue, &value) |
| 125 | + } |
| 126 | + |
| 127 | + if v.MapValue != nil { |
| 128 | + value := object.Key("mapValue") |
| 129 | + marshalMapShapeAWSREST(v.MapValue, &value) |
| 130 | + } |
| 131 | + |
| 132 | + if v.ByteSlice != nil { |
| 133 | + object.Key("byteSlice").ByteSlice(v.ByteSlice) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func marshalListValueShapeAWSREST(v []nestedShape, j *json.Value) { |
| 138 | + array := j.Array() |
| 139 | + defer array.Close() |
| 140 | + |
| 141 | + for i := range v { |
| 142 | + arrayValue := array.Value() |
| 143 | + marshalNestedShapeAWSREST(&v[i], &arrayValue) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func marshalNestedShapeAWSREST(v *nestedShape, j *json.Value) { |
| 148 | + object := j.Object() |
| 149 | + defer object.Close() |
| 150 | + |
| 151 | + if v.StringValue != nil { |
| 152 | + object.Key("stringValue").String(*v.StringValue) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func marshalMapShapeAWSREST(v map[string]string, j *json.Value) { |
| 157 | + object := j.Object() |
| 158 | + defer object.Close() |
| 159 | + |
| 160 | + for k := range v { |
| 161 | + object.Key(k).String(v[k]) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +var testOperationCases = [...]*testOperationInput{ |
| 166 | + 0: {}, |
| 167 | + 1: { |
| 168 | + StringValue: aws.String("someStringValue1"), |
| 169 | + IntegerValue: aws.Int64(42), |
| 170 | + EnumValue: "SOME_ENUM", |
| 171 | + FloatValue: aws.Float64(3.14), |
| 172 | + ListValue: []nestedShape{ |
| 173 | + {StringValue: aws.String("someStringValue2")}, |
| 174 | + {}, |
| 175 | + }, |
| 176 | + ShapeValue: &nestedShape{StringValue: aws.String("someStringValue3")}, |
| 177 | + MapValue: map[string]string{ |
| 178 | + "someMapKey": "someMapValue", |
| 179 | + }, |
| 180 | + ByteSlice: make([]byte, 1024), |
| 181 | + }, |
| 182 | +} |
| 183 | + |
| 184 | +func BenchmarkEncoderV2(b *testing.B) { |
| 185 | + for i, operationCase := range testOperationCases { |
| 186 | + b.Run(fmt.Sprintf("Case%d", i), func(b *testing.B) { |
| 187 | + encoder := json.NewEncoder() |
| 188 | + _ = MarshalTestOperationInputAWSJSON(operationCase, encoder) |
| 189 | + }) |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +func BenchmarkEncoderV1(b *testing.B) { |
| 194 | + for i, operationCase := range testOperationCases { |
| 195 | + b.Run(fmt.Sprintf("Case%d", i), func(b *testing.B) { |
| 196 | + encoder := v1Encoder.NewEncoder() |
| 197 | + err := operationCase.MarshalFields(encoder) |
| 198 | + b.StopTimer() |
| 199 | + if err != nil { |
| 200 | + b.Fatal(err) |
| 201 | + } |
| 202 | + _, err = encoder.Encode() |
| 203 | + if err != nil { |
| 204 | + b.Fatal(err) |
| 205 | + } |
| 206 | + }) |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +func BenchmarkEncoderReflection(b *testing.B) { |
| 211 | + for i, operationCase := range testOperationCases { |
| 212 | + b.Run(fmt.Sprintf("Case%d", i), func(b *testing.B) { |
| 213 | + _, err := reflectEncoder.BuildJSON(operationCase) |
| 214 | + if err != nil { |
| 215 | + b.Fatal(err) |
| 216 | + } |
| 217 | + }) |
| 218 | + } |
| 219 | +} |
0 commit comments