5
5
"fmt"
6
6
"net/url"
7
7
"strconv"
8
- "strings"
9
8
10
9
"golang.org/x/text/cases"
11
10
"golang.org/x/text/language"
@@ -189,30 +188,41 @@ type SendMessageRequest struct {
189
188
QueueUrl string `json:"QueueUrl" schema:"QueueUrl"`
190
189
}
191
190
192
- func (r * SendMessageRequest ) SetAttributesFromForm (values url.Values ) {
191
+ func parseMessageAttributes (values url.Values , keyPrefix string ) map [string ]MessageAttribute {
192
+ result := map [string ]MessageAttribute {}
193
+
193
194
for i := 1 ; true ; i ++ {
194
- nameKey := fmt .Sprintf ("MessageAttribute .%d.Name" , i )
195
+ nameKey := fmt .Sprintf ("%s .%d.Name" , keyPrefix , i )
195
196
name := values .Get (nameKey )
196
197
if name == "" {
197
198
break
198
199
}
199
200
200
- dataTypeKey := fmt .Sprintf ("MessageAttribute .%d.Value.DataType" , i )
201
+ dataTypeKey := fmt .Sprintf ("%s .%d.Value.DataType" , keyPrefix , i )
201
202
dataType := values .Get (dataTypeKey )
202
203
if dataType == "" {
203
- log .Warnf ("DataType of MessageAttribute %s is missing, MD5 checksum will most probably be wrong!\n " , name )
204
+ log .Warnf ("DataType of message attribute %s is missing, MD5 checksum will most probably be wrong!\n " , name )
204
205
continue
205
206
}
206
207
207
- stringValue := values .Get (fmt .Sprintf ("MessageAttribute .%d.Value.StringValue" , i ))
208
- binaryValue := values .Get (fmt .Sprintf ("MessageAttribute .%d.Value.BinaryValue" , i ))
208
+ stringValue := values .Get (fmt .Sprintf ("%s .%d.Value.StringValue" , keyPrefix , i ))
209
+ binaryValue := values .Get (fmt .Sprintf ("%s .%d.Value.BinaryValue" , keyPrefix , i ))
209
210
210
- r . MessageAttributes [name ] = MessageAttribute {
211
+ result [name ] = MessageAttribute {
211
212
DataType : dataType ,
212
213
StringValue : stringValue ,
213
214
BinaryValue : []byte (binaryValue ),
214
215
}
215
216
}
217
+
218
+ if len (result ) > 0 {
219
+ return result
220
+ }
221
+ return nil
222
+ }
223
+
224
+ func (r * SendMessageRequest ) SetAttributesFromForm (values url.Values ) {
225
+ r .MessageAttributes = parseMessageAttributes (values , "MessageAttribute" )
216
226
}
217
227
218
228
func NewSendMessageBatchRequest () * SendMessageBatchRequest {
@@ -225,53 +235,8 @@ type SendMessageBatchRequest struct {
225
235
}
226
236
227
237
func (r * SendMessageBatchRequest ) SetAttributesFromForm (values url.Values ) {
228
- for key := range values {
229
-
230
- keySegments := strings .Split (key , "." )
231
- //If index value size is 3 or less, there is no attribute value
232
- if len (keySegments ) <= 3 {
233
- continue
234
- }
235
-
236
- // Both patterns below are supported here.
237
- // strconv.Atoi(keySegments[1] - targets the index value in pattern: `Entries.1.MessageBody`
238
- // strconv.Atoi(keySegments[3] - targets the index value in pattern: `Entries.1.MessageAttributes.1.Name`
239
- entryIndex , err1 := strconv .Atoi (keySegments [1 ])
240
- attributeIndex , err2 := strconv .Atoi (keySegments [3 ])
241
-
242
- // If the entry index and attribute index cannot be obtained, the attribute will not be set, so skip
243
- if err1 != nil || err2 != nil {
244
- continue
245
- }
246
-
247
- nameKey := fmt .Sprintf ("Entries.%d.MessageAttributes.%d.Name" , entryIndex , attributeIndex )
248
- if key != nameKey {
249
- continue
250
- }
251
- name := values .Get (nameKey )
252
- dataTypeKey := fmt .Sprintf ("Entries.%d.MessageAttributes.%d.Value.DataType" , entryIndex , attributeIndex )
253
- dataType := values .Get (dataTypeKey )
254
- if dataType == "" {
255
- log .Warnf ("DataType of MessageAttribute %s is missing, MD5 checksum will most probably be wrong!\n " , name )
256
- continue
257
- }
258
-
259
- stringValue := values .Get (fmt .Sprintf ("Entries.%d.MessageAttributes.%d.Value.StringValue" , entryIndex , attributeIndex ))
260
- binaryValue := values .Get (fmt .Sprintf ("Entries.%d.MessageAttributes.%d.Value.BinaryValue" , entryIndex , attributeIndex ))
261
-
262
- if r .Entries [entryIndex ].MessageAttributes == nil {
263
- r .Entries [entryIndex ].MessageAttributes = make (map [string ]MessageAttribute )
264
- }
265
-
266
- r .Entries [entryIndex ].MessageAttributes [name ] = MessageAttribute {
267
- DataType : dataType ,
268
- StringValue : stringValue ,
269
- BinaryValue : []byte (binaryValue ),
270
- }
271
-
272
- if _ , ok := r .Entries [entryIndex ].MessageAttributes [name ]; ! ok {
273
- log .Warnf ("StringValue or BinaryValue of MessageAttribute %s is missing, MD5 checksum will most probably be wrong!\n " , name )
274
- }
238
+ for entryIndex := range r .Entries {
239
+ r .Entries [entryIndex ].MessageAttributes = parseMessageAttributes (values , fmt .Sprintf ("Entries.%d.MessageAttributes" , entryIndex ))
275
240
}
276
241
}
277
242
@@ -744,34 +709,7 @@ type PublishRequest struct {
744
709
}
745
710
746
711
func (r * PublishRequest ) SetAttributesFromForm (values url.Values ) {
747
- attributes := map [string ]MessageAttribute {}
748
- for i := 1 ; true ; i ++ {
749
- nameKey := fmt .Sprintf ("MessageAttributes.entry.%d.Name" , i )
750
- name := values .Get (nameKey )
751
- if name == "" {
752
- break
753
- }
754
-
755
- dataTypeKey := fmt .Sprintf ("MessageAttributes.entry.%d.Value.DataType" , i )
756
- dataType := values .Get (dataTypeKey )
757
- if dataType == "" {
758
- log .Warnf ("DataType of MessageAttribute %s is missing, MD5 checksum will most probably be wrong!\n " , name )
759
- continue
760
- }
761
-
762
- stringValue := values .Get (fmt .Sprintf ("MessageAttributes.entry.%d.Value.StringValue" , i ))
763
- binaryValue := values .Get (fmt .Sprintf ("MessageAttributes.entry.%d.Value.BinaryValue" , i ))
764
-
765
- if r .MessageAttributes == nil {
766
- r .MessageAttributes = make (map [string ]MessageAttribute )
767
- }
768
- attributes [name ] = MessageAttribute {
769
- DataType : caser .String (dataType ), // capitalize
770
- StringValue : stringValue ,
771
- BinaryValue : []byte (binaryValue ),
772
- }
773
- }
774
- r .MessageAttributes = attributes
712
+ r .MessageAttributes = parseMessageAttributes (values , "MessageAttributes.entry" )
775
713
}
776
714
777
715
// Satisfy the AbstractPublishEntry interface
@@ -890,7 +828,14 @@ type PublishBatchRequest struct {
890
828
}
891
829
892
830
func (r * PublishBatchRequest ) SetAttributesFromForm (values url.Values ) {
893
- // TAG - Implement me
831
+ for entryIndex , entry := range r .PublishBatchRequestEntries .Member {
832
+ if entry == nil {
833
+ // The form values are 1-indexed; at the point that this is called, the first element in the
834
+ // list of requests will be nil.
835
+ continue
836
+ }
837
+ entry .MessageAttributes = parseMessageAttributes (values , fmt .Sprintf ("PublishBatchRequestEntries.member.%d.MessageAttributes.entry" , entryIndex ))
838
+ }
894
839
}
895
840
896
841
type PublishBatchRequestEntries struct {
0 commit comments