Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Spell endianness correctly #13752

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ package internal

import "encoding/binary"

var HostEndianess = binary.BigEndian
var HostEndianness = binary.BigEndian
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ package internal

import "encoding/binary"

var HostEndianess = binary.LittleEndian
var HostEndianness = binary.LittleEndian
14 changes: 7 additions & 7 deletions plugins/inputs/modbus/type_conversions16.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type convert16 func([]byte) uint16

func endianessConverter16(byteOrder string) (convert16, error) {
func endiannessConverter16(byteOrder string) (convert16, error) {
switch byteOrder {
case "ABCD", "CDAB": // Big endian (Motorola)
return binary.BigEndian.Uint16, nil
Expand All @@ -21,7 +21,7 @@ func endianessConverter16(byteOrder string) (convert16, error) {

// I16 - no scale
func determineConverterI16(outType, byteOrder string) (fieldConverterFunc, error) {
tohost, err := endianessConverter16(byteOrder)
tohost, err := endiannessConverter16(byteOrder)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -49,7 +49,7 @@ func determineConverterI16(outType, byteOrder string) (fieldConverterFunc, error

// U16 - no scale
func determineConverterU16(outType, byteOrder string) (fieldConverterFunc, error) {
tohost, err := endianessConverter16(byteOrder)
tohost, err := endiannessConverter16(byteOrder)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func determineConverterU16(outType, byteOrder string) (fieldConverterFunc, error

// F16 - no scale
func determineConverterF16(outType, byteOrder string) (fieldConverterFunc, error) {
tohost, err := endianessConverter16(byteOrder)
tohost, err := endiannessConverter16(byteOrder)
if err != nil {
return nil, err
}
Expand All @@ -100,7 +100,7 @@ func determineConverterF16(outType, byteOrder string) (fieldConverterFunc, error

// I16 - scale
func determineConverterI16Scale(outType, byteOrder string, scale float64) (fieldConverterFunc, error) {
tohost, err := endianessConverter16(byteOrder)
tohost, err := endiannessConverter16(byteOrder)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -132,7 +132,7 @@ func determineConverterI16Scale(outType, byteOrder string, scale float64) (field

// U16 - scale
func determineConverterU16Scale(outType, byteOrder string, scale float64) (fieldConverterFunc, error) {
tohost, err := endianessConverter16(byteOrder)
tohost, err := endiannessConverter16(byteOrder)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func determineConverterU16Scale(outType, byteOrder string, scale float64) (field

// F16 - scale
func determineConverterF16Scale(outType, byteOrder string, scale float64) (fieldConverterFunc, error) {
tohost, err := endianessConverter16(byteOrder)
tohost, err := endiannessConverter16(byteOrder)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions plugins/inputs/modbus/type_conversions32.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func binaryLSWBEU32(b []byte) uint32 {
return uint32(binary.BigEndian.Uint16(b[2:]))<<16 | uint32(binary.BigEndian.Uint16(b[0:]))
}

func endianessConverter32(byteOrder string) (convert32, error) {
func endiannessConverter32(byteOrder string) (convert32, error) {
switch byteOrder {
case "ABCD": // Big endian (Motorola)
return binary.BigEndian.Uint32, nil
Expand All @@ -34,7 +34,7 @@ func endianessConverter32(byteOrder string) (convert32, error) {

// I32 - no scale
func determineConverterI32(outType, byteOrder string) (fieldConverterFunc, error) {
tohost, err := endianessConverter32(byteOrder)
tohost, err := endiannessConverter32(byteOrder)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -62,7 +62,7 @@ func determineConverterI32(outType, byteOrder string) (fieldConverterFunc, error

// U32 - no scale
func determineConverterU32(outType, byteOrder string) (fieldConverterFunc, error) {
tohost, err := endianessConverter32(byteOrder)
tohost, err := endiannessConverter32(byteOrder)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func determineConverterU32(outType, byteOrder string) (fieldConverterFunc, error

// F32 - no scale
func determineConverterF32(outType, byteOrder string) (fieldConverterFunc, error) {
tohost, err := endianessConverter32(byteOrder)
tohost, err := endiannessConverter32(byteOrder)
if err != nil {
return nil, err
}
Expand All @@ -113,7 +113,7 @@ func determineConverterF32(outType, byteOrder string) (fieldConverterFunc, error

// I32 - scale
func determineConverterI32Scale(outType, byteOrder string, scale float64) (fieldConverterFunc, error) {
tohost, err := endianessConverter32(byteOrder)
tohost, err := endiannessConverter32(byteOrder)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func determineConverterI32Scale(outType, byteOrder string, scale float64) (field

// U32 - scale
func determineConverterU32Scale(outType, byteOrder string, scale float64) (fieldConverterFunc, error) {
tohost, err := endianessConverter32(byteOrder)
tohost, err := endiannessConverter32(byteOrder)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -177,7 +177,7 @@ func determineConverterU32Scale(outType, byteOrder string, scale float64) (field

// F32 - scale
func determineConverterF32Scale(outType, byteOrder string, scale float64) (fieldConverterFunc, error) {
tohost, err := endianessConverter32(byteOrder)
tohost, err := endiannessConverter32(byteOrder)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions plugins/inputs/modbus/type_conversions64.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func binaryLSWBEU64(b []byte) uint64 {
uint64(binary.BigEndian.Uint16(b[2:]))<<16 | uint64(binary.BigEndian.Uint16(b[0:]))
}

func endianessConverter64(byteOrder string) (convert64, error) {
func endiannessConverter64(byteOrder string) (convert64, error) {
switch byteOrder {
case "ABCD": // Big endian (Motorola)
return binary.BigEndian.Uint64, nil
Expand All @@ -36,7 +36,7 @@ func endianessConverter64(byteOrder string) (convert64, error) {

// I64 - no scale
func determineConverterI64(outType, byteOrder string) (fieldConverterFunc, error) {
tohost, err := endianessConverter64(byteOrder)
tohost, err := endiannessConverter64(byteOrder)
if err != nil {
return nil, err
}
Expand All @@ -62,7 +62,7 @@ func determineConverterI64(outType, byteOrder string) (fieldConverterFunc, error

// U64 - no scale
func determineConverterU64(outType, byteOrder string) (fieldConverterFunc, error) {
tohost, err := endianessConverter64(byteOrder)
tohost, err := endiannessConverter64(byteOrder)
if err != nil {
return nil, err
}
Expand All @@ -86,7 +86,7 @@ func determineConverterU64(outType, byteOrder string) (fieldConverterFunc, error

// F64 - no scale
func determineConverterF64(outType, byteOrder string) (fieldConverterFunc, error) {
tohost, err := endianessConverter64(byteOrder)
tohost, err := endiannessConverter64(byteOrder)
if err != nil {
return nil, err
}
Expand All @@ -103,7 +103,7 @@ func determineConverterF64(outType, byteOrder string) (fieldConverterFunc, error

// I64 - scale
func determineConverterI64Scale(outType, byteOrder string, scale float64) (fieldConverterFunc, error) {
tohost, err := endianessConverter64(byteOrder)
tohost, err := endiannessConverter64(byteOrder)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func determineConverterI64Scale(outType, byteOrder string, scale float64) (field

// U64 - scale
func determineConverterU64Scale(outType, byteOrder string, scale float64) (fieldConverterFunc, error) {
tohost, err := endianessConverter64(byteOrder)
tohost, err := endiannessConverter64(byteOrder)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func determineConverterU64Scale(outType, byteOrder string, scale float64) (field

// F64 - scale
func determineConverterF64Scale(outType, byteOrder string, scale float64) (fieldConverterFunc, error) {
tohost, err := endianessConverter64(byteOrder)
tohost, err := endiannessConverter64(byteOrder)
if err != nil {
return nil, err
}
Expand Down
18 changes: 9 additions & 9 deletions plugins/inputs/modbus/type_conversions8.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
)

func endianessIndex8(byteOrder string, low bool) (int, error) {
func endiannessIndex8(byteOrder string, low bool) (int, error) {
switch byteOrder {
case "ABCD": // Big endian (Motorola)
if low {
Expand All @@ -22,7 +22,7 @@ func endianessIndex8(byteOrder string, low bool) (int, error) {

// I8 lower byte - no scale
func determineConverterI8L(outType, byteOrder string) (fieldConverterFunc, error) {
idx, err := endianessIndex8(byteOrder, true)
idx, err := endiannessIndex8(byteOrder, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -50,7 +50,7 @@ func determineConverterI8L(outType, byteOrder string) (fieldConverterFunc, error

// I8 higher byte - no scale
func determineConverterI8H(outType, byteOrder string) (fieldConverterFunc, error) {
idx, err := endianessIndex8(byteOrder, false)
idx, err := endiannessIndex8(byteOrder, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -78,7 +78,7 @@ func determineConverterI8H(outType, byteOrder string) (fieldConverterFunc, error

// U8 lower byte - no scale
func determineConverterU8L(outType, byteOrder string) (fieldConverterFunc, error) {
idx, err := endianessIndex8(byteOrder, true)
idx, err := endiannessIndex8(byteOrder, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func determineConverterU8L(outType, byteOrder string) (fieldConverterFunc, error

// U8 higher byte - no scale
func determineConverterU8H(outType, byteOrder string) (fieldConverterFunc, error) {
idx, err := endianessIndex8(byteOrder, false)
idx, err := endiannessIndex8(byteOrder, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func determineConverterU8H(outType, byteOrder string) (fieldConverterFunc, error

// I8 lower byte - scale
func determineConverterI8LScale(outType, byteOrder string, scale float64) (fieldConverterFunc, error) {
idx, err := endianessIndex8(byteOrder, true)
idx, err := endiannessIndex8(byteOrder, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -166,7 +166,7 @@ func determineConverterI8LScale(outType, byteOrder string, scale float64) (field

// I8 higher byte - scale
func determineConverterI8HScale(outType, byteOrder string, scale float64) (fieldConverterFunc, error) {
idx, err := endianessIndex8(byteOrder, false)
idx, err := endiannessIndex8(byteOrder, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -198,7 +198,7 @@ func determineConverterI8HScale(outType, byteOrder string, scale float64) (field

// U8 lower byte - scale
func determineConverterU8LScale(outType, byteOrder string, scale float64) (fieldConverterFunc, error) {
idx, err := endianessIndex8(byteOrder, true)
idx, err := endiannessIndex8(byteOrder, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func determineConverterU8LScale(outType, byteOrder string, scale float64) (field

// U8 higher byte - scale
func determineConverterU8HScale(outType, byteOrder string, scale float64) (fieldConverterFunc, error) {
idx, err := endianessIndex8(byteOrder, false)
idx, err := endiannessIndex8(byteOrder, false)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions plugins/inputs/powerdns_recursor/protocol_commons.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func writeNativeUIntToConn(conn net.Conn, value uint) error {

switch uintSizeInBytes {
case 4:
internal.HostEndianess.PutUint32(intData, uint32(value))
internal.HostEndianness.PutUint32(intData, uint32(value))
case 8:
internal.HostEndianess.PutUint64(intData, uint64(value))
internal.HostEndianness.PutUint64(intData, uint64(value))
default:
return fmt.Errorf("unsupported system configuration")
}
Expand All @@ -79,9 +79,9 @@ func readNativeUIntFromConn(conn net.Conn) (uint, error) {

switch uintSizeInBytes {
case 4:
return uint(internal.HostEndianess.Uint32(intData)), nil
return uint(internal.HostEndianness.Uint32(intData)), nil
case 8:
return uint(internal.HostEndianess.Uint64(intData)), nil
return uint(internal.HostEndianness.Uint64(intData)), nil
default:
return 0, fmt.Errorf("unsupported system configuration")
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/socket_listener/socket_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (sl *SocketListener) Init() error {
case "le":
order = binary.LittleEndian
default:
return fmt.Errorf("invalid 'endianess' %q", sl.SplittingLengthField.Endianness)
return fmt.Errorf("invalid 'endianness' %q", sl.SplittingLengthField.Endianness)
}

switch sl.SplittingLengthField.Bytes {
Expand Down
4 changes: 2 additions & 2 deletions plugins/parsers/binary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ user-specified configurations.
## Specify the endianness of the data.
## Available values are "be" (big-endian), "le" (little-endian) and "host",
## where "host" means the same endianness as the machine running Telegraf.
# endianess = "host"
# endianness = "host"

## Interpret input as string containing hex-encoded data.
# hex_encoding = false
Expand Down Expand Up @@ -282,7 +282,7 @@ you can use the following configuration
[[inputs.file]]
files = ["messageA.bin", "messageB.bin", "messageC.bin"]
data_format = "binary"
endianess = "le"
endianness = "le"

[[inputs.file.binary]]
metric_name = "messageA"
Expand Down
8 changes: 4 additions & 4 deletions plugins/parsers/binary/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ func TestEntryConvertType(t *testing.T) {
testdata := []byte{0x01, 0x02, 0x03, 0x04}

e := &Entry{Type: "garbage"}
_, err := e.convertType(testdata, internal.HostEndianess)
_, err := e.convertType(testdata, internal.HostEndianness)
require.EqualError(t, err, `cannot handle type "garbage"`)
}

func TestEntryConvertTimeType(t *testing.T) {
testdata := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}

e := &Entry{Type: "unix_ns", location: time.UTC}
_, err := e.convertTimeType(testdata, internal.HostEndianess)
_, err := e.convertTimeType(testdata, internal.HostEndianness)
require.EqualError(t, err, `too many bytes 9 vs 8`)
}

func TestConvertNumericType(t *testing.T) {
testdata := []byte{0x01, 0x02, 0x03, 0x04}

_, err := convertNumericType(testdata, "garbage", internal.HostEndianess)
_, err := convertNumericType(testdata, "garbage", internal.HostEndianness)
require.EqualError(t, err, `cannot determine length for type "garbage"`)

_, err = convertNumericType(testdata, "uint8", internal.HostEndianess)
_, err = convertNumericType(testdata, "uint8", internal.HostEndianness)
require.EqualError(t, err, `too many bytes 4 vs 1`)
}
13 changes: 9 additions & 4 deletions plugins/parsers/binary/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (

type Parser struct {
AllowNoMatch bool `toml:"allow_no_match"`
Endianess string `toml:"endianess"`
Endianess string `toml:"endianess" deprecated:"1.27.4;use 'endianness' instead"`
Endianness string `toml:"endianness"`
Configs []Config `toml:"binary"`
HexEncoding bool `toml:"hex_encoding"`
Log telegraf.Logger `toml:"-"`
Expand All @@ -26,15 +27,19 @@ type Parser struct {
}

func (p *Parser) Init() error {
switch p.Endianess {
if p.Endianess != "" && p.Endianness == "" {
p.Endianness = p.Endianess
}

switch p.Endianness {
case "le":
p.converter = binary.LittleEndian
case "be":
p.converter = binary.BigEndian
case "", "host":
p.converter = internal.HostEndianess
p.converter = internal.HostEndianness
default:
return fmt.Errorf("unknown endianess %q", p.Endianess)
return fmt.Errorf("unknown endianness %q", p.Endianness)
}

// Pre-process the configurations
Expand Down
Loading
Loading