From 5d411bbc86b9f30b507479bf32cbcfaa2dd1b010 Mon Sep 17 00:00:00 2001 From: "mojo-machine[bot]" <111131124+mojo-machine[bot]@users.noreply.github.com> Date: Fri, 3 Jan 2025 12:31:44 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=84=20Sync=20from=20monorepo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/wearemojo/mojo/commit/9aa23e198a15166928115e395a1127353a096187 --- lib/ab/ab_test.go | 2 +- lib/crpc/server_test.go | 1 - lib/ksuid/id_test.go | 2 +- lib/ksuid/node_test.go | 2 +- lib/middleware/request/logger.go | 55 ++++++++++++++++---------------- lib/revenuecat/revenuecat.go | 5 +-- 6 files changed, 33 insertions(+), 34 deletions(-) diff --git a/lib/ab/ab_test.go b/lib/ab/ab_test.go index a7a15a3..b975d01 100644 --- a/lib/ab/ab_test.go +++ b/lib/ab/ab_test.go @@ -44,7 +44,7 @@ var ( ) func BenchmarkParse(b *testing.B) { - for i := 0; i < b.N; i++ { + for range b.N { Check(testID, userID, 5000) } } diff --git a/lib/crpc/server_test.go b/lib/crpc/server_test.go index acfeffb..a7d71d8 100644 --- a/lib/crpc/server_test.go +++ b/lib/crpc/server_test.go @@ -1,4 +1,3 @@ -//nolint:bodyclose // incorrect package crpc import ( diff --git a/lib/ksuid/id_test.go b/lib/ksuid/id_test.go index 95fbde4..025690e 100644 --- a/lib/ksuid/id_test.go +++ b/lib/ksuid/id_test.go @@ -111,7 +111,7 @@ func TestParse(t *testing.T) { } func BenchmarkParse(b *testing.B) { - for i := 0; i < b.N; i++ { + for range b.N { _, _ = Parse("user_000000BPG6Lks9tQoAiJYrBRSXPX6") } } diff --git a/lib/ksuid/node_test.go b/lib/ksuid/node_test.go index d4a02ae..e800840 100644 --- a/lib/ksuid/node_test.go +++ b/lib/ksuid/node_test.go @@ -6,7 +6,7 @@ import ( ) func BenchmarkGenerate(b *testing.B) { - for i := 0; i < b.N; i++ { + for range b.N { Generate(context.Background(), "user") } } diff --git a/lib/middleware/request/logger.go b/lib/middleware/request/logger.go index 462d82a..aad8c27 100644 --- a/lib/middleware/request/logger.go +++ b/lib/middleware/request/logger.go @@ -97,36 +97,35 @@ func Logger(log *logrus.Entry) func(http.Handler) http.Handler { err := getError(clog.Get(ctx)) if err == nil { mlog.Info(ctx, merr.New(ctx, "request_completed", nil)) - } else { - var fn func(context.Context, merr.Merrer) - switch clog.DetermineLevel(err, clog.TimeoutsAsErrors(ctx)) { - case - logrus.PanicLevel, - logrus.FatalLevel, - logrus.ErrorLevel: - fn = mlog.Error - case - logrus.WarnLevel: - fn = mlog.Warn - case - logrus.InfoLevel, - logrus.DebugLevel, - logrus.TraceLevel: - fn = mlog.Info - } + return + } + var fn func(context.Context, merr.Merrer) + switch clog.DetermineLevel(err, clog.TimeoutsAsErrors(ctx)) { + case + logrus.PanicLevel, + logrus.FatalLevel, + logrus.ErrorLevel: + fn = mlog.Error + case logrus.WarnLevel: + fn = mlog.Warn + case + logrus.InfoLevel, + logrus.DebugLevel, + logrus.TraceLevel: + fn = mlog.Info + } - if mErr, ok := gerrors.As[merr.E](err); ok { - fn(ctx, mErr) - } else if cErr, ok := gerrors.As[cher.E](err); ok { - reasons := slicefn.Map(cErr.Reasons, func(r cher.E) error { return r }) - // If the cher error has no reasons, add the cher error itself - if len(reasons) == 0 { - reasons = append(reasons, cErr) - } - fn(ctx, merr.New(ctx, merr.Code(cErr.Code), merr.M(cErr.Meta), reasons...)) - } else { - fn(ctx, merr.New(ctx, "unexpected_request_failure", nil, err)) + if mErr, ok := gerrors.As[merr.E](err); ok { + fn(ctx, mErr) + } else if cErr, ok := gerrors.As[cher.E](err); ok { + reasons := slicefn.Map(cErr.Reasons, func(r cher.E) error { return r }) + // If the cher error has no reasons, add the cher error itself + if len(reasons) == 0 { + reasons = append(reasons, cErr) } + fn(ctx, merr.New(ctx, merr.Code(cErr.Code), merr.M(cErr.Meta), reasons...)) + } else { + fn(ctx, merr.New(ctx, "unexpected_request_failure", nil, err)) } }) } diff --git a/lib/revenuecat/revenuecat.go b/lib/revenuecat/revenuecat.go index 0191919..f17ef6c 100644 --- a/lib/revenuecat/revenuecat.go +++ b/lib/revenuecat/revenuecat.go @@ -3,14 +3,15 @@ package revenuecat import ( "context" "fmt" + "maps" "net/url" + "slices" "time" "github.com/samber/lo" "github.com/wearemojo/mojo-public-go/lib/httpclient" "github.com/wearemojo/mojo-public-go/lib/jsonclient" "github.com/wearemojo/mojo-public-go/lib/secret" - "golang.org/x/exp/maps" ) const baseURL = "https://api.revenuecat.com/v1" @@ -92,7 +93,7 @@ func (c *Client) GetOrCreateSubscriberInfo(ctx context.Context, appUserID string } func (s *Subscriber) ActiveSubscriptionCount() int { - activeNonSubCount := len(lo.Flatten(maps.Values(s.NonSubscriptions))) // always active + activeNonSubCount := len(lo.Flatten(slices.Collect(maps.Values(s.NonSubscriptions)))) // always active activeSubCount := 0 now := time.Now()