Skip to content

Commit

Permalink
fixup! Fix issues reported by golangsci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Sep 3, 2024
1 parent 474eb12 commit 1e2c2cf
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 24 deletions.
10 changes: 2 additions & 8 deletions message/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,7 @@ func (options Options) GetUint32(id OptionID) (uint32, error) {
// ContentFormat gets the content format of body.
func (options Options) ContentFormat() (MediaType, error) {
v, err := options.GetUint32(ContentFormat)
if err != nil {
return MediaType(0), err
}
return MediaTypeFromNumber(v)
return math.CastTo[MediaType](v), err
}

// GetString gets the string value of the first option with the given ID.
Expand Down Expand Up @@ -359,10 +356,7 @@ func (options Options) SetAccept(buf []byte, contentFormat MediaType) (Options,
// Accept gets accept option.
func (options Options) Accept() (MediaType, error) {
v, err := options.GetUint32(Accept)
if err != nil {
return MediaType(0), err
}
return MediaTypeFromNumber(v)
return math.CastTo[MediaType](v), err
}

// Find returns range of type options. First number is index and second number is index of next option type.
Expand Down
15 changes: 5 additions & 10 deletions message/pool/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/go-coap/v3/message/codes"
"github.com/plgd-dev/go-coap/v3/net"
"github.com/plgd-dev/go-coap/v3/pkg/math"
"go.uber.org/atomic"
)

Expand Down Expand Up @@ -373,10 +374,7 @@ func (r *Message) AddOptionUint32(opt message.OptionID, value uint32) {

func (r *Message) ContentFormat() (message.MediaType, error) {
v, err := r.GetOptionUint32(message.ContentFormat)
if err != nil {
return message.MediaType(0), err
}
return message.MediaTypeFromNumber(v)
return math.CastTo[message.MediaType](v), err
}

func (r *Message) HasOption(id message.OptionID) bool {
Expand All @@ -395,18 +393,15 @@ func (r *Message) Observe() (uint32, error) {
return r.GetOptionUint32(message.Observe)
}

// SetAccept set's accept option.
// SetAccept sets accept option.
func (r *Message) SetAccept(contentFormat message.MediaType) {
r.SetOptionUint32(message.Accept, uint32(contentFormat))
}

// Accept get's accept option.
// Accept gets accept option.
func (r *Message) Accept() (message.MediaType, error) {
v, err := r.GetOptionUint32(message.Accept)
if err != nil {
return message.MediaType(0), err
}
return message.MediaTypeFromNumber(v)
return math.CastTo[message.MediaType](v), err
}

func (r *Message) BodySize() (int64, error) {
Expand Down
2 changes: 1 addition & 1 deletion net/blockwise/blockwise.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func DecodeBlockOption(blockVal uint32) (szx SZX, blockNumber int64, moreBlocksF
return
}

szx = SZX(blockVal & szxMask) // masking for the SZX
szx = math.CastTo[SZX](blockVal & szxMask) // masking for the SZX
if (blockVal & moreBlocksFollowingMask) != 0 { // masking for the "M"
moreBlocksFollowing = true
}
Expand Down
4 changes: 2 additions & 2 deletions options/commonOptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (o MuxHandlerOpt) UDPClientApply(cfg *udpClient.Config) {
cfg.Handler = mux.ToHandler[*udpClient.Conn](o.m)
}

// WithMux set's multiplexer for handle requests.
// WithMux sets multiplexer for handle requests.
func WithMux(m mux.Handler) MuxHandlerOpt {
return MuxHandlerOpt{
m: m,
Expand Down Expand Up @@ -148,7 +148,7 @@ func (o ContextOpt) UDPClientApply(cfg *udpClient.Config) {
cfg.Ctx = o.ctx
}

// WithContext set's parent context of server.
// WithContext sets parent context of server.
func WithContext(ctx context.Context) ContextOpt {
return ContextOpt{ctx: ctx}
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/math/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"golang.org/x/exp/constraints"
)

// Max returns maximal value for given integer type
func Max[T constraints.Integer]() T {
size := unsafe.Sizeof(T(0))
switch reflect.TypeOf((*T)(nil)).Elem().Kind() {
Expand All @@ -21,6 +22,7 @@ func Max[T constraints.Integer]() T {
}
}

// Min returns minimal value for given integer type
func Min[T constraints.Integer]() T {
size := unsafe.Sizeof(T(0))
switch reflect.TypeOf((*T)(nil)).Elem().Kind() {
Expand All @@ -33,6 +35,7 @@ func Min[T constraints.Integer]() T {
}
}

// CastTo casts one integer type to another with bounds checking and returns error in case of overflow
func SafeCastTo[T, F constraints.Integer](from F) (T, error) {
if from > 0 && uint64(Max[T]()) < uint64(from) {
return T(0), fmt.Errorf("value(%v) exceeds the maximum value for type(%v)", from, Max[T]())
Expand All @@ -43,10 +46,12 @@ func SafeCastTo[T, F constraints.Integer](from F) (T, error) {
return T(from), nil
}

// CastTo casts one integer type to another without bounds checking
func CastTo[T, F constraints.Integer](from F) T {
return T(from)
}

// MustSafeCastTo casts one integer type to another with bounds checking and panics in case of overflow
func MustSafeCastTo[T, F constraints.Integer](from F) T {
to, err := SafeCastTo[T](from)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions tcp/coder/coder.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func (c *Coder) Size(m message.Message) (int, error) {

func getHeader(messageLength int) (uint8, []byte) {
if messageLength < MessageLength13Base {
return uint8(messageLength), nil
return math.CastTo[uint8](messageLength), nil
}
if messageLength < MessageLength14Base {
extLen := messageLength - MessageLength13Base
extLenBytes := []byte{uint8(extLen)}
extLenBytes := []byte{math.CastTo[uint8](extLen)}
return 13, extLenBytes
}
if messageLength < MessageLength15Base {
Expand Down Expand Up @@ -115,7 +115,7 @@ func (c *Coder) Encode(m message.Message, buf []byte) (int, error) {
}

// Length and TKL nibbles.
hdr[hdrOff] = uint8(0xf&len(m.Token)) | (lenNib << 4)
hdr[hdrOff] = math.CastTo[uint8](len(m.Token)) | (lenNib << 4)
hdrOff++

// Extended length, if present.
Expand Down

0 comments on commit 1e2c2cf

Please sign in to comment.