Skip to content

Commit

Permalink
fix(SPV-1069) fix lint errors for "models" package (#800)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-4chain authored Dec 10, 2024
1 parent a71b048 commit 74c0717
Show file tree
Hide file tree
Showing 20 changed files with 43 additions and 19 deletions.
1 change: 1 addition & 0 deletions models/bsv/satoshis.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package bsv

// Satoshis specifies a type that should be used to store value in satoshis
type Satoshis uint64
3 changes: 3 additions & 0 deletions models/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"github.com/bitcoin-sv/spv-wallet/models/response"
)

// CreateContactResponse is a model for response on contact creation.
type CreateContactResponse struct {
Contact *Contact `json:"contact"`
AdditionalInfo map[string]string `json:"additionalInfo"`
}

// Contact is a model for contact.
type Contact struct {
common.Model

Expand All @@ -25,6 +27,7 @@ type Contact struct {
Status response.ContactStatus `json:"status" example:"unconfirmed"`
}

// AddAdditionalInfo adds additional information (as key-value map) to the response.
func (m *CreateContactResponse) AddAdditionalInfo(k, v string) {
if m.AdditionalInfo == nil {
m.AdditionalInfo = make(map[string]string)
Expand Down
2 changes: 1 addition & 1 deletion models/filter/access_key_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package filter
// AccessKeyFilter is a struct for handling request parameters for destination search requests
type AccessKeyFilter struct {
// ModelFilter is a struct for handling typical request parameters for search requests
//lint:ignore SA5008 We want to reuse json tags also to mapstructure.
//nolint:staticcheck // SA5008 We want to reuse json tags also to mapstructure.
ModelFilter `json:",inline,squash"`

// RevokedRange specifies the time range when a record was revoked.
Expand Down
2 changes: 1 addition & 1 deletion models/filter/contact_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package filter
// ContactFilter is a struct for handling request parameters for contact search requests
type ContactFilter struct {
// ModelFilter is a struct for handling typical request parameters for search requests
//lint:ignore SA5008 We want to reuse json tags also to mapstructure.
//nolint:staticcheck // SA5008 We want to reuse json tags also to mapstructure.
ModelFilter `json:",inline,squash"`
ID *string `json:"id" example:"ffdbe74e-0700-4710-aac5-611a1f877c7f"`
FullName *string `json:"fullName" example:"Alice"`
Expand Down
2 changes: 1 addition & 1 deletion models/filter/destination_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package filter
// DestinationFilter is a struct for handling request parameters for destination search requests
type DestinationFilter struct {
// ModelFilter is a struct for handling typical request parameters for search requests
//lint:ignore SA5008 We want to reuse json tags also to mapstructure.
//nolint:staticcheck // SA5008 We want to reuse json tags also to mapstructure.
ModelFilter `json:",inline,squash"`
LockingScript *string `json:"lockingScript,omitempty" example:"76a9147b05764a97f3b4b981471492aa703b188e45979b88ac"`
Address *string `json:"address,omitempty" example:"1CDUf7CKu8ocTTkhcYUbq75t14Ft168K65"`
Expand Down
2 changes: 1 addition & 1 deletion models/filter/generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type SearchModel[TFilter any] struct {
QueryParams *QueryParams `json:"params,omitempty" swaggertype:"object,string" example:"page:1,page_size:10,order_by_field:created_at,order_by_direction:desc"`
}

// DefaultsIfNil fills empty but neccessary fields with default values
// DefaultsIfNil fills empty but necessary fields with default values
func (sm *SearchModel[TFilter]) DefaultsIfNil() {
if sm.QueryParams == nil {
sm.QueryParams = DefaultQueryParams()
Expand Down
6 changes: 4 additions & 2 deletions models/filter/paymail_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package filter
// PaymailFilter is a struct for handling request parameters for paymail_addresses search requests
type PaymailFilter struct {
// ModelFilter is a struct for handling typical request parameters for search requests
//lint:ignore SA5008 We want to reuse json tags also to mapstructure.
//nolint:staticcheck // SA5008 We want to reuse json tags also to mapstructure.
ModelFilter `json:",inline,squash"`

ID *string `json:"id,omitempty" example:"ffb86c103d17d87c15aaf080aab6be5415c9fa885309a79b04c9910e39f2b542"`
Expand All @@ -28,13 +28,15 @@ func (d *PaymailFilter) ToDbConditions() map[string]interface{} {
return conditions
}

// AdminPaymailFilter wraps the PaymailFilter providing additional fields for admin paymail search requests
type AdminPaymailFilter struct {
//lint:ignore SA5008 We want to reuse json tags also to mapstructure.
//nolint:staticcheck // SA5008 We want to reuse json tags also to mapstructure.
PaymailFilter `json:",inline,squash"`

XpubID *string `json:"xpubId,omitempty" example:"79f90a6bab0a44402fc64828af820e9465645658aea2d138c5205b88e6dabd00"`
}

// ToDbConditions converts filter fields to the datastore conditions using gorm naming strategy
func (d *AdminPaymailFilter) ToDbConditions() map[string]interface{} {
if d == nil {
return nil
Expand Down
6 changes: 5 additions & 1 deletion models/filter/search_params.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package filter

// Page is a struct for handling paging parameters for search requests
type Page struct {
Number int `json:"page,omitempty"`
Size int `json:"size,omitempty"`
Sort string `json:"sort,omitempty"`
SortBy string `json:"sortBy,omitempty"`
}

// SearchParams is a generic struct for handling request parameters for search requests
type SearchParams[T any] struct {
Page Page `json:"paging,squash"`
//nolint:staticcheck // SA5008 We want to reuse json tags also to mapstructure.
Page Page `json:"paging,squash"`
//nolint:staticcheck // SA5008 We want to reuse json tags also to mapstructure.
Conditions T `json:"conditions,squash"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
2 changes: 1 addition & 1 deletion models/filter/transaction_admin_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package filter

// AdminTransactionFilter extends TransactionFilter for admin-specific use, including xpubid filtering
type AdminTransactionFilter struct {
//lint:ignore SA5008 We want to reuse json tags also to mapstructure.
//nolint:staticcheck // SA5008 We want to reuse json tags also to mapstructure.
TransactionFilter `json:",inline,squash"`
XPubID *string `json:"xpubid,omitempty" example:"623bc25ce1c0fc510dea72b5ee27b2e70384c099f1f3dce9e73dd987198c3486"`
}
Expand Down
2 changes: 1 addition & 1 deletion models/filter/transaction_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package filter
// TransactionFilter is a struct for handling request parameters for transactions search requests
type TransactionFilter struct {
// ModelFilter is a struct for handling typical request parameters for search requests
//lint:ignore SA5008 We want to reuse json tags also to mapstructure.
//nolint:staticcheck // SA5008 We want to reuse json tags also to mapstructure.
ModelFilter `json:",inline,squash"`
Id *string `json:"id,omitempty" example:"d425432e0d10a46af1ec6d00f380e9581ebf7907f3486572b3cd561a4c326e14"`
Hex *string `json:"hex,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion models/filter/utxo_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package filter
type UtxoFilter struct {

// ModelFilter is a struct for handling typical request parameters for search requests
//lint:ignore SA5008 We want to reuse json tags also to mapstructure.
//nolint:staticcheck // SA5008 We want to reuse json tags also to mapstructure.
ModelFilter `json:",inline,squash"`

TransactionID *string `json:"transactionId,omitempty" example:"5e17858ea0ca4155827754ba82bdcfcce108d5bb5b47fbb3aa54bd14540683c6"`
Expand Down
2 changes: 1 addition & 1 deletion models/filter/xpub_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package filter
// XpubFilter is a struct for handling request parameters for utxo search requests
type XpubFilter struct {
// ModelFilter is a struct for handling typical request parameters for search requests
//lint:ignore SA5008 We want to reuse json tags also to mapstructure.
//nolint:staticcheck // SA5008 We want to reuse json tags also to mapstructure.
ModelFilter `json:",inline,squash"`

ID *string `json:"id,omitempty" example:"00b953624f78004a4c727cd28557475d5233c15f17aef545106639f4d71b712d"`
Expand Down
2 changes: 2 additions & 0 deletions models/optional/optional.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package optional

// Param is a pointer to a value of type T.
type Param[T any] *T

// Of returns a pointer to a value of type T.
func Of[T any](v T) Param[T] {
return &v
}
6 changes: 4 additions & 2 deletions models/request/opreturn/data_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"errors"
)

// DataType represents the type of data in the OP_RETURN output.
type DataType int

// Enum values for DataType
const (
DataTypeDefault DataType = iota
DataTypeStrings
Expand All @@ -17,7 +19,7 @@ const (
func (d *DataType) UnmarshalJSON(data []byte) error {
var dataType string
if err := json.Unmarshal(data, &dataType); err != nil {
return err
return err //nolint:wrapcheck // UnmarshalJSON is run internally by json.Unmarshal on "DataType" object, so we don't want to wrap the error
}

switch dataType {
Expand All @@ -44,5 +46,5 @@ func (d DataType) MarshalJSON() ([]byte, error) {
default:
return nil, errors.New("invalid data type")
}
return json.Marshal(dataType)
return json.Marshal(dataType) //nolint:wrapcheck // MarshalJSON is run internally by json.Marshal on "DataType" object, so we don't want to wrap the error
}
2 changes: 2 additions & 0 deletions models/request/opreturn/output.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package opreturn

// Output is a struct that represents the output containing OP_RETURN script
type Output struct {
DataType DataType `json:"dataType,omitempty"`
Data []string `json:"data"`
}

// GetType returns the string typename of the output
func (o Output) GetType() string {
return "op_return"
}
2 changes: 2 additions & 0 deletions models/request/paymail/paymail_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"github.com/bitcoin-sv/spv-wallet/models/optional"
)

// Output represents a paymail output.
type Output struct {
To string `json:"to"`
Satoshis bsv.Satoshis `json:"satoshis"`
From optional.Param[string] `json:"from,omitempty"`
}

// GetType returns a string typename of the output.
func (o Output) GetType() string {
return "paymail"
}
6 changes: 3 additions & 3 deletions models/request/transaction_specification.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (dt *TransactionSpecification) unmarshalPartials(data []byte) (rawOutputs [
}

if err := json.Unmarshal(data, &temp); err != nil {
return nil, err
return nil, err //nolint:wrapcheck // unmarshalPartials is run internally by json.Unmarshal, so we don't want to wrap the error
}

return temp.Outputs, nil
Expand All @@ -58,7 +58,7 @@ func unmarshalOutputs(outputs []json.RawMessage) ([]Output, error) {
Type string `json:"type"`
}
if err := json.Unmarshal(rawOutput, &typeField); err != nil {
return nil, err
return nil, err //nolint:wrapcheck // unmarshalOutputs is run internally by json.Unmarshal, so we don't want to wrap the error
}

output, err := unmarshalOutput(rawOutput, typeField.Type)
Expand Down Expand Up @@ -88,5 +88,5 @@ func (dt *TransactionSpecification) MarshalJSON() ([]byte, error) {
temp.Outputs = append(temp.Outputs, out)
}

return json.Marshal(temp)
return json.Marshal(temp) //nolint:wrapcheck // MarshalJSON is run internally by json.Marshal, so we don't want to wrap the error
}
6 changes: 3 additions & 3 deletions models/request/transaction_specification_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ func unmarshalOutput(rawOutput json.RawMessage, outputType string) (Output, erro
case "op_return":
var out opreturn.Output
if err := json.Unmarshal(rawOutput, &out); err != nil {
return nil, err
return nil, err //nolint:wrapcheck // unmarshalOutput is run internally by json.Unmarshal, so we don't want to wrap the error
}
return out, nil
case "paymail":
var out paymailreq.Output
if err := json.Unmarshal(rawOutput, &out); err != nil {
return nil, err
return nil, err //nolint:wrapcheck // unmarshalOutput is run internally by json.Unmarshal, so we don't want to wrap the error
}
return out, nil
default:
return nil, errors.New("unsupported output type")
}
}

// expandOutputForMarshaling used by TransactionSpecification marshalling to expand Output object before marshalling.
// expandOutputForMarshaling used by TransactionSpecification marshaling to expand Output object before marshaling.
// IMPORTANT: Every time a new output type is added, it must be handled here also.
func expandOutputForMarshaling(output Output) (any, error) {
switch o := output.(type) {
Expand Down
5 changes: 5 additions & 0 deletions models/response/contact.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package response

// CreateContactResponse is a model for response on contact creation.
type CreateContactResponse struct {
Contact *Contact `json:"contact"`
AdditionalInfo map[string]string `json:"additionalInfo"`
}

// Contact is a model for contact.
type Contact struct {
Model

Expand All @@ -20,15 +22,18 @@ type Contact struct {
Status ContactStatus `json:"status" example:"unconfirmed"`
}

// ContactStatus is a type for contact status.
type ContactStatus string

// Enum values for ContactStatus.
const (
ContactNotConfirmed ContactStatus = "unconfirmed"
ContactAwaitAccept ContactStatus = "awaiting"
ContactConfirmed ContactStatus = "confirmed"
ContactRejected ContactStatus = "rejected"
)

// AddAdditionalInfo adds additional information (as key-value map) to the response.
func (m *CreateContactResponse) AddAdditionalInfo(k, v string) {
if m.AdditionalInfo == nil {
m.AdditionalInfo = make(map[string]string)
Expand Down
1 change: 1 addition & 0 deletions models/swagger/access_key_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/bitcoin-sv/spv-wallet/models/filter"
)

// CommonFilteringQueryParams is a model for common filtering query parameters.
type CommonFilteringQueryParams struct {
filter.Page `json:",inline"`

Expand Down

0 comments on commit 74c0717

Please sign in to comment.