Skip to content

Commit

Permalink
refactor(helpers): share them as separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
simylein committed Jan 24, 2025
1 parent f6e5445 commit d7b146e
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/truvami/decoder/internal/logger"
helpers "github.com/truvami/decoder/pkg/common"
"github.com/truvami/decoder/pkg/decoder"
"github.com/truvami/decoder/pkg/decoder/helpers"
"github.com/truvami/decoder/pkg/decoder/nomadxl/v1"
"github.com/truvami/decoder/pkg/decoder/nomadxs/v1"
"github.com/truvami/decoder/pkg/decoder/smartlabel/v1"
Expand Down
2 changes: 1 addition & 1 deletion cmd/nomadxl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/spf13/cobra"
"github.com/truvami/decoder/internal/logger"
"github.com/truvami/decoder/pkg/decoder/helpers"
helpers "github.com/truvami/decoder/pkg/common"
"github.com/truvami/decoder/pkg/decoder/nomadxl/v1"
"go.uber.org/zap"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/nomadxs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/spf13/cobra"
"github.com/truvami/decoder/internal/logger"
"github.com/truvami/decoder/pkg/decoder/helpers"
helpers "github.com/truvami/decoder/pkg/common"
"github.com/truvami/decoder/pkg/decoder/nomadxs/v1"
"go.uber.org/zap"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/smartlabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/spf13/cobra"
"github.com/truvami/decoder/internal/logger"
"github.com/truvami/decoder/pkg/decoder/helpers"
helpers "github.com/truvami/decoder/pkg/common"
"github.com/truvami/decoder/pkg/decoder/smartlabel/v1"
"github.com/truvami/decoder/pkg/loracloud"
"go.uber.org/zap"
Expand Down
2 changes: 1 addition & 1 deletion cmd/tagsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/spf13/cobra"
"github.com/truvami/decoder/internal/logger"
"github.com/truvami/decoder/pkg/decoder/helpers"
helpers "github.com/truvami/decoder/pkg/common"
"github.com/truvami/decoder/pkg/decoder/tagsl/v1"
"go.uber.org/zap"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/tagxl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/truvami/decoder/internal/logger"
"github.com/truvami/decoder/pkg/decoder/helpers"
helpers "github.com/truvami/decoder/pkg/common"
"github.com/truvami/decoder/pkg/decoder/tagxl/v1"
"github.com/truvami/decoder/pkg/loracloud"
"go.uber.org/zap"
Expand Down
67 changes: 67 additions & 0 deletions pkg/decoder/helpers/helpers.go → pkg/common/helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package helpers

Check failure on line 1 in pkg/common/helpers.go

View workflow job for this annotation

GitHub Actions / Test 🧪

File test coverage below threshold

File test coverage below threshold: coverage: 71.0% (93/131); threshold: 75%

