diff --git a/internal/cmd/gtrace/main.go b/internal/cmd/gtrace/main.go index 84a16add4..41e96b24a 100644 --- a/internal/cmd/gtrace/main.go +++ b/internal/cmd/gtrace/main.go @@ -20,7 +20,7 @@ import ( "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" ) -//nolint:gocyclo +//nolint:gocyclo,funlen func main() { var ( // Reports whether we were called from go:generate. diff --git a/internal/cmd/gtrace/writer.go b/internal/cmd/gtrace/writer.go index 16e30ad2c..463f544f8 100644 --- a/internal/cmd/gtrace/writer.go +++ b/internal/cmd/gtrace/writer.go @@ -372,6 +372,7 @@ func (w *Writer) composeHook(hook Hook, t1, t2, dst string) { w.line(`}`) } +//nolint:funlen func (w *Writer) composeHookCall(fn *Func, h1, h2 string) { w.newScope(func() { w.capture(h1, h2) diff --git a/internal/credentials/oauth2.go b/internal/credentials/oauth2.go index f49826127..1fc5da7d3 100644 --- a/internal/credentials/oauth2.go +++ b/internal/credentials/oauth2.go @@ -349,62 +349,89 @@ func (provider *oauth2TokenExchange) getRequestParams() (string, error) { } func (provider *oauth2TokenExchange) processTokenExchangeResponse(result *http.Response, now time.Time) error { - var ( - data []byte - err error - ) - if result.Body != nil { - data, err = io.ReadAll(result.Body) - if err != nil { - return xerrors.WithStackTrace(err) - } - } else { - data = make([]byte, 0) + data, err := readResponseBody(result) + if err != nil { + return err } if result.StatusCode != http.StatusOK { - description := result.Status + return provider.handleErrorResponse(result.Status, data) + } - //nolint:tagliatelle - type errorResponse struct { - ErrorName string `json:"error"` - ErrorDescription string `json:"error_description"` - ErrorURI string `json:"error_uri"` - } - var parsedErrorResponse errorResponse - if err := json.Unmarshal(data, &parsedErrorResponse); err != nil { - description += ", could not parse response: " + err.Error() + parsedResponse, err := parseTokenResponse(data) + if err != nil { + return err + } - return xerrors.WithStackTrace(fmt.Errorf("%w: %s", errCouldNotExchangeToken, description)) - } + if err := validateTokenResponse(parsedResponse, provider); err != nil { + return err + } - if parsedErrorResponse.ErrorName != "" { - description += ", error: " + parsedErrorResponse.ErrorName - } + provider.updateToken(parsedResponse, now) - if parsedErrorResponse.ErrorDescription != "" { - description += fmt.Sprintf(", description: %q", parsedErrorResponse.ErrorDescription) - } + return nil +} - if parsedErrorResponse.ErrorURI != "" { - description += ", error_uri: " + parsedErrorResponse.ErrorURI +func readResponseBody(result *http.Response) ([]byte, error) { + if result.Body != nil { + data, err := io.ReadAll(result.Body) + if err != nil { + return nil, xerrors.WithStackTrace(err) } - return xerrors.WithStackTrace(fmt.Errorf("%w: %s", errCouldNotExchangeToken, description)) + return data, nil } + return make([]byte, 0), nil +} + +func (provider *oauth2TokenExchange) handleErrorResponse(status string, data []byte) error { + description := status + //nolint:tagliatelle - type response struct { - AccessToken string `json:"access_token"` - TokenType string `json:"token_type"` - ExpiresIn int64 `json:"expires_in"` - Scope string `json:"scope"` + type errorResponse struct { + ErrorName string `json:"error"` + ErrorDescription string `json:"error_description"` + ErrorURI string `json:"error_uri"` + } + var parsedErrorResponse errorResponse + if err := json.Unmarshal(data, &parsedErrorResponse); err != nil { + description += ", could not parse response: " + err.Error() + + return xerrors.WithStackTrace(fmt.Errorf("%w: %s", errCouldNotExchangeToken, description)) } - var parsedResponse response + + if parsedErrorResponse.ErrorName != "" { + description += ", error: " + parsedErrorResponse.ErrorName + } + if parsedErrorResponse.ErrorDescription != "" { + description += fmt.Sprintf(", description: %q", parsedErrorResponse.ErrorDescription) + } + if parsedErrorResponse.ErrorURI != "" { + description += ", error_uri: " + parsedErrorResponse.ErrorURI + } + + return xerrors.WithStackTrace(fmt.Errorf("%w: %s", errCouldNotExchangeToken, description)) +} + +//nolint:tagliatelle +type tokenResponse struct { + AccessToken string `json:"access_token"` + TokenType string `json:"token_type"` + ExpiresIn int64 `json:"expires_in"` + Scope string `json:"scope"` +} + +func parseTokenResponse(data []byte) (*tokenResponse, error) { + var parsedResponse tokenResponse if err := json.Unmarshal(data, &parsedResponse); err != nil { - return xerrors.WithStackTrace(fmt.Errorf("%w: %w", errCouldNotParseResponse, err)) + return nil, xerrors.WithStackTrace(fmt.Errorf("%w: %w", errCouldNotParseResponse, err)) } + return &parsedResponse, nil +} + +func validateTokenResponse(parsedResponse *tokenResponse, provider *oauth2TokenExchange) error { if !strings.EqualFold(parsedResponse.TokenType, "bearer") { return xerrors.WithStackTrace( fmt.Errorf("%w: %q", errUnsupportedTokenType, parsedResponse.TokenType)) @@ -423,18 +450,17 @@ func (provider *oauth2TokenExchange) processTokenExchangeResponse(result *http.R } } + return nil +} + +func (provider *oauth2TokenExchange) updateToken(parsedResponse *tokenResponse, now time.Time) { provider.receivedToken = "Bearer " + parsedResponse.AccessToken - // Expire time - expireDelta := time.Duration(parsedResponse.ExpiresIn) - expireDelta *= time.Second + expireDelta := time.Duration(parsedResponse.ExpiresIn) * time.Second provider.receivedTokenExpireTime = now.Add(expireDelta) - updateDelta := time.Duration(parsedResponse.ExpiresIn / updateTimeDivider) - updateDelta *= time.Second + updateDelta := time.Duration(parsedResponse.ExpiresIn/updateTimeDivider) * time.Second provider.updateTokenTime = now.Add(updateDelta) - - return nil } func (provider *oauth2TokenExchange) exchangeToken(ctx context.Context, now time.Time) error { diff --git a/internal/credentials/static.go b/internal/credentials/static.go index 7d9437dfb..5c6328992 100644 --- a/internal/credentials/static.go +++ b/internal/credentials/static.go @@ -87,9 +87,7 @@ func (c *Static) Token(ctx context.Context) (token string, err error) { fmt.Errorf("dial failed: %w", err), ) } - defer func() { - _ = cc.Close() - }() + defer cc.Close() client := Ydb_Auth_V1.NewAuthServiceClient(cc) diff --git a/internal/decimal/decimal.go b/internal/decimal/decimal.go index 2795592e8..5926562f5 100644 --- a/internal/decimal/decimal.go +++ b/internal/decimal/decimal.go @@ -224,6 +224,8 @@ func handleRemainingDigits(s string, v *big.Int, precision uint32) (*big.Int, er // Format returns the string representation of x with the given precision and // scale. +// +//nolint:funlen func Format(x *big.Int, precision, scale uint32) string { // Check for special values and nil pointer upfront. if x == nil { diff --git a/internal/value/nullable.go b/internal/value/nullable.go index 894b1ca8c..8a532d9e1 100644 --- a/internal/value/nullable.go +++ b/internal/value/nullable.go @@ -299,7 +299,7 @@ func NullableDyNumberValue(v *string) Value { // Nullable makes optional value from nullable type // Warning: type interface will be replaced in the future with typed parameters pattern from go1.18 // -//nolint:gocyclo +//nolint:gocyclo, funlen func Nullable(t types.Type, v interface{}) Value { switch t { case types.Bool: diff --git a/internal/value/value.go b/internal/value/value.go index 5e0b2a29f..095f56174 100644 --- a/internal/value/value.go +++ b/internal/value/value.go @@ -78,6 +78,7 @@ func nullValueFromYDB(x *Ydb.Value, t types.Type) (_ Value, ok bool) { } } +//nolint:funlen func primitiveValueFromYDB(t types.Primitive, v *Ydb.Value) (Value, error) { switch t { case types.Bool: @@ -167,6 +168,7 @@ func primitiveValueFromYDB(t types.Primitive, v *Ydb.Value) (Value, error) { } } +//nolint:funlen func fromYDB(t *Ydb.Type, v *Ydb.Value) (Value, error) { tt := types.TypeFromYDB(t) @@ -2307,6 +2309,7 @@ func YSONValue(v []byte) ysonValue { return v } +//nolint:funlen func zeroPrimitiveValue(t types.Primitive) Value { switch t { case types.Bool: diff --git a/retry/sql.go b/retry/sql.go index 23706971e..5bf974351 100644 --- a/retry/sql.go +++ b/retry/sql.go @@ -130,6 +130,8 @@ func WithTxOptions(txOptions *sql.TxOptions) txOptionsOption { } // DoTx is a retryer of database/sql transactions with fallbacks on errors +// +//nolint:funlen func DoTx(ctx context.Context, db *sql.DB, op func(context.Context, *sql.Tx) error, opts ...doTxOption) error { var ( options = doTxOptions{