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

Enable funlen linter (part 4) #1224

Merged
merged 2 commits into from
May 15, 2024
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
2 changes: 1 addition & 1 deletion internal/cmd/gtrace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/gtrace/writer.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think tool gtrace no need to change because it is an ad-hoc tool

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the breaking action was broken with your PR - because you change a code of gtrace tool

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for the review. I've rolled back [writer.go] and added //nolint:funlen where needed.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
118 changes: 72 additions & 46 deletions internal/credentials/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions internal/credentials/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions internal/decimal/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/value/nullable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions internal/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -2307,6 +2309,7 @@ func YSONValue(v []byte) ysonValue {
return v
}

//nolint:funlen
func zeroPrimitiveValue(t types.Primitive) Value {
switch t {
case types.Bool:
Expand Down
2 changes: 2 additions & 0 deletions retry/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Loading