Skip to content

Commit d27ed9a

Browse files
committed
feat: removed deprecated identifiers.
Also support warpped errors in ToExceptionCode, and fixed some lint warnings
1 parent a691ec2 commit d27ed9a

15 files changed

+93
-71
lines changed

.golangci.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ issues:
5555
linters:
5656
- maintidx
5757
- unused
58-
58+
59+
- path: "(.+)_test.go"
60+
linters:
61+
- govet
62+
text: "declaration of \"t\" shadows declaration"
63+
5964
exclude:
6065
- "G104: Errors unhandled." # turn off errcheck
6166
- "declaration of \"[a-z]\" shadows declaration at .*_test.go" # ignore shadowing a single character variables in tests

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ModbusOne [![GoDoc](https://godoc.org/github.com/xiegeo/modbusone?status.svg)](https://godoc.org/github.com/xiegeo/modbusone)
1+
# ModbusOne [![Go Reference](https://pkg.go.dev/badge/github.com/xiegeo/modbusone?utm_source=godoc#section-documentation.svg)](https://pkg.go.dev/github.com/xiegeo/modbusone?utm_source=godoc#section-documentation)
22
A Modbus library for Go, with unified client and server APIs.
33
One implementation to rule them all.
44
<details>
@@ -14,13 +14,13 @@ func handlerGenerator(name string) modbusone.ProtocolHandler {
1414
fmt.Printf("%v ReadHoldingRegisters from %v, quantity %v\n",
1515
name, address, quantity)
1616
r := make([]uint16, quantity)
17-
// application code that fills in r here
17+
// Application code that fills in r here.
1818
return r, nil
1919
},
2020
WriteHoldingRegisters: func(address uint16, values []uint16) error {
2121
fmt.Printf("%v WriteHoldingRegisters from %v, quantity %v\n",
2222
name, address, len(values))
23-
// application code here
23+
// Application code here.
2424
return nil
2525
},
2626
OnErrorImp: func(req modbusone.PDU, errRep modbusone.PDU) {
@@ -29,7 +29,7 @@ func handlerGenerator(name string) modbusone.ProtocolHandler {
2929
}
3030
}
3131

32-
// serial is a fake serial port
32+
// serial is a fake serial port.
3333
type serial struct {
3434
io.ReadCloser
3535
io.WriteCloser
@@ -241,6 +241,17 @@ Development tools:
241241

242242
## Breaking Changes
243243

244+
2022-09-09 v1.0.0
245+
246+
V1 released with the following depreciated identifiers removed:
247+
248+
- The `Server` interface is removed, use `ServerCloser` instead.
249+
- Public global variable `DebugOut` is changed to private. Use `SetDebugOut(w io.Writer)` instead for thread safety.
250+
- Type alias `type ProtocalHandler = ProtocolHandler` removed.
251+
- Function redirect from `GetRTUBidirectionSizeFromHeader` to `GetRTUBidirectionalSizeFromHeader` removed.
252+
- Function redirect from `NewRTUCLient` to `NewRTUClient` removed.
253+
254+
244255
2018-09-27 v0.2.0
245256

246257
- NewRTUPacketReader returns PacketReader interface instead of io.Reader. When a new RTU server or client receives a SerialContext, it will test if it is also a PacketReader, and only create a new NewRTUPacketReader if not.

data.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,20 @@ func BoolsToData(values []bool, fc FunctionCode) ([]byte, error) {
6262

6363
for v := 0; v < count; v++ {
6464
if values[v] {
65+
// set bit in byte to true
6566
byteVal |= 1 << bitNr
6667
}
67-
if bitNr == 7 {
68+
switch {
69+
case bitNr == 7:
70+
// last bit in byte, set data value and go to the next byte
6871
data[byteNr] = byteVal
6972
byteVal = 0
7073
bitNr = 0
7174
byteNr++
72-
} else if v+1 == count {
75+
case v+1 == count:
76+
// last bit, set byte value and exit loop
7377
data[byteNr] = byteVal
74-
} else {
78+
default:
7579
bitNr++
7680
}
7781
}

examples/memory/client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import (
77
"strconv"
88
"strings"
99

10-
"github.com/xiegeo/modbusone"
11-
1210
"github.com/fatih/color"
11+
"github.com/xiegeo/modbusone"
1312
)
1413

1514
func runClient(c *modbusone.RTUClient) {

examples/memory/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func main() {
7171
if *fillData == "am3" {
7272
fillAm3()
7373
}
74-
var device modbusone.Server
74+
var device modbusone.ServerCloser
7575
if *isClient {
7676
if *writeSizeLimit > modbusone.MaxRTUSize || *readSizeLimit > modbusone.MaxRTUSize {
7777
fmt.Fprintf(os.Stderr, "write/read size limit is too big")

examples_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ func handlerGenerator(name string) modbusone.ProtocolHandler {
2020
fmt.Printf("%v ReadHoldingRegisters from %v, quantity %v\n",
2121
name, address, quantity)
2222
r := make([]uint16, quantity)
23-
// application code that fills in r here
23+
// Application code that fills in r here.
2424
return r, nil
2525
},
2626
WriteHoldingRegisters: func(address uint16, values []uint16) error {
2727
fmt.Printf("%v WriteHoldingRegisters from %v, quantity %v\n",
2828
name, address, len(values))
29-
// application code here
29+
// Application code here.
3030
return nil
3131
},
3232
OnErrorImp: func(req modbusone.PDU, errRep modbusone.PDU) {
@@ -35,7 +35,7 @@ func handlerGenerator(name string) modbusone.ProtocolHandler {
3535
}
3636
}
3737

38-
// serial is a fake serial port
38+
// serial is a fake serial port.
3939
type serial struct {
4040
io.ReadCloser
4141
io.WriteCloser

failover_rtu_client.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package modbusone
33
import (
44
"bytes"
55
"encoding/hex"
6+
"errors"
67
"fmt"
78
"sync/atomic"
89
"time"
@@ -18,8 +19,8 @@ type FailoverRTUClient struct {
1819
actions chan rtuAction
1920
}
2021

21-
// FailoverRTUClient is also a Server.
22-
var _ Server = &FailoverRTUClient{}
22+
// FailoverRTUClient is also a ServerCloser.
23+
var _ ServerCloser = &FailoverRTUClient{}
2324

2425
// NewFailoverRTUClient create a new client with failover function communicating over SerialContext with the
2526
// give slaveID as default.
@@ -199,7 +200,7 @@ func (c *FailoverRTUClient) Serve(handler ProtocolHandler) error {
199200
}
200201
rp, err := react.data.GetPDU()
201202
if err != nil {
202-
if err == ErrorCrc {
203+
if errors.Is(err, ErrorCrc) {
203204
atomic.AddInt64(&c.com.Stats().CrcErrors, 1)
204205
} else {
205206
atomic.AddInt64(&c.com.Stats().OtherErrors, 1)

handler2serial_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (s *mockSerial) BytesDelay(n int) time.Duration { return 0 }
6565
func (s *mockSerial) Stats() *Stats { return &s.s }
6666

6767
// TestHandler runs through each of simplymodbus.ca's samples, conforms both
68-
// end-to-end behavior and wire format
68+
// end-to-end behavior and wire format.
6969
func TestHandler(t *testing.T) {
7070
// DebugOut = os.Stdout
7171
slaveID := byte(0x11)

modbus.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,17 @@
55
package modbusone
66

77
import (
8+
"errors"
89
"fmt"
910
"io"
1011
)
1112

12-
// Server is a common interface for all Clients and Servers that use ProtocolHandlers.
13-
// Deprecated: added Closer in ServerCloser.
14-
type Server interface {
15-
Serve(handler ProtocolHandler) error
16-
}
17-
1813
// ServerCloser is the common interface for all Clients and Servers that use ProtocolHandlers.
1914
type ServerCloser interface {
20-
Server
15+
Serve(handler ProtocolHandler) error
2116
io.Closer
2217
}
2318

24-
// ProtocalHandler is a misspelling of ProtocolHandler.
25-
//
26-
// Deprecated: misspelling.
27-
type ProtocalHandler = ProtocolHandler
28-
2919
// ProtocolHandler handles PDUs based on if it is a write or read from the local
3020
// perspective.
3121
type ProtocolHandler interface {
@@ -257,11 +247,12 @@ func ToExceptionCode(err error) ExceptionCode {
257247
if err == nil {
258248
debugf("ToExceptionCode: unexpected covert nil error to ExceptionCode")
259249
}
260-
e, ok := err.(ExceptionCode)
250+
var e ExceptionCode
251+
ok := errors.As(err, &e)
261252
if ok {
262253
return e
263254
}
264-
if err == ErrFcNotSupported {
255+
if errors.Is(err, ErrFcNotSupported) {
265256
return EcIllegalFunction
266257
}
267258
return EcServerDeviceFailure

modbus_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package modbusone
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"reflect"
7+
"testing"
8+
)
9+
10+
func TestToExceptionCode(t *testing.T) {
11+
tests := []struct {
12+
err error
13+
want ExceptionCode
14+
}{
15+
{err: EcIllegalDataAddress, want: EcIllegalDataAddress},
16+
{err: ErrFcNotSupported, want: EcIllegalFunction},
17+
{err: errors.New("other errors"), want: EcServerDeviceFailure},
18+
}
19+
for _, tt := range tests {
20+
t.Run(tt.err.Error(), func(t *testing.T) {
21+
if got := ToExceptionCode(tt.err); !reflect.DeepEqual(got, tt.want) {
22+
t.Errorf("ToExceptionCode() = %v, want %v", got, tt.want)
23+
}
24+
errWarped := fmt.Errorf("warped error for %w", tt.err)
25+
if got := ToExceptionCode(errWarped); !reflect.DeepEqual(got, tt.want) {
26+
t.Errorf("ToExceptionCode() = %v, want %v", got, tt.want)
27+
}
28+
})
29+
}
30+
}

0 commit comments

Comments
 (0)