Skip to content

Commit 75000ae

Browse files
Danielius1922Daniel Adam
authored andcommitted
Add AddETag and ETags functions
Modify the interface bacause ETag option is repeatable: - AddETag allows to add value for the option without replacing the previously existing ones - ETags writes all values for the option to output array
1 parent 13173c3 commit 75000ae

File tree

5 files changed

+157
-94
lines changed

5 files changed

+157
-94
lines changed

message/getETag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"io"
88
)
99

10-
// GetETag calculate ETag from payload via CRC64
10+
// GetETag calculates ETag from payload via CRC64
1111
func GetETag(r io.ReadSeeker) ([]byte, error) {
1212
if r == nil {
1313
return make([]byte, 8), nil

message/option.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,15 @@ func extendOpt(opt int) (int, int) {
241241
return opt, ext
242242
}
243243

244+
// VerifyOptLen checks whether valueLen is within (min, max) length limits for given option.
245+
func VerifyOptLen(optID OptionID, valueLen int) bool {
246+
def := CoapOptionDefs[optID]
247+
if valueLen < int(def.MinLen) || valueLen > int(def.MaxLen) {
248+
return false
249+
}
250+
return true
251+
}
252+
244253
func marshalOptionHeaderExt(buf []byte, opt, ext int) (int, error) {
245254
switch opt {
246255
case ExtendOptionByteCode:

message/options.go

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func setPath(options Options, optionID OptionID, buf []byte, path string) (Optio
7575

7676
// SetPath splits path by '/' to URIPath options and copies it to buffer.
7777
//
78-
// Return's modified options, number of used buf bytes and error if occurs.
78+
// Returns modified options, number of used buf bytes and error if occurs.
7979
//
8080
// @note the url encoded into URIHost, URIPort, URIPath is expected to be
8181
// absolute (https://www.rfc-editor.org/rfc/rfc7252.txt)
@@ -85,7 +85,7 @@ func (options Options) SetPath(buf []byte, path string) (Options, int, error) {
8585

8686
// SetLocationPath splits path by '/' to LocationPath options and copies it to buffer.
8787
//
88-
// Return's modified options, number of used buf bytes and error if occurs.
88+
// Returns modified options, number of used buf bytes and error if occurs.
8989
//
9090
// @note the url encoded into LocationPath is expected to be
9191
// absolute (https://www.rfc-editor.org/rfc/rfc7252.txt)
@@ -119,7 +119,7 @@ func (options Options) path(buf []byte, id OptionID) (int, error) {
119119

120120
// Path joins URIPath options by '/' to the buf.
121121
//
122-
// Return's number of used buf bytes or error when occurs.
122+
// Returns number of used buf bytes or error when occurs.
123123
func (options Options) Path() (string, error) {
124124
buf := make([]byte, 32)
125125
m, err := options.path(buf, URIPath)
@@ -136,7 +136,7 @@ func (options Options) Path() (string, error) {
136136

137137
// LocationPath joins Location-Path options by '/' to the buf.
138138
//
139-
// Return's number of used buf bytes or error when occurs.
139+
// Returns number of used buf bytes or error when occurs.
140140
func (options Options) LocationPath() (string, error) {
141141
buf := make([]byte, 32)
142142
m, err := options.path(buf, LocationPath)
@@ -151,29 +151,29 @@ func (options Options) LocationPath() (string, error) {
151151
return string(buf), nil
152152
}
153153

154-
// SetString replace's/store's string option to options.
154+
// SetString replaces/stores string option to options.
155155
//
156-
// Return's modified options, number of used buf bytes and error if occurs.
156+
// Returns modified options, number of used buf bytes and error if occurs.
157157
func (options Options) SetString(buf []byte, id OptionID, str string) (Options, int, error) {
158158
data := []byte(str)
159159
return options.SetBytes(buf, id, data)
160160
}
161161

162-
// AddString append's string option to options.
162+
// AddString appends string option to options.
163163
//
164-
// Return's modified options, number of used buf bytes and error if occurs.
164+
// Returns modified options, number of used buf bytes and error if occurs.
165165
func (options Options) AddString(buf []byte, id OptionID, str string) (Options, int, error) {
166166
data := []byte(str)
167167
return options.AddBytes(buf, id, data)
168168
}
169169

170-
// HasOption return's true is option exist in options.
170+
// HasOption returns true is option exist in options.
171171
func (options Options) HasOption(id OptionID) bool {
172172
_, _, err := options.Find(id)
173173
return err == nil
174174
}
175175

176-
// GetUint32s get's all options with same id.
176+
// GetUint32s gets all options with same id.
177177
func (options Options) GetUint32s(id OptionID, r []uint32) (int, error) {
178178
firstIdx, lastIdx, err := options.Find(id)
179179
if err != nil {
@@ -194,7 +194,7 @@ func (options Options) GetUint32s(id OptionID, r []uint32) (int, error) {
194194
return idx, nil
195195
}
196196

197-
// GetUint32 get's first option with id of type uint32.
197+
// GetUint32 gets the uin32 value of the first option with the given ID.
198198
func (options Options) GetUint32(id OptionID) (uint32, error) {
199199
firstIdx, _, err := options.Find(id)
200200
if err != nil {
@@ -204,13 +204,13 @@ func (options Options) GetUint32(id OptionID) (uint32, error) {
204204
return val, err
205205
}
206206

207-
// ContentFormat get's content format of body.
207+
// ContentFormat gets the content format of body.
208208
func (options Options) ContentFormat() (MediaType, error) {
209209
v, err := options.GetUint32(ContentFormat)
210210
return MediaType(v), err
211211
}
212212

213-
// GetString get's first option with id of type string.
213+
// GetString gets the string value of the first option with the given ID.
214214
func (options Options) GetString(id OptionID) (string, error) {
215215
firstIdx, _, err := options.Find(id)
216216
if err != nil {
@@ -219,9 +219,41 @@ func (options Options) GetString(id OptionID) (string, error) {
219219
return string(options[firstIdx].Value), nil
220220
}
221221

222-
// SetBytes replace's/store's byte's option to options.
222+
// GetStrings gets string array of all options with the given id.
223+
func (options Options) GetStrings(id OptionID, r []string) (int, error) {
224+
firstIdx, lastIdx, err := options.Find(id)
225+
if err != nil {
226+
return 0, err
227+
}
228+
if len(r) < lastIdx-firstIdx {
229+
return lastIdx - firstIdx, ErrTooSmall
230+
}
231+
var idx int
232+
for i := firstIdx; i < lastIdx; i++ {
233+
r[idx] = string(options[i].Value)
234+
idx++
235+
}
236+
237+
return idx, nil
238+
}
239+
240+
// Queries gets URIQuery parameters.
241+
func (options Options) Queries() ([]string, error) {
242+
q := make([]string, 4)
243+
n, err := options.GetStrings(URIQuery, q)
244+
if errors.Is(err, ErrTooSmall) {
245+
q = append(q, make([]string, n-len(q))...)
246+
n, err = options.GetStrings(URIQuery, q)
247+
}
248+
if err != nil {
249+
return nil, err
250+
}
251+
return q[:n], nil
252+
}
253+
254+
// SetBytes replaces/stores bytes of a option to options.
223255
//
224-
// Return's modified options, number of used buf bytes and error if occurs.
256+
// Returns modified options, number of used buf bytes and error if occurs.
225257
func (options Options) SetBytes(buf []byte, id OptionID, data []byte) (Options, int, error) {
226258
if len(buf) < len(data) {
227259
return options, len(data), ErrTooSmall
@@ -233,9 +265,9 @@ func (options Options) SetBytes(buf []byte, id OptionID, data []byte) (Options,
233265
return options.Set(Option{ID: id, Value: buf[:len(data)]}), len(data), nil
234266
}
235267

236-
// AddBytes append's byte's option to options.
268+
// AddBytes appends bytes of a option option to options.
237269
//
238-
// Return's modified options, number of used buf bytes and error if occurs.
270+
// Returns modified options, number of used buf bytes and error if occurs.
239271
func (options Options) AddBytes(buf []byte, id OptionID, data []byte) (Options, int, error) {
240272
if len(buf) < len(data) {
241273
return options, len(data), ErrTooSmall
@@ -247,7 +279,7 @@ func (options Options) AddBytes(buf []byte, id OptionID, data []byte) (Options,
247279
return options.Add(Option{ID: id, Value: buf[:len(data)]}), len(data), nil
248280
}
249281

250-
// GetBytes get's first option with id of type byte's.
282+
// GetBytes gets bytes of the first option with given id.
251283
func (options Options) GetBytes(id OptionID) ([]byte, error) {
252284
firstIdx, _, err := options.Find(id)
253285
if err != nil {
@@ -256,39 +288,7 @@ func (options Options) GetBytes(id OptionID) ([]byte, error) {
256288
return options[firstIdx].Value, nil
257289
}
258290

259-
// GetStrings get's all options with same id.
260-
func (options Options) GetStrings(id OptionID, r []string) (int, error) {
261-
firstIdx, lastIdx, err := options.Find(id)
262-
if err != nil {
263-
return 0, err
264-
}
265-
if len(r) < lastIdx-firstIdx {
266-
return lastIdx - firstIdx, ErrTooSmall
267-
}
268-
var idx int
269-
for i := firstIdx; i < lastIdx; i++ {
270-
r[idx] = string(options[i].Value)
271-
idx++
272-
}
273-
274-
return idx, nil
275-
}
276-
277-
// Queries get's URIQuery parameters.
278-
func (options Options) Queries() ([]string, error) {
279-
q := make([]string, 4)
280-
n, err := options.GetStrings(URIQuery, q)
281-
if errors.Is(err, ErrTooSmall) {
282-
q = append(q, make([]string, n-len(q))...)
283-
n, err = options.GetStrings(URIQuery, q)
284-
}
285-
if err != nil {
286-
return nil, err
287-
}
288-
return q[:n], nil
289-
}
290-
291-
// GetBytess get's all options with same id.
291+
// GetBytess gets array of bytes of all options with the same id.
292292
func (options Options) GetBytess(id OptionID, r [][]byte) (int, error) {
293293
firstIdx, lastIdx, err := options.Find(id)
294294
if err != nil {
@@ -306,9 +306,9 @@ func (options Options) GetBytess(id OptionID, r [][]byte) (int, error) {
306306
return idx, nil
307307
}
308308

309-
// AddUint32 append's uint32 option to options.
309+
// AddUint32 appends uint32 option to options.
310310
//
311-
// Return's modified options, number of used buf bytes and error if occurs.
311+
// Returns modified options, number of used buf bytes and error if occurs.
312312
func (options Options) AddUint32(buf []byte, id OptionID, value uint32) (Options, int, error) {
313313
enc, err := EncodeUint32(buf, value)
314314
if err != nil {
@@ -318,9 +318,9 @@ func (options Options) AddUint32(buf []byte, id OptionID, value uint32) (Options
318318
return o, enc, err
319319
}
320320

321-
// SetUint32 replace's/store's uint32 option to options.
321+
// SetUint32 replaces/stores uint32 option to options.
322322
//
323-
// Return's modified options, number of used buf bytes and error if occurs.
323+
// Returns modified options, number of used buf bytes and error if occurs.
324324
func (options Options) SetUint32(buf []byte, id OptionID, value uint32) (Options, int, error) {
325325
enc, err := EncodeUint32(buf, value)
326326
if err != nil {
@@ -330,33 +330,33 @@ func (options Options) SetUint32(buf []byte, id OptionID, value uint32) (Options
330330
return o, enc, err
331331
}
332332

333-
// SetContentFormat set's ContentFormat option.
333+
// SetContentFormat sets ContentFormat option.
334334
func (options Options) SetContentFormat(buf []byte, contentFormat MediaType) (Options, int, error) {
335335
return options.SetUint32(buf, ContentFormat, uint32(contentFormat))
336336
}
337337

338-
// SetObserve set's ContentFormat option.
338+
// SetObserve sets ContentFormat option.
339339
func (options Options) SetObserve(buf []byte, observe uint32) (Options, int, error) {
340340
return options.SetUint32(buf, Observe, observe)
341341
}
342342

343-
// Observe get's observe option.
343+
// Observe gets observe option.
344344
func (options Options) Observe() (uint32, error) {
345345
return options.GetUint32(Observe)
346346
}
347347

348-
// SetAccept set's accept option.
348+
// SetAccept sets accept option.
349349
func (options Options) SetAccept(buf []byte, contentFormat MediaType) (Options, int, error) {
350350
return options.SetUint32(buf, Accept, uint32(contentFormat))
351351
}
352352

353-
// Accept get's accept option.
353+
// Accept gets accept option.
354354
func (options Options) Accept() (MediaType, error) {
355355
v, err := options.GetUint32(Accept)
356356
return MediaType(v), err
357357
}
358358

359-
// Find return's range of type options. First number is index and second number is index of next option type.
359+
// Find returns range of type options. First number is index and second number is index of next option type.
360360
func (options Options) Find(ID OptionID) (int, int, error) {
361361
idxPre, idxPost := options.findPosition(ID)
362362
if idxPre == -1 && idxPost == 0 {
@@ -404,9 +404,9 @@ func (options Options) findPosition(ID OptionID) (minIdx int, maxIdx int) {
404404
}
405405
}
406406

407-
// Set replace's/store's option at options.
407+
// Set replaces/stores option at options.
408408
//
409-
// Return's modified options.
409+
// Returns modified options.
410410
func (options Options) Set(opt Option) Options {
411411
idxPre, idxPost := options.findPosition(opt.ID)
412412
if idxPre == -1 && idxPost == -1 {
@@ -463,7 +463,7 @@ func (options Options) Set(opt Option) Options {
463463
return options
464464
}
465465

466-
// Add append's option to options.
466+
// Add appends option to options.
467467
func (options Options) Add(opt Option) Options {
468468
_, idxPost := options.findPosition(opt.ID)
469469
if idxPost == -1 {
@@ -481,7 +481,7 @@ func (options Options) Add(opt Option) Options {
481481
return options
482482
}
483483

484-
// Remove remove's all options with ID.
484+
// Remove removes all options with ID.
485485
func (options Options) Remove(ID OptionID) Options {
486486
idxPre, idxPost, err := options.Find(ID)
487487
if err != nil {
@@ -498,9 +498,9 @@ func (options Options) Remove(ID OptionID) Options {
498498
return options
499499
}
500500

501-
// Marshal marshal's options to buf.
501+
// Marshal marshals options to buf.
502502
//
503-
// Return's number of used buf byte's.
503+
// Returns the number of used buf bytes.
504504
func (options Options) Marshal(buf []byte) (int, error) {
505505
previousID := OptionID(0)
506506
length := 0
@@ -536,7 +536,7 @@ func (options Options) Marshal(buf []byte) (int, error) {
536536
return length, nil
537537
}
538538

539-
// Unmarshal unmarshal's data bytes to options and returns number of consumned byte's.
539+
// Unmarshal unmarshals data bytes to options and returns the number of consumed bytes.
540540
func (options *Options) Unmarshal(data []byte, optionDefs map[OptionID]OptionDef) (int, error) {
541541
prev := 0
542542
processed := 0
@@ -595,9 +595,9 @@ func (options *Options) Unmarshal(data []byte, optionDefs map[OptionID]OptionDef
595595
return processed, nil
596596
}
597597

598-
// ResetOptionsTo reset's options to in options.
598+
// ResetOptionsTo resets options to in options.
599599
//
600-
// Return's modified options, number of used buf bytes and error if occurs.
600+
// Returns modified options, number of used buf bytes and error if occurs.
601601
func (options Options) ResetOptionsTo(buf []byte, in Options) (Options, int, error) {
602602
opts := options[:0]
603603
used := 0

0 commit comments

Comments
 (0)