import (
"encoding/hex"
h "encoding/hex"
"errors"
"fmt"
Expand All @@ -11,6 +12,7 @@ import (

"github.com/go-playground/validator"
"github.com/truvami/decoder/pkg/decoder"
"github.com/truvami/decoder/pkg/encoder"
)

func HexStringToBytes(hexString string) ([]byte, error) {
Expand Down Expand Up @@ -230,3 +232,68 @@ func ValidateLength(payload *string, config *decoder.PayloadConfig) error {

return nil
}

func Encode(data interface{}, config encoder.PayloadConfig) (string, error) {
v := reflect.ValueOf(data)

// Validate input data is a struct
if v.Kind() != reflect.Struct {
return "", fmt.Errorf("data must be a struct")
}

Check warning on line 242 in pkg/common/helpers.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/helpers.go#L236-L242

Added lines #L236 - L242 were not covered by tests

// Determine total payload length
var length int
for _, field := range config.Fields {
if field.Start+field.Length > length {
length = field.Start + field.Length
}

Check warning on line 249 in pkg/common/helpers.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/helpers.go#L245-L249

Added lines #L245 - L249 were not covered by tests
}
payload := make([]byte, length)

// Encode fields into the payload
for _, field := range config.Fields {
fieldValue := v.FieldByName(field.Name)

// Check if the field exists
if !fieldValue.IsValid() {
return "", fmt.Errorf("field %s not found in data", field.Name)
}

Check warning on line 260 in pkg/common/helpers.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/helpers.go#L251-L260

Added lines #L251 - L260 were not covered by tests

// Convert the value to bytes
var fieldBytes []byte
switch fieldValue.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
fieldBytes = uintToBytes(fieldValue.Uint(), field.Length)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fieldBytes = intToBytes(fieldValue.Int(), field.Length)
default:
return "", fmt.Errorf("unsupported field type: %s", fieldValue.Kind())

Check warning on line 270 in pkg/common/helpers.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/helpers.go#L263-L270

Added lines #L263 - L270 were not covered by tests
}

// Copy the bytes into the payload at the correct position
copy(payload[field.Start:field.Start+field.Length], fieldBytes)

Check warning on line 274 in pkg/common/helpers.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/helpers.go#L274

Added line #L274 was not covered by tests
}

// Convert the payload to a hexadecimal string
return hex.EncodeToString(payload), nil

Check warning on line 278 in pkg/common/helpers.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/helpers.go#L278

Added line #L278 was not covered by tests
}

// intToBytes converts an integer value to a byte slice
func intToBytes(value int64, length int) []byte {
buf := make([]byte, length)
for i := length - 1; i >= 0; i-- {
buf[i] = byte(value & 0xFF)
value >>= 8
}
return buf

Check warning on line 288 in pkg/common/helpers.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/helpers.go#L282-L288

Added lines #L282 - L288 were not covered by tests
}

// uintToBytes converts an unsigned integer value to a byte slice
func uintToBytes(value uint64, length int) []byte {
buf := make([]byte, length)
for i := length - 1; i >= 0; i-- {
buf[i] = byte(value & 0xFF)
value >>= 8
}
return buf

Check warning on line 298 in pkg/common/helpers.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/helpers.go#L292-L298

Added lines #L292 - L298 were not covered by tests
}
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/decoder/nomadxl/v1/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"reflect"

helpers "github.com/truvami/decoder/pkg/common"
"github.com/truvami/decoder/pkg/decoder"
"github.com/truvami/decoder/pkg/decoder/helpers"
)

type Option func(*NomadXLv1Decoder)
Expand Down
2 changes: 1 addition & 1 deletion pkg/decoder/nomadxs/v1/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"reflect"

helpers "github.com/truvami/decoder/pkg/common"
"github.com/truvami/decoder/pkg/decoder"
"github.com/truvami/decoder/pkg/decoder/helpers"
)

type Option func(*NomadXSv1Decoder)
Expand Down
2 changes: 1 addition & 1 deletion pkg/decoder/nomadxs/v1/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/truvami/decoder/pkg/decoder/helpers"
helpers "github.com/truvami/decoder/pkg/common"
)

func TestDecode(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/decoder/smartlabel/v1/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"reflect"
"strings"

helpers "github.com/truvami/decoder/pkg/common"
"github.com/truvami/decoder/pkg/decoder"
"github.com/truvami/decoder/pkg/decoder/helpers"
"github.com/truvami/decoder/pkg/loracloud"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/decoder/tagsl/v1/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"reflect"

helpers "github.com/truvami/decoder/pkg/common"
"github.com/truvami/decoder/pkg/decoder"
"github.com/truvami/decoder/pkg/decoder/helpers"
)

type Option func(*TagSLv1Decoder)
Expand Down
2 changes: 1 addition & 1 deletion pkg/decoder/tagsl/v1/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
"time"

"github.com/truvami/decoder/pkg/decoder/helpers"
helpers "github.com/truvami/decoder/pkg/common"
)

func TestDecode(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/decoder/tagxl/v1/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"reflect"

helpers "github.com/truvami/decoder/pkg/common"
"github.com/truvami/decoder/pkg/decoder"
"github.com/truvami/decoder/pkg/decoder/helpers"
"github.com/truvami/decoder/pkg/loracloud"
)

Expand Down

0 comments on commit d7b146e

Please sign in to comment.