Skip to content

Commit

Permalink
chore: improve code lints (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac authored Dec 28, 2024
1 parent 1e0043f commit 50fc4dd
Show file tree
Hide file tree
Showing 57 changed files with 377 additions and 401 deletions.
11 changes: 2 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
linters:
enable-all: true
disable:
- dupl
- err113
- lll
- gocognit
- exportloopref
- funlen
- godot
- gofumpt
- gomoddirectives
- depguard
- gosec
Expand All @@ -20,20 +17,14 @@ linters:
- varnamelen
- exhaustive
- exhaustruct
- gocyclo
- prealloc
- ireturn
- gochecknoglobals
- gocyclo
- godox
- stylecheck
- nilnil
- maintidx
- mnd
- tagliatelle
- goconst
- noctx
- unparam
- recvcheck

linters-settings:
Expand All @@ -45,6 +36,8 @@ linters-settings:
gocritic:
disabled-checks:
- appendAssign
gocyclo:
min-complexity: 40

issues:
exclude-files:
Expand Down
4 changes: 2 additions & 2 deletions connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/hasura/ndc-sdk-go/schema"
)

// HTTPConnector implements the SDK interface of NDC specification
// HTTPConnector implements the SDK interface of NDC specification.
type HTTPConnector struct {
config *configuration.Configuration
metadata internal.MetadataCollection
Expand All @@ -25,7 +25,7 @@ type HTTPConnector struct {
procSendHttpRequest rest.OperationInfo
}

// NewHTTPConnector creates a HTTP connector instance
// NewHTTPConnector creates a HTTP connector instance.
func NewHTTPConnector(opts ...Option) *HTTPConnector {
for _, opt := range opts {
opt(&defaultOptions)
Expand Down
14 changes: 7 additions & 7 deletions connector/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ import (

var tracer = connector.NewTracer("HTTPClient")

// HTTPClient represents a http client wrapper with advanced methods
// HTTPClient represents a http client wrapper with advanced methods.
type HTTPClient struct {
manager *UpstreamManager
requests *RequestBuilderResults
}

// Send creates and executes the request and evaluate response selection
// Send creates and executes the request and evaluate response selection.
func (client *HTTPClient) Send(ctx context.Context, selection schema.NestedField) (any, http.Header, error) {
httpOptions := client.requests.HTTPOptions
var result any
Expand Down Expand Up @@ -72,7 +72,7 @@ func (client *HTTPClient) Send(ctx context.Context, selection schema.NestedField
return result, headers, nil
}

// execute a request to a list of remote servers in sequence
// execute a request to a list of remote servers in sequence.
func (client *HTTPClient) sendSequence(ctx context.Context, requests []*RetryableRequest) (*DistributedResponse[any], http.Header) {
results := NewDistributedResponse[any]()
var firstHeaders http.Header
Expand All @@ -98,7 +98,7 @@ func (client *HTTPClient) sendSequence(ctx context.Context, requests []*Retryabl
return results, firstHeaders
}

// execute a request to a list of remote servers in parallel
// execute a request to a list of remote servers in parallel.
func (client *HTTPClient) sendParallel(ctx context.Context, requests []*RetryableRequest) (*DistributedResponse[any], http.Header) {
var firstHeaders http.Header
httpOptions := client.requests.HTTPOptions
Expand Down Expand Up @@ -154,7 +154,7 @@ func (client *HTTPClient) sendParallel(ctx context.Context, requests []*Retryabl
return r, firstHeaders
}

// execute a request to the remote server with retries
// execute a request to the remote server with retries.
func (client *HTTPClient) sendSingle(ctx context.Context, request *RetryableRequest, mode string) (any, http.Header, *schema.ConnectorError) {
ctx, span := tracer.Start(ctx, "Send Request to Server "+request.ServerID)
defer span.End()
Expand Down Expand Up @@ -236,7 +236,7 @@ func (client *HTTPClient) sendSingle(ctx context.Context, request *RetryableRequ
defer cancel()

contentType := parseContentType(resp.Header.Get(rest.ContentTypeHeader))
if resp.StatusCode >= 400 {
if resp.StatusCode >= http.StatusBadRequest {
details := make(map[string]any)
switch contentType {
case rest.ContentTypeJSON:
Expand All @@ -259,7 +259,7 @@ func (client *HTTPClient) sendSingle(ctx context.Context, request *RetryableRequ
span.SetStatus(codes.Error, "received error from remote server")

statusCode := resp.StatusCode
if statusCode < 500 {
if statusCode < http.StatusInternalServerError {
statusCode = http.StatusUnprocessableEntity
}

Expand Down
2 changes: 1 addition & 1 deletion connector/internal/compression/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (c Compressors) Compress(w io.Writer, encoding string, data []byte) (int, e
return compressor.Compress(w, data)
}

// Decompress reads and decompresses the reader with equivalent the content encoding
// Decompress reads and decompresses the reader with equivalent the content encoding.
func (c Compressors) Decompress(reader io.ReadCloser, encoding string) (io.ReadCloser, error) {
compressor, ok := c.compressors[strings.ToLower(strings.TrimSpace(encoding))]
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions connector/internal/contenttype/data_uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

const (
// EncodingBase64 is base64 encoding for the data url
// EncodingBase64 is base64 encoding for the data url.
EncodingBase64 = "base64"
// EncodingASCII is ascii encoding for the data url
// EncodingASCII is ascii encoding for the data url.
EncodingASCII = "ascii"
)

Expand Down
6 changes: 3 additions & 3 deletions connector/internal/contenttype/multipart_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import (
"github.com/hasura/ndc-sdk-go/utils"
)

// MultipartWriter extends multipart.Writer with helpers
// MultipartWriter extends multipart.Writer with helpers.
type MultipartWriter struct {
*multipart.Writer
}

// NewMultipartWriter creates a MultipartWriter instance
// NewMultipartWriter creates a MultipartWriter instance.
func NewMultipartWriter(w io.Writer) *MultipartWriter {
return &MultipartWriter{multipart.NewWriter(w)}
}

// WriteDataURI write a file from data URI string
// WriteDataURI write a file from data URI string.
func (w *MultipartWriter) WriteDataURI(name string, value any, headers http.Header) error {
b64, err := utils.DecodeString(value)
if err != nil {
Expand Down
30 changes: 15 additions & 15 deletions connector/internal/contenttype/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

type ParameterItems []ParameterItem

// String implements fmt.Stringer interface
// String implements fmt.Stringer interface.
func (ssp ParameterItems) String() string {
var results []string
results := []string{}
sortedPairs := append([]ParameterItem{}, ssp...)
slices.SortFunc(sortedPairs, func(a, b ParameterItem) int {
return strings.Compare(a.keys.String(), b.keys.String())
Expand Down Expand Up @@ -82,10 +82,10 @@ func (ssp ParameterItems) find(keys []Key) (*ParameterItem, int) {
return nil, -1
}

// Keys represent a key slice
// Keys represent a key slice.
type Keys []Key

// String implements fmt.Stringer interface
// String implements fmt.Stringer interface.
func (ks Keys) String() string {
if len(ks) == 0 {
return ""
Expand All @@ -109,38 +109,38 @@ func (ks Keys) String() string {
return sb.String()
}

// Key represents a key string or index
// Key represents a key string or index.
type Key struct {
key string
index *int
}

// NewIndexKey creates an index key
// NewIndexKey creates an index key.
func NewIndexKey(index int) Key {
return Key{index: &index}
}

// NewKey creates a string key
// NewKey creates a string key.
func NewKey(key string) Key {
return Key{key: key}
}

// IsEmpty checks if the key is empty
// IsEmpty checks if the key is empty.
func (k Key) IsEmpty() bool {
return k.key == "" && k.index == nil
}

// Key gets the string key
// Key gets the string key.
func (k Key) Key() string {
return k.key
}

// Index gets the integer key
// Index gets the integer key.
func (k Key) Index() *int {
return k.index
}

// String implements fmt.Stringer interface
// String implements fmt.Stringer interface.
func (k Key) String() string {
if k.index != nil {
return strconv.Itoa(*k.index)
Expand All @@ -149,21 +149,21 @@ func (k Key) String() string {
return k.key
}

// ParameterItem represents the key-value slice pair
// ParameterItem represents the key-value slice pair.
type ParameterItem struct {
keys Keys
values []string
}

// NewParameterItem creates a parameter value pair
// NewParameterItem creates a parameter value pair.
func NewParameterItem(keys Keys, values []string) ParameterItem {
return ParameterItem{
keys: keys,
values: values,
}
}

// String implements fmt.Stringer interface
// String implements fmt.Stringer interface.
func (ssp ParameterItem) String() string {
key := ssp.keys.String()
value := strings.Join(ssp.values, ",")
Expand All @@ -174,7 +174,7 @@ func (ssp ParameterItem) String() string {
return fmt.Sprintf("%s=%s", key, value)
}

// Keys returns keys of the parameter item
// Keys returns keys of the parameter item.
func (ssp ParameterItem) Keys() Keys {
return ssp.keys
}
Expand Down
8 changes: 4 additions & 4 deletions connector/internal/contenttype/url_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (c *URLParameterEncoder) encodeScalarParameterReflectionValues(reflectValue
}

return []ParameterItem{NewParameterItem([]Key{}, []string{value})}, nil
case *schema.TypeRepresentationInteger, *schema.TypeRepresentationInt8, *schema.TypeRepresentationInt16, *schema.TypeRepresentationInt32, *schema.TypeRepresentationInt64, *schema.TypeRepresentationBigInteger: //nolint:all
case *schema.TypeRepresentationInt8, *schema.TypeRepresentationInt16, *schema.TypeRepresentationInt32, *schema.TypeRepresentationInt64, *schema.TypeRepresentationBigInteger:
value, err := utils.DecodeIntReflection[int64](reflectValue)
if err != nil {
return nil, fmt.Errorf("%s: %w", strings.Join(fieldPaths, ""), err)
Expand All @@ -211,7 +211,7 @@ func (c *URLParameterEncoder) encodeScalarParameterReflectionValues(reflectValue
return []ParameterItem{
NewParameterItem([]Key{}, []string{strconv.FormatInt(value, 10)}),
}, nil
case *schema.TypeRepresentationNumber, *schema.TypeRepresentationFloat32, *schema.TypeRepresentationFloat64, *schema.TypeRepresentationBigDecimal: //nolint:all
case *schema.TypeRepresentationFloat32, *schema.TypeRepresentationFloat64, *schema.TypeRepresentationBigDecimal:
value, err := utils.DecodeFloatReflection[float64](reflectValue)
if err != nil {
return nil, fmt.Errorf("%s: %w", strings.Join(fieldPaths, ""), err)
Expand Down Expand Up @@ -402,7 +402,7 @@ func buildParamQueryKey(name string, encObject rest.EncodingObject, keys Keys, v
return strings.Join(resultKeys, "")
}

// EvalQueryParameterURL evaluate the query parameter URL
// EvalQueryParameterURL evaluate the query parameter URL.
func EvalQueryParameterURL(q *url.Values, name string, encObject rest.EncodingObject, keys Keys, values []string) {
if len(values) == 0 {
return
Expand Down Expand Up @@ -455,7 +455,7 @@ func EncodeQueryValues(qValues url.Values, allowReserved bool) string {
return builder.String()
}

// SetHeaderParameters set parameters to request headers
// SetHeaderParameters set parameters to request headers.
func SetHeaderParameters(header *http.Header, param *rest.RequestParameter, queryParams ParameterItems) {
defaultParam := queryParams.FindDefault()
// the param is an array
Expand Down
5 changes: 3 additions & 2 deletions connector/internal/contenttype/xml_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func (c *XMLDecoder) getArrayItemObjectField(field rest.ObjectField, t *schema.A

return fieldItem
}

func (c *XMLDecoder) evalArrayField(block *xmlBlock, fieldName string, field rest.ObjectField, t *schema.ArrayType, fieldPaths []string) (any, error) {
if block.Fields == nil {
return nil, nil
Expand Down Expand Up @@ -335,13 +336,13 @@ func (c *XMLDecoder) decodeSimpleScalarValue(block *xmlBlock, scalarType schema.
}

result = block.Data
case *schema.TypeRepresentationInteger, *schema.TypeRepresentationInt8, *schema.TypeRepresentationInt16, *schema.TypeRepresentationInt32, *schema.TypeRepresentationInt64: //nolint:all
case *schema.TypeRepresentationInt8, *schema.TypeRepresentationInt16, *schema.TypeRepresentationInt32, *schema.TypeRepresentationInt64:
if len(block.Data) == 0 {
break
}

result, err = strconv.ParseInt(block.Data, 10, 64)
case *schema.TypeRepresentationNumber, *schema.TypeRepresentationFloat32, *schema.TypeRepresentationFloat64: //nolint:all
case *schema.TypeRepresentationFloat32, *schema.TypeRepresentationFloat64:
if len(block.Data) == 0 {
break
}
Expand Down
Loading

0 comments on commit 50fc4dd

Please sign in to comment.