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

[lint] Add golangci-lint and fix issues as a result of it #141

Closed
wants to merge 5 commits into from
Closed
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
26 changes: 26 additions & 0 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: golangci-lint
on:
push:
branches:
- main
- master
pull_request:

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: stable
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.64
2 changes: 1 addition & 1 deletion account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// It can represent an Object, an Account, and much more.
type AccountAddress = types.AccountAddress

// Account is a wrapper for a signer, handling the AccountAddress and signing
// Account is a wrapper for a signer, handling the AccountAddress and signing.
type Account = types.Account

// AccountZero represents the 0x0 address
Expand Down
5 changes: 2 additions & 3 deletions accountInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ type AccountInfo struct {
// AuthenticationKey Hex decode of AuthenticationKeyHex
func (ai AccountInfo) AuthenticationKey() ([]byte, error) {
ak := ai.AuthenticationKeyHex
if strings.HasPrefix(ak, "0x") {
ak = ak[2:]
}
ak = strings.TrimPrefix(ak, "0x")

return hex.DecodeString(ak)
}

Expand Down
10 changes: 6 additions & 4 deletions api/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
)

//region Block
// region Block

// Block describes a block properties and may have attached transactions
//
Expand All @@ -27,7 +27,7 @@ type Block struct {
Transactions []*CommittedTransaction // Transactions in the block if requested, otherwise it is empty
}

//region Block JSON
// region Block JSON

// UnmarshalJSON deserializes a JSON data blob into a [Block]
//
Expand All @@ -53,15 +53,17 @@ func (o *Block) UnmarshalJSON(b []byte) error {
o.FirstVersion = data.FirstVersion.ToUint64()
o.LastVersion = data.LastVersion.ToUint64()
o.Transactions = make([]*CommittedTransaction, len(data.Transactions))

for i, tx := range data.Transactions {
// TODO: Do I just save transactions as "unknown" if I can't parse them?
err = json.Unmarshal(tx, &o.Transactions[i])
if err != nil {
return err
}
}

return nil
}

//endregion
//endregion
// endregion
// endregion
11 changes: 6 additions & 5 deletions api/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package api

