Skip to content

Commit ac95021

Browse files
author
Gleb Brozhe
committed
refactor for funlen linter
1 parent 9d49511 commit ac95021

File tree

4 files changed

+133
-92
lines changed

4 files changed

+133
-92
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: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -385,57 +385,70 @@ func (w *Writer) composeHookCall(fn *Func, h1, h2 string) {
385385
w.funcResults(fn)
386386
w.line(` {`)
387387
w.line(`if options.panicCallback != nil {`)
388-
w.block(func() {
389-
w.line("defer func() {")
390-
w.block(func() {
391-
w.line("if e := recover(); e != nil {")
392-
w.block(func() {
393-
w.line(`options.panicCallback(e)`)
394-
})
395-
w.line("}")
396-
})
397-
w.line("}()")
398-
})
388+
w.addPanicCallback()
399389
w.line("}")
400-
var (
401-
r1 string
402-
r2 string
403-
rs []string
404-
)
405390
if fn.HasResult() {
406-
r1 = w.declare("r")
407-
r2 = w.declare("r")
408-
rs = []string{r1, r2}
409-
w.code("var " + r1 + ", " + r2 + " ")
410-
w.funcResults(fn)
411-
_ = w.bw.WriteByte('\n')
412-
w.atEOL = true
413-
}
414-
for i, h := range []string{h1, h2} {
415-
w.line("if " + h + " != nil {")
416-
w.block(func() {
417-
if fn.HasResult() {
418-
w.code(rs[i], ` = `) //nolint:scopelint
419-
}
420-
w.code(h) //nolint:scopelint
421-
w.call(args)
422-
})
423-
w.line("}")
391+
rs := w.declareResults(fn)
392+
w.addHookCalls(fn, h1, h2, args, rs)
393+
w.addReturnStatement(fn, rs)
394+
} else {
395+
w.addHookCalls(fn, h1, h2, args, nil)
424396
}
397+
w.line(`}`)
398+
})
399+
})
400+
}
401+
402+
func (w *Writer) addPanicCallback() {
403+
w.block(func() {
404+
w.line("defer func() {")
405+
w.block(func() {
406+
w.line("if e := recover(); e != nil {")
407+
w.block(func() {
408+
w.line(`options.panicCallback(e)`)
409+
})
410+
w.line("}")
411+
})
412+
w.line("}()")
413+
})
414+
}
415+
416+
func (w *Writer) declareResults(fn *Func) []string {
417+
r1 := w.declare("r")
418+
r2 := w.declare("r")
419+
rs := []string{r1, r2}
420+
w.code("var " + r1 + ", " + r2 + " ")
421+
w.funcResults(fn)
422+
_ = w.bw.WriteByte('\n')
423+
w.atEOL = true
424+
425+
return rs
426+
}
427+
428+
func (w *Writer) addHookCalls(fn *Func, h1, h2 string, args, rs []string) {
429+
for i, h := range []string{h1, h2} {
430+
w.line("if " + h + " != nil {")
431+
w.block(func() {
425432
if fn.HasResult() {
426-
w.code(`return `)
427-
switch x := fn.Result[0].(type) {
428-
case *Func:
429-
w.composeHookCall(x, r1, r2)
430-
case *Trace:
431-
w.line(r1, `.Compose(`, r2, `)`)
432-
default:
433-
panic("unknown result type")
434-
}
433+
w.code(rs[i], ` = `) //nolint:scopelint
435434
}
435+
w.code(h) //nolint:scopelint
436+
w.call(args)
436437
})
437-
w.line(`}`)
438-
})
438+
w.line("}")
439+
}
440+
}
441+
442+
func (w *Writer) addReturnStatement(fn *Func, rs []string) {
443+
w.code(`return `)
444+
switch x := fn.Result[0].(type) {
445+
case *Func:
446+
w.composeHookCall(x, rs[0], rs[1])
447+
case *Trace:
448+
w.line(rs[0], `.Compose(`, rs[1], `)`)
449+
default:
450+
panic("unknown result type")
451+
}
439452
}
440453

441454
func (w *Writer) options(trace *Trace) {

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/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 {

0 commit comments

Comments
 (0)