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

update workflows, go version and expose map sorting #35

Merged
merged 4 commits into from
Dec 12, 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
36 changes: 36 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Continuous Integration
on:
push:
branches:
- main
- develop

pull_request:
branches:
- main
- develop
types:
- opened
- synchronize
- ready_for_review
paths:
- "**.go"
- "**.mod"
- "**.sum"
- ".github/workflows/**.yaml"

workflow_call: {}
workflow_dispatch: {}

permissions:
contents: read

jobs:
lint:
name: Lint
uses: ./.github/workflows/lint.yaml

test:
name: Test
needs: lint
uses: ./.github/workflows/test.yaml
30 changes: 30 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Lint
on:
workflow_dispatch: {}
workflow_call: {}

permissions:
contents: read

jobs:
golangci:
name: GolangCI Lint
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v3

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

- name: Run Golang CI Lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.52.2
skip-cache: true
skip-pkg-cache: true
skip-build-cache: true
25 changes: 25 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Test
on:
workflow_dispatch: {}
workflow_call: {}

permissions:
contents: read

jobs:
test:
name: Unit Tests
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v3

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

- name: Run Golang Tests
run: go test ./... -v -race
32 changes: 0 additions & 32 deletions .github/workflows/testing.yml

This file was deleted.

67 changes: 67 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
run:
tests: true
# timeout for analysis, e.g. 30s, 5m, default is 1m
timeout: 5m
# exit code when at least one issue was found, default is 1
issues-exit-code: 1

linters:
disable-all: true
enable:
- dogsled
- dupl
- depguard
- errname
- errorlint
- forcetypeassert
- exportloopref
- goconst
- gocritic
- gofumpt
- gosec
- gosimple
- govet
- importas
- ineffassign
- lll
- makezero
- misspell
- nakedret
- nolintlint
- predeclared
- prealloc
- staticcheck
- stylecheck
- tparallel
- thelper
- typecheck
- unconvert
- unused
- whitespace
- wsl

issues:
exclude-rules:
- text: "Use of weak random number generator"
linters:
- gosec

max-issues-per-linter: 10000
max-same-issues: 10000

linters-settings:
dogsled:
max-blank-identifiers: 3
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
nolintlint:
allow-unused: false # to be set to false once all issues are addressed, any exceptions needed to be added above
allow-leading-space: true
require-explanation: false # nice to have as true
require-specific: false
depguard:
Main:
allow:
- $github.com/sarvalabs/*
- github.com/pkg/errors
1 change: 0 additions & 1 deletion bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func BenchmarkEncoding(b *testing.B) {
})
})
})

}

func BenchmarkDocument(b *testing.B) {
Expand Down
18 changes: 9 additions & 9 deletions bufferread.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (rb readbuffer) decodeBool() (bool, error) {
return false, nil
// Default Value
case WireNull:
return false, nilValue
return false, errNilValue
default:
return false, IncompatibleWireType(rb.wire, WireNull, WireTrue, WireFalse)
}
Expand All @@ -116,7 +116,7 @@ func (rb readbuffer) decodeBytes() ([]byte, error) {
return rb.data, nil
// Nil Byte Slice (Default)
case WireNull:
return nil, nilValue
return nil, errNilValue
default:
return nil, IncompatibleWireType(rb.wire, WireNull, WireWord)
}
Expand Down Expand Up @@ -155,7 +155,7 @@ func (rb readbuffer) decodeBytesFromPack() ([]byte, error) {

// Nil Byte Slice (Default)
case WireNull:
return nil, nilValue
return nil, errNilValue
default:
return nil, IncompatibleWireType(rb.wire, WireNull, WirePack)
}
Expand All @@ -168,7 +168,7 @@ func (rb readbuffer) decodeString() (string, error) {
return string(rb.data), nil
// Empty String (Default)
case WireNull:
return "", nilValue
return "", errNilValue
default:
return "", IncompatibleWireType(rb.wire, WireNull, WireWord)
}
Expand All @@ -188,7 +188,7 @@ func (rb readbuffer) decodeUint64() (uint64, error) {
return number, nil

case WireNull:
return 0, nilValue
return 0, errNilValue
default:
return 0, IncompatibleWireType(rb.wire, WireNull, WirePosInt)
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func (rb readbuffer) decodeInt64() (decoded int64, err error) {
return int64(number), nil

case WireNull:
return 0, nilValue
return 0, errNilValue
default:
return 0, IncompatibleWireType(rb.wire, WireNull, WirePosInt, WireNegInt)
}
Expand Down Expand Up @@ -327,7 +327,7 @@ func (rb readbuffer) decodeFloat32() (float32, error) {

// 0 (Default)
case WireNull:
return 0, nilValue
return 0, errNilValue
default:
return 0, IncompatibleWireType(rb.wire, WireNull, WireFloat)
}
Expand All @@ -350,7 +350,7 @@ func (rb readbuffer) decodeFloat64() (float64, error) {

// 0 (Default)
case WireNull:
return 0, nilValue
return 0, errNilValue
default:
return 0, IncompatibleWireType(rb.wire, WireNull, WireFloat)
}
Expand All @@ -365,7 +365,7 @@ func (rb readbuffer) decodeBigInt() (*big.Int, error) {

// Nil big.Int
case WireNull:
return nil, nilValue
return nil, errNilValue
default:
return nil, IncompatibleWireType(rb.wire, WireNull, WirePosInt, WireNegInt)
}
Expand Down
22 changes: 13 additions & 9 deletions depolorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (depolorizer *Depolorizer) Depolorize(object any) error {

// Handle Errors or Nils
switch {
case err != nil && errors.Is(err, nilValue):
case err != nil && errors.Is(err, errNilValue):
return nil
case err != nil:
return err
Expand All @@ -98,6 +98,7 @@ func (depolorizer *Depolorizer) Depolorize(object any) error {

// Convert and set the decoded value
value.Elem().Set(result.Convert(target))

return nil
}

Expand All @@ -119,7 +120,7 @@ func (depolorizer *Depolorizer) DepolorizeNull() error {
}

func allowNilValue[V any](value V, err error) (V, error) {
if errors.Is(err, nilValue) {
if errors.Is(err, errNilValue) {
return value, nil
}

Expand Down Expand Up @@ -520,7 +521,7 @@ func (depolorizer *Depolorizer) depolorizeMapValue(target reflect.Type) (reflect

// Depolorize the raw value for the key into map's value type
val, err := decoder.depolorizeValue(valType)
if err != nil && !errors.Is(err, nilValue) {
if err != nil && !errors.Is(err, errNilValue) {
return zeroVal, err
}

Expand Down Expand Up @@ -575,7 +576,9 @@ func (depolorizer *Depolorizer) depolorizeStructValue(target reflect.Type) (refl
// Depolorize the next object from the pack into the map key type
val, err := pack.depolorizeValue(field.Type)
if err != nil {
return zeroVal, IncompatibleWireError{fmt.Sprintf("struct field [%v.%v <%v>]: %v", target, field.Name, field.Type, err)}
return zeroVal, IncompatibleWireError{
fmt.Sprintf("struct field [%v.%v <%v>]: %v", target, field.Name, field.Type, err),
}
}

if val != zeroVal {
Expand Down Expand Up @@ -630,8 +633,10 @@ func (depolorizer *Depolorizer) depolorizeStructValue(target reflect.Type) (refl
}

fieldVal, err := object.depolorizeValue(field.Type)
if err != nil && !errors.Is(err, nilValue) {
return zeroVal, IncompatibleWireError{fmt.Sprintf("struct field [%v.%v <%v>]: %v", target, field.Name, field.Type, err)}
if err != nil && !errors.Is(err, errNilValue) {
return zeroVal, IncompatibleWireError{
fmt.Sprintf("struct field [%v.%v <%v>]: %v", target, field.Name, field.Type, err),
}
}

if fieldVal != zeroVal {
Expand All @@ -656,7 +661,7 @@ func (depolorizer *Depolorizer) depolorizePointer(target reflect.Type) (reflect.
value, err := depolorizer.depolorizeValue(target.Elem())

switch {
case err != nil && errors.Is(err, nilValue):
case err != nil && errors.Is(err, errNilValue):
return zeroVal, nil
case err != nil:
return zeroVal, err
Expand Down Expand Up @@ -686,7 +691,7 @@ func (depolorizer *Depolorizer) depolorizeDepolorizable(target reflect.Type) (re
// Call the Depolorize method of Depolorizable (accepts a Depolorizer and returns an error)
outputs := value.MethodByName("Depolorize").Call([]reflect.Value{reflect.ValueOf(inner)})
if !outputs[0].IsNil() {
return zeroVal, outputs[0].Interface().(error)
return zeroVal, outputs[0].Interface().(error) //nolint:forcetypeassert
}

return value.Elem(), nil
Expand All @@ -701,7 +706,6 @@ func (depolorizer *Depolorizer) depolorizeValue(target reflect.Type) (reflect.Va
}

switch kind := target.Kind(); kind {

// Pointer Value
case reflect.Ptr:
return depolorizer.depolorizePointer(target)
Expand Down
Loading