import (
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"

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

func TestBlock(t *testing.T) {
testJson := `{
testJSON := `{
"block_height": "1",
"block_hash": "0x014e30aafd9f715ab6262322bf919abebd66d948f6822ffb8a2699a57722fb80",
"block_timestamp": "1665609760857472",
Expand All @@ -16,7 +17,7 @@ func TestBlock(t *testing.T) {
"transactions": null
}`
data := &Block{}
err := json.Unmarshal([]byte(testJson), &data)
err := json.Unmarshal([]byte(testJSON), &data)
assert.NoError(t, err)

assert.Equal(t, "0x014e30aafd9f715ab6262322bf919abebd66d948f6822ffb8a2699a57722fb80", data.BlockHash)
Expand All @@ -28,7 +29,7 @@ func TestBlock(t *testing.T) {
}

func TestBlockWithNoTransactions(t *testing.T) {
testJson := `{
testJSON := `{
"block_height": "1",
"block_hash": "0x014e30aafd9f715ab6262322bf919abebd66d948f6822ffb8a2699a57722fb80",
"block_timestamp": "1665609760857472",
Expand All @@ -37,7 +38,7 @@ func TestBlockWithNoTransactions(t *testing.T) {
"transactions": []
}`
data := &Block{}
err := json.Unmarshal([]byte(testJson), &data)
err := json.Unmarshal([]byte(testJSON), &data)
assert.NoError(t, err)

assert.Equal(t, "0x014e30aafd9f715ab6262322bf919abebd66d948f6822ffb8a2699a57722fb80", data.BlockHash)
Expand Down
2 changes: 1 addition & 1 deletion api/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package api

// Error is an error from the REST API
// Error is an error from the REST API.
type Error struct {
Message string `json:"message"` // Message is the error message
ErrorCode string `json:"error_code"` // ErrorCode is the string name of the error
Expand Down
3 changes: 2 additions & 1 deletion api/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package api
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"testing"

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

func Test_Error(t *testing.T) {
Expand Down
12 changes: 7 additions & 5 deletions api/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import "encoding/json"

//region Event
// region Event

// Event describes an on-chain event from Move. There are currently two types:
//
Expand Down Expand Up @@ -41,12 +41,12 @@ type Event struct {
Data map[string]any // Data is the event data, a map of field name to value, this should match it's on-chain struct representation
}

// region Event JSON
// region Event JSON.
const (
AnyDataName = "__any_data__"
)

// UnmarshalJSON deserializes a JSON data blob into an Event
// UnmarshalJSON deserializes a JSON data blob into an Event.
func (o *Event) UnmarshalJSON(b []byte) error {
// In order to handle non-map types of data, we will give a new field name for it
type innerStandard struct {
Expand All @@ -63,6 +63,7 @@ func (o *Event) UnmarshalJSON(b []byte) error {
o.Guid = data.Guid
o.SequenceNumber = data.SequenceNumber.ToUint64()
o.Data = data.Data

return nil
}

Expand All @@ -79,6 +80,7 @@ func (o *Event) UnmarshalJSON(b []byte) error {
if err != nil {
return err
}

dataMap := make(map[string]any)
dataMap[AnyDataName] = dataAny.Data

Expand All @@ -90,5 +92,5 @@ func (o *Event) UnmarshalJSON(b []byte) error {
return err
}

//endregion
//endregion
// endregion
// endregion
3 changes: 2 additions & 1 deletion api/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package api

import (
"encoding/json"
"testing"

"github.com/aptos-labs/aptos-go-sdk/internal/types"
"github.com/stretchr/testify/assert"
"testing"
)

func TestEvent_V1(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions api/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package api

import (
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"

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

func Test_HealthCheckResponse(t *testing.T) {
testJson := `{
testJSON := `{
"message": "aptos-node:ok"
}`
data := &HealthCheckResponse{}
err := json.Unmarshal([]byte(testJson), &data)
err := json.Unmarshal([]byte(testJSON), &data)
assert.NoError(t, err)
assert.Equal(t, "aptos-node:ok", data.Message)
}
2 changes: 1 addition & 1 deletion api/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type MoveBytecode struct {
Abi *MoveModule `json:"abi,omitempty"` // Abi is the ABI for the module, and is optional
}

// MoveComponentId is an id for a struct, function, or other type e.g. 0x1::aptos_coin::AptosCoin
// MoveComponentId is an id for a struct, function, or other type e.g. 0x1::aptos_coin::AptosCoin.
type MoveComponentId = string

// MoveModule describes the abilities and types associated with a specific module.
Expand Down
8 changes: 4 additions & 4 deletions api/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import (

// TestModule_MoveBytecode tests the MoveBytecode struct
func TestModule_MoveBytecode(t *testing.T) {
testJson := `{
testJSON := `{
"bytecode": "0xa11ceb0b060000000901000202020403060f0515"
}`
data := &MoveBytecode{}
err := json.Unmarshal([]byte(testJson), &data)
err := json.Unmarshal([]byte(testJSON), &data)
assert.NoError(t, err)
expectedRes, _ := util.ParseHex("0xa11ceb0b060000000901000202020403060f0515")
assert.Equal(t, HexBytes(expectedRes), data.Bytecode)
}

// TestModule_MoveScript tests the MoveScript struct
func TestModule_MoveScript(t *testing.T) {
testJson := `{
testJSON := `{
"bytecode": "0xa11ceb0b060000000901000202020403060f0515"
}`
data := &MoveScript{}
err := json.Unmarshal([]byte(testJson), &data)
err := json.Unmarshal([]byte(testJSON), &data)
assert.NoError(t, err)
expectedRes, _ := util.ParseHex("0xa11ceb0b060000000901000202020403060f0515")
assert.Equal(t, HexBytes(expectedRes), data.Bytecode)
Expand Down
8 changes: 6 additions & 2 deletions api/payloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package api

import (
"encoding/json"

"github.com/aptos-labs/aptos-go-sdk/internal/types"
)

// TransactionPayloadVariant is the type of payload represented in JSON
// TransactionPayloadVariant is the type of payload represented in JSON.
type TransactionPayloadVariant string

const (
Expand All @@ -17,7 +18,7 @@ const (
TransactionPayloadVariantUnknown TransactionPayloadVariant = "unknown" // TransactionPayloadVariantUnknown maps to TransactionPayloadUnknown for unknown types
)

// TransactionPayload is an enum of all possible transaction payloads
// TransactionPayload is an enum of all possible transaction payloads.
//
// Unknown types will have the Type set to [TransactionPayloadVariantUnknown] and the Inner set to [TransactionPayloadUnknown]
type TransactionPayload struct {
Expand All @@ -35,7 +36,9 @@ func (o *TransactionPayload) UnmarshalJSON(b []byte) error {
if err != nil {
return err
}

o.Type = TransactionPayloadVariant(data.Type)

switch o.Type {
case TransactionPayloadVariantEntryFunction:
o.Inner = &TransactionPayloadEntryFunction{}
Expand All @@ -53,6 +56,7 @@ func (o *TransactionPayload) UnmarshalJSON(b []byte) error {
o.Type = TransactionPayloadVariantUnknown
return json.Unmarshal(b, &o.Inner.(*TransactionPayloadUnknown).Payload)
}

return json.Unmarshal(b, o.Inner)
}

Expand Down
11 changes: 6 additions & 5 deletions api/payloads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package api

import (
"encoding/json"
"testing"

"github.com/aptos-labs/aptos-go-sdk/internal/types"
"github.com/stretchr/testify/assert"
"testing"
)

func TestPayload_EntryFunction(t *testing.T) {
Expand All @@ -24,7 +25,7 @@ func TestPayload_EntryFunction(t *testing.T) {
data := &TransactionPayload{}
err := json.Unmarshal([]byte(testJson), &data)
assert.NoError(t, err)
assert.Equal(t, data.Type, TransactionPayloadVariantEntryFunction)
assert.Equal(t, TransactionPayloadVariantEntryFunction, data.Type)
payload := data.Inner.(*TransactionPayloadEntryFunction)

assert.Equal(t, "0x1::object::transfer", payload.Function)
Expand Down Expand Up @@ -61,12 +62,12 @@ func TestPayload_Script(t *testing.T) {
data := &TransactionPayload{}
err := json.Unmarshal([]byte(testJson), &data)
assert.NoError(t, err)
assert.Equal(t, data.Type, TransactionPayloadVariantScript)
assert.Equal(t, TransactionPayloadVariantScript, data.Type)
payload := data.Inner.(*TransactionPayloadScript)

assert.Len(t, payload.Code.Bytecode, 263)
assert.Equal(t, "main", payload.Code.Abi.Name)
assert.Len(t, payload.TypeArguments, 0)
assert.Empty(t, payload.TypeArguments)
assert.Len(t, payload.Arguments, 2)
}

Expand All @@ -91,7 +92,7 @@ func TestPayload_Multisig(t *testing.T) {
data := &TransactionPayload{}
err := json.Unmarshal([]byte(testJson), &data)
assert.NoError(t, err)
assert.Equal(t, data.Type, TransactionPayloadVariantMultisig)
assert.Equal(t, TransactionPayloadVariantMultisig, data.Type)
payload := data.Inner.(*TransactionPayloadMultisig)
assert.Equal(t, types.AccountOne, *payload.MultisigAddress)
}
Expand Down
4 changes: 4 additions & 0 deletions api/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"encoding/json"

"github.com/aptos-labs/aptos-go-sdk/crypto"
"github.com/aptos-labs/aptos-go-sdk/internal/types"
)
Expand Down Expand Up @@ -36,7 +37,9 @@ func (o *Signature) UnmarshalJSON(b []byte) error {
if err != nil {
return err
}

o.Type = SignatureVariant(data.Type)

switch o.Type {
case SignatureVariantEd25519:
o.Inner = &Ed25519Signature{}
Expand Down Expand Up @@ -145,6 +148,7 @@ func (o *MultiEd25519Signature) UnmarshalJSON(b []byte) error {
return err
}
}

o.Signatures = make([]*crypto.Ed25519Signature, len(data.Signatures))
for i, signature := range data.Signatures {
o.Signatures[i] = &crypto.Ed25519Signature{}
Expand Down
4 changes: 3 additions & 1 deletion api/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package api

import (
"encoding/json"
"testing"

"github.com/aptos-labs/aptos-go-sdk/crypto"
"github.com/stretchr/testify/assert"
"testing"
)

func TestAccountAuthenticator_Unknown(t *testing.T) {
Expand All @@ -19,6 +20,7 @@ func TestAccountAuthenticator_Unknown(t *testing.T) {

assert.Equal(t, "something", auth.Type)
}

func TestAccountAuthenticator_Ed25519(t *testing.T) {
testJson := `{
"public_key": "0xfc0947a61275f90ed089e1584143362eb236b11d72f901b8c2a5ca546f7fa34f",
Expand Down
Loading
Loading