12
12
package protocol
13
13
14
14
import (
15
+ "encoding/json"
15
16
"fmt"
16
17
"strconv"
17
18
"strings"
18
19
"time"
20
+
21
+ "github.com/google/uuid"
19
22
)
20
23
21
24
const (
@@ -72,15 +75,16 @@ const (
72
75
HeaderContentType = "content-type"
73
76
)
74
77
75
- // Headers represents all Ditto-specific headers along with additional HTTP/etc. headers
78
+ // Headers represents all Ditto-specific headers along with additional HTTP/etc. Headers
76
79
// that can be applied depending on the transport used.
77
- // For the pre-defined headers, the values are in the row type. The getter methods are provided
78
- // to get the header value in specified type.
80
+ //
81
+ // The header values in this map should be serialized.
82
+ // The provided getter methods returns the header values which is associated with this definition's key.
79
83
// See https://www.eclipse.org/ditto/protocol-specification.html
80
84
type Headers map [string ]interface {}
81
85
82
86
// CorrelationID returns the 'correlation-id' header value if it is presented.
83
- // If the header value is not presented, the CorrelationID returns empty string .
87
+ // If the header value is not presented, the 'correlation-id' header value will be generated in UUID format .
84
88
//
85
89
// If there are two headers differing only in capitalization CorrelationID returns the first value.
86
90
// To use the provided key to get the value, access the map directly.
@@ -90,10 +94,10 @@ func (h Headers) CorrelationID() string {
90
94
return v .(string )
91
95
}
92
96
}
93
- return ""
97
+ return uuid . New (). String ()
94
98
}
95
99
96
- // Timeout returns the 'timeout' header value if it is presented
100
+ // Timeout returns the 'timeout' header value if it is presented.
97
101
// The default and maximum value is duration of 60 seconds.
98
102
// If the header value is not presented, the Timout returns the default value.
99
103
//
@@ -134,7 +138,7 @@ func parseTimeout(timeout string) (time.Duration, error) {
134
138
t = time .Duration (i ) * time .Second
135
139
}
136
140
}
137
- if t >= 0 && t < time .Hour {
141
+ if t >= 0 && t <= 60 * time .Second {
138
142
return t , nil
139
143
}
140
144
}
@@ -284,7 +288,7 @@ func (h Headers) ReplyTo() string {
284
288
}
285
289
286
290
// Version returns the 'version' header value if it is presented.
287
- // If the header value is not presented, the Version returns 0 .
291
+ // If the header value is not presented, the Version returns 2 .
288
292
//
289
293
// If there are two headers differing only in capitalization, the Version returns the first value.
290
294
// To use the provided key to get the value, access the map directly.
@@ -294,7 +298,7 @@ func (h Headers) Version() int64 {
294
298
return v .(int64 )
295
299
}
296
300
}
297
- return 0
301
+ return 2
298
302
}
299
303
300
304
// ContentType returns the 'content-type' header value if it is presented.
@@ -325,44 +329,26 @@ func (h Headers) Generic(id string) interface{} {
325
329
return nil
326
330
}
327
331
328
- // // MarshalJSON marshels Headers.
329
- // func (h *Headers) MarshalJSON() ([]byte, error) {
330
- // // TODO validation
331
- // // convert - timeout - ditto timeout string
332
- // // error for invalid values
333
- // return json.Marshal(h.Values)
334
- // }
335
-
336
- // UnmarshalJSON unmarshels Headers.
337
- // func (h *Headers) UnmarshalJSON(data []byte) error {
338
- // var m map[string]interface{}
339
-
340
- // if err := json.Unmarshal(data, &m); err != nil {
341
- // return err
342
- // }
343
-
344
- // for k := range m {
345
- // // TODO for all headers
346
- // // error for ivalid values
347
- // if strings.EqualFold(k, HeaderTimeout) && m[k] != nil {
348
- // m[k] = parseTimeout(m[k].(string))
349
- // }
350
- // }
351
-
352
- // h.Values = m
353
-
354
- // return nil
355
- // }
356
-
357
332
// UnmarshalJSON unmarshels Headers.
358
- // func (h *Headers) UnmarshalJSON(data []byte) error {
359
- // temp := make(map[string]interface{})
360
- // if err := json.Unmarshal(data, &temp); err != nil {
361
- // return err
362
- // }
363
- // *h = temp
364
- // return nil
365
- // }
333
+ //
334
+ // The header names are case-insensitive and case-preserving.
335
+ // If there is the same header name as the provided and the difference is
336
+ // in capitalization the new header name will be set.
337
+ func (h * Headers ) UnmarshalJSON (data []byte ) error {
338
+ temp := make (map [string ]interface {})
339
+ if err := json .Unmarshal (data , & temp ); err != nil {
340
+ return err
341
+ }
342
+ for k := range temp {
343
+ for k1 := range * h {
344
+ if strings .EqualFold (k , k1 ) {
345
+ delete (* h , k1 )
346
+ }
347
+ }
348
+ }
349
+ * h = temp
350
+ return nil
351
+ }
366
352
367
353
// With sets new Headers to the existing.
368
354
func (h Headers ) With (opts ... HeaderOpt ) Headers {
0 commit comments