Skip to content

Commit 0880f78

Browse files
authored
Merge pull request #1224 from brojeg/master
Enable funlen linter (part 4)
2 parents cd3bc77 + 59b3246 commit 0880f78

File tree

8 files changed

+83
-51
lines changed

8 files changed

+83
-51
lines changed

internal/cmd/gtrace/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
2121
)
2222

23-
//nolint:gocyclo
23+
//nolint:gocyclo,funlen
2424
func main() {
2525
var (
2626
// Reports whether we were called from go:generate.

internal/cmd/gtrace/writer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ func (w *Writer) composeHook(hook Hook, t1, t2, dst string) {
372372
w.line(`}`)
373373
}
374374

375+
//nolint:funlen
375376
func (w *Writer) composeHookCall(fn *Func, h1, h2 string) {
376377
w.newScope(func() {
377378
w.capture(h1, h2)

internal/credentials/oauth2.go

Lines changed: 72 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -349,62 +349,89 @@ func (provider *oauth2TokenExchange) getRequestParams() (string, error) {
349349
}
350350

351351
func (provider *oauth2TokenExchange) processTokenExchangeResponse(result *http.Response, now time.Time) error {
352-
var (
353-
data []byte
354-
err error
355-
)
356-
if result.Body != nil {
357-
data, err = io.ReadAll(result.Body)
358-
if err != nil {
359-
return xerrors.WithStackTrace(err)
360-
}
361-
} else {
362-
data = make([]byte, 0)
352+
data, err := readResponseBody(result)
353+
if err != nil {
354+
return err
363355
}
364356

365357
if result.StatusCode != http.StatusOK {
366-
description := result.Status
358+
return provider.handleErrorResponse(result.Status, data)
359+
}
367360

368-
//nolint:tagliatelle
369-
type errorResponse struct {
370-
ErrorName string `json:"error"`
371-
ErrorDescription string `json:"error_description"`
372-
ErrorURI string `json:"error_uri"`
373-
}
374-
var parsedErrorResponse errorResponse
375-
if err := json.Unmarshal(data, &parsedErrorResponse); err != nil {
376-
description += ", could not parse response: " + err.Error()
361+
parsedResponse, err := parseTokenResponse(data)
362+
if err != nil {
363+
return err
364+
}
377365

378-
return xerrors.WithStackTrace(fmt.Errorf("%w: %s", errCouldNotExchangeToken, description))
379-
}
366+
if err := validateTokenResponse(parsedResponse, provider); err != nil {
367+
return err
368+
}
380369

381-
if parsedErrorResponse.ErrorName != "" {
382-
description += ", error: " + parsedErrorResponse.ErrorName
383-
}
370+
provider.updateToken(parsedResponse, now)
384371

385-
if parsedErrorResponse.ErrorDescription != "" {
386-
description += fmt.Sprintf(", description: %q", parsedErrorResponse.ErrorDescription)
387-
}
372+
return nil
373+
}
388374

389-
if parsedErrorResponse.ErrorURI != "" {
390-
description += ", error_uri: " + parsedErrorResponse.ErrorURI
375+
func readResponseBody(result *http.Response) ([]byte, error) {
376+
if result.Body != nil {
377+
data, err := io.ReadAll(result.Body)
378+
if err != nil {
379+
return nil, xerrors.WithStackTrace(err)
391380
}
392381

393-
return xerrors.WithStackTrace(fmt.Errorf("%w: %s", errCouldNotExchangeToken, description))
382+
return data, nil
394383
}
395384

385+
return make([]byte, 0), nil
386+
}
387+
388+
func (provider *oauth2TokenExchange) handleErrorResponse(status string, data []byte) error {
389+
description := status
390+
396391
//nolint:tagliatelle
397-
type response struct {
398-
AccessToken string `json:"access_token"`
399-
TokenType string `json:"token_type"`
400-
ExpiresIn int64 `json:"expires_in"`
401-
Scope string `json:"scope"`
392+
type errorResponse struct {
393+
ErrorName string `json:"error"`
394+
ErrorDescription string `json:"error_description"`
395+
ErrorURI string `json:"error_uri"`
396+
}
397+
var parsedErrorResponse errorResponse
398+
if err := json.Unmarshal(data, &parsedErrorResponse); err != nil {
399+
description += ", could not parse response: " + err.Error()
400+
401+
return xerrors.WithStackTrace(fmt.Errorf("%w: %s", errCouldNotExchangeToken, description))
402402
}
403-
var parsedResponse response
403+
404+
if parsedErrorResponse.ErrorName != "" {
405+
description += ", error: " + parsedErrorResponse.ErrorName
406+
}
407+
if parsedErrorResponse.ErrorDescription != "" {
408+
description += fmt.Sprintf(", description: %q", parsedErrorResponse.ErrorDescription)
409+
}
410+
if parsedErrorResponse.ErrorURI != "" {
411+
description += ", error_uri: " + parsedErrorResponse.ErrorURI
412+
}
413+
414+
return xerrors.WithStackTrace(fmt.Errorf("%w: %s", errCouldNotExchangeToken, description))
415+
}
416+
417+
//nolint:tagliatelle
418+
type tokenResponse struct {
419+
AccessToken string `json:"access_token"`
420+
TokenType string `json:"token_type"`
421+
ExpiresIn int64 `json:"expires_in"`
422+
Scope string `json:"scope"`
423+
}
424+
425+
func parseTokenResponse(data []byte) (*tokenResponse, error) {
426+
var parsedResponse tokenResponse
404427
if err := json.Unmarshal(data, &parsedResponse); err != nil {
405-
return xerrors.WithStackTrace(fmt.Errorf("%w: %w", errCouldNotParseResponse, err))
428+
return nil, xerrors.WithStackTrace(fmt.Errorf("%w: %w", errCouldNotParseResponse, err))
406429
}
407430

431+
return &parsedResponse, nil
432+
}
433+
434+
func validateTokenResponse(parsedResponse *tokenResponse, provider *oauth2TokenExchange) error {
408435
if !strings.EqualFold(parsedResponse.TokenType, "bearer") {
409436
return xerrors.WithStackTrace(
410437
fmt.Errorf("%w: %q", errUnsupportedTokenType, parsedResponse.TokenType))
@@ -423,18 +450,17 @@ func (provider *oauth2TokenExchange) processTokenExchangeResponse(result *http.R
423450
}
424451
}
425452

453+
return nil
454+
}
455+
456+
func (provider *oauth2TokenExchange) updateToken(parsedResponse *tokenResponse, now time.Time) {
426457
provider.receivedToken = "Bearer " + parsedResponse.AccessToken
427458

428-
// Expire time
429-
expireDelta := time.Duration(parsedResponse.ExpiresIn)
430-
expireDelta *= time.Second
459+
expireDelta := time.Duration(parsedResponse.ExpiresIn) * time.Second
431460
provider.receivedTokenExpireTime = now.Add(expireDelta)
432461

433-
updateDelta := time.Duration(parsedResponse.ExpiresIn / updateTimeDivider)
434-
updateDelta *= time.Second
462+
updateDelta := time.Duration(parsedResponse.ExpiresIn/updateTimeDivider) * time.Second
435463
provider.updateTokenTime = now.Add(updateDelta)
436-
437-
return nil
438464
}
439465

440466
func (provider *oauth2TokenExchange) exchangeToken(ctx context.Context, now time.Time) error {

internal/credentials/static.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ func (c *Static) Token(ctx context.Context) (token string, err error) {
8787
fmt.Errorf("dial failed: %w", err),
8888
)
8989
}
90-
defer func() {
91-
_ = cc.Close()
92-
}()
90+
defer cc.Close()
9391

9492
client := Ydb_Auth_V1.NewAuthServiceClient(cc)
9593

internal/decimal/decimal.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ func handleRemainingDigits(s string, v *big.Int, precision uint32) (*big.Int, er
224224

225225
// Format returns the string representation of x with the given precision and
226226
// scale.
227+
//
228+
//nolint:funlen
227229
func Format(x *big.Int, precision, scale uint32) string {
228230
// Check for special values and nil pointer upfront.
229231
if x == nil {

internal/value/nullable.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ func NullableDyNumberValue(v *string) Value {
299299
// Nullable makes optional value from nullable type
300300
// Warning: type interface will be replaced in the future with typed parameters pattern from go1.18
301301
//
302-
//nolint:gocyclo
302+
//nolint:gocyclo, funlen
303303
func Nullable(t types.Type, v interface{}) Value {
304304
switch t {
305305
case types.Bool:

internal/value/value.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func nullValueFromYDB(x *Ydb.Value, t types.Type) (_ Value, ok bool) {
7878
}
7979
}
8080

81+
//nolint:funlen
8182
func primitiveValueFromYDB(t types.Primitive, v *Ydb.Value) (Value, error) {
8283
switch t {
8384
case types.Bool:
@@ -167,6 +168,7 @@ func primitiveValueFromYDB(t types.Primitive, v *Ydb.Value) (Value, error) {
167168
}
168169
}
169170

171+
//nolint:funlen
170172
func fromYDB(t *Ydb.Type, v *Ydb.Value) (Value, error) {
171173
tt := types.TypeFromYDB(t)
172174

@@ -2307,6 +2309,7 @@ func YSONValue(v []byte) ysonValue {
23072309
return v
23082310
}
23092311

2312+
//nolint:funlen
23102313
func zeroPrimitiveValue(t types.Primitive) Value {
23112314
switch t {
23122315
case types.Bool:

retry/sql.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ func WithTxOptions(txOptions *sql.TxOptions) txOptionsOption {
130130
}
131131

132132
// DoTx is a retryer of database/sql transactions with fallbacks on errors
133+
//
134+
//nolint:funlen
133135
func DoTx(ctx context.Context, db *sql.DB, op func(context.Context, *sql.Tx) error, opts ...doTxOption) error {
134136
var (
135137
options = doTxOptions{

0 commit comments

Comments
 (0)