Skip to content

Commit

Permalink
Fix build, dependencies and CI
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Jun 24, 2024
1 parent d2ef3a4 commit 7bb531d
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 36 deletions.
17 changes: 8 additions & 9 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: '^1.21'

- name: Get dependencies
run: go mod download
cache: false

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v4
with:
version: v1.52.2
version: v1.57.1
only-new-issues: true
args: -c ./.golangci.yml --timeout 15m

Expand All @@ -41,12 +39,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: '^1.21'
cache: false

- name: Get dependencies
run: go mod download
Expand Down
2 changes: 1 addition & 1 deletion container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"io"

"github.com/safing/portbase/formats/varint"
"github.com/safing/structures/varint"
)

// Container is []byte sclie on steroids, allowing for quick data appending, prepending and fetching.
Expand Down
19 changes: 12 additions & 7 deletions container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package container
import (
"bytes"
"testing"

"github.com/safing/portbase/utils"
)

var (
Expand All @@ -25,7 +23,7 @@ var (
func TestContainerDataHandling(t *testing.T) {
t.Parallel()

c1 := New(utils.DuplicateBytes(testData))
c1 := New(duplicateBytes(testData))
c1c := c1.carbonCopy()

c2 := New()
Expand Down Expand Up @@ -87,7 +85,7 @@ func compareMany(t *testing.T, reference []byte, other ...[]byte) {
func TestDataFetching(t *testing.T) {
t.Parallel()

c1 := New(utils.DuplicateBytes(testData))
c1 := New(duplicateBytes(testData))
data := c1.GetMax(1)
if string(data[0]) != "T" {
t.Errorf("failed to GetMax(1), got %s, expected %s", string(data), "T")
Expand All @@ -107,7 +105,7 @@ func TestDataFetching(t *testing.T) {
func TestBlocks(t *testing.T) {
t.Parallel()

c1 := New(utils.DuplicateBytes(testData))
c1 := New(duplicateBytes(testData))
c1.PrependLength()

n, err := c1.GetNextN8()
Expand Down Expand Up @@ -149,7 +147,7 @@ func TestBlocks(t *testing.T) {
func TestContainerBlockHandling(t *testing.T) {
t.Parallel()

c1 := New(utils.DuplicateBytes(testData))
c1 := New(duplicateBytes(testData))
c1.PrependLength()
c1.AppendAsBlock(testData)
c1c := c1.carbonCopy()
Expand Down Expand Up @@ -204,5 +202,12 @@ func TestContainerMisc(t *testing.T) {
func TestDeprecated(t *testing.T) {
t.Parallel()

NewContainer(utils.DuplicateBytes(testData))
NewContainer(duplicateBytes(testData))
}

// duplicateBytes returns a new copy of the given byte slice.
func duplicateBytes(a []byte) []byte {
b := make([]byte, len(a))
copy(b, a)
return b
}
2 changes: 1 addition & 1 deletion dsd/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"compress/gzip"
"errors"

"github.com/safing/portbase/formats/varint"
"github.com/safing/structures/varint"
)

// DumpAndCompress stores the interface as a dsd formatted data structure and compresses the resulting data.
Expand Down
13 changes: 6 additions & 7 deletions dsd/dsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
"github.com/ghodss/yaml"
"github.com/vmihailenco/msgpack/v5"

"github.com/safing/portbase/formats/varint"
"github.com/safing/portbase/utils"
"github.com/safing/structures/varint"
)

// Load loads an dsd structured data blob into the given interface.
Expand All @@ -39,25 +38,25 @@ func LoadAsFormat(data []byte, format uint8, t interface{}) (err error) {
case JSON:
err = json.Unmarshal(data, t)
if err != nil {
return fmt.Errorf("dsd: failed to unpack json: %w, data: %s", err, utils.SafeFirst16Bytes(data))
return fmt.Errorf("dsd: failed to unpack json: %w, data: %s", err, safeFirst16Bytes(data))
}
return nil
case YAML:
err = yaml.Unmarshal(data, t)
if err != nil {
return fmt.Errorf("dsd: failed to unpack yaml: %w, data: %s", err, utils.SafeFirst16Bytes(data))
return fmt.Errorf("dsd: failed to unpack yaml: %w, data: %s", err, safeFirst16Bytes(data))
}
return nil
case CBOR:
err = cbor.Unmarshal(data, t)
if err != nil {
return fmt.Errorf("dsd: failed to unpack cbor: %w, data: %s", err, utils.SafeFirst16Bytes(data))
return fmt.Errorf("dsd: failed to unpack cbor: %w, data: %s", err, safeFirst16Bytes(data))
}
return nil
case MsgPack:
err = msgpack.Unmarshal(data, t)
if err != nil {
return fmt.Errorf("dsd: failed to unpack msgpack: %w, data: %s", err, utils.SafeFirst16Bytes(data))
return fmt.Errorf("dsd: failed to unpack msgpack: %w, data: %s", err, safeFirst16Bytes(data))
}
return nil
case GenCode:
Expand All @@ -67,7 +66,7 @@ func LoadAsFormat(data []byte, format uint8, t interface{}) (err error) {
}
_, err = genCodeStruct.GenCodeUnmarshal(data)
if err != nil {
return fmt.Errorf("dsd: failed to unpack gencode: %w, data: %s", err, utils.SafeFirst16Bytes(data))
return fmt.Errorf("dsd: failed to unpack gencode: %w, data: %s", err, safeFirst16Bytes(data))
}
return nil
default:
Expand Down
5 changes: 3 additions & 2 deletions dsd/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMimeTypes(t *testing.T) {
Expand All @@ -13,12 +14,12 @@ func TestMimeTypes(t *testing.T) {
// Test static maps.
for _, mimeType := range FormatToMimeType {
cleaned, _, err := mime.ParseMediaType(mimeType)
assert.NoError(t, err, "mime type must be parse-able")
require.NoError(t, err, "mime type must be parse-able")
assert.Equal(t, mimeType, cleaned, "mime type should be clean in map already")
}
for mimeType := range MimeTypeToFormat {
cleaned, _, err := mime.ParseMediaType(mimeType)
assert.NoError(t, err, "mime type must be parse-able")
require.NoError(t, err, "mime type must be parse-able")
assert.Equal(t, mimeType, cleaned, "mime type should be clean in map already")
}

Expand Down
23 changes: 23 additions & 0 deletions dsd/safe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dsd

import (
"encoding/hex"
"strings"
)

// safeFirst16Bytes return the first 16 bytes of the given data in safe form.
func safeFirst16Bytes(data []byte) string {
if len(data) == 0 {
return "<empty>"
}

return strings.TrimPrefix(
strings.SplitN(hex.Dump(data), "\n", 2)[0],
"00000000 ",
)
}

// safeFirst16Chars return the first 16 characters of the given data in safe form.
func safeFirst16Chars(s string) string {
return safeFirst16Bytes([]byte(s))
}
29 changes: 29 additions & 0 deletions dsd/safe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dsd

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSafeFirst16(t *testing.T) {
t.Parallel()

assert.Equal(t,
"47 6f 20 69 73 20 61 6e 20 6f 70 65 6e 20 73 6f |Go is an open so|",
safeFirst16Bytes([]byte("Go is an open source programming language.")),
)
assert.Equal(t,
"47 6f 20 69 73 20 61 6e 20 6f 70 65 6e 20 73 6f |Go is an open so|",
safeFirst16Chars("Go is an open source programming language."),
)

assert.Equal(t,
"<empty>",
safeFirst16Bytes(nil),
)
assert.Equal(t,
"<empty>",
safeFirst16Chars(""),
)
}
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ toolchain go1.21.2
require (
github.com/fxamacker/cbor/v2 v2.7.0
github.com/ghodss/yaml v1.0.0
github.com/safing/portbase v0.19.5
github.com/stretchr/testify v1.8.4
github.com/vmihailenco/msgpack/v5 v5.4.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tevino/abool v1.2.0 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,15 @@ github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv
github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/safing/portbase v0.19.5 h1:3/8odzlvb629tHPwdj/sthSeJcwZHYrqA6YuvNUZzNc=
github.com/safing/portbase v0.19.5/go.mod h1:Qrh3ck+7VZloFmnozCs9Hj8godhJAi55cmiDiC7BwTc=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tevino/abool v1.2.0 h1:heAkClL8H6w+mK5md9dzsuohKeXHUpY7Vw0ZCKW+huA=
github.com/tevino/abool v1.2.0/go.mod h1:qc66Pna1RiIsPa7O4Egxxs9OqkuxDX55zznh9K07Tzg=
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
Expand Down
Loading

0 comments on commit 7bb531d

Please sign in to comment.