Skip to content

Commit

Permalink
use stdlib context.WithoutCancel (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
leclark authored Aug 15, 2023
1 parent 5df99bb commit 307c8a1
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
run:
go: 1.18
go: 1.21
deadline: 10m
skip-dirs:
- temp
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
39 changes: 1 addition & 38 deletions logctx/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package logctx

import (
"context"
"fmt"
"time"

"github.com/rs/zerolog"
)
Expand All @@ -17,7 +15,7 @@ func NewContextFrom(ctx context.Context) context.Context {

// NewSubLoggerContext creates a new logger with an empty context.
func NewSubLoggerContext(ctx context.Context, log zerolog.Logger) context.Context {
return log.With().Logger().WithContext(WithoutCancel(ctx))
return log.With().Logger().WithContext(context.WithoutCancel(ctx))
}

// AddStrToContext adds the key/value to the log context.
Expand All @@ -40,38 +38,3 @@ func AddMapToContext(ctx context.Context, fields map[string]any) {
return c.Fields(fields)
})
}

// WithoutCancel is a port from go1.21. Placeholder until generally available in stdlib.
func WithoutCancel(parent context.Context) context.Context {
if parent == nil {
panic("cannot create context from nil parent")
}

return withoutCancelCtx{parent}
}

//nolint:containedctx
type withoutCancelCtx struct {
ctx context.Context
}

//nolint:nonamedreturns
func (withoutCancelCtx) Deadline() (deadline time.Time, ok bool) {
return
}

func (withoutCancelCtx) Done() <-chan struct{} {
return nil
}

func (withoutCancelCtx) Err() error {
return nil
}

func (c withoutCancelCtx) Value(key any) any {
return c.ctx.Value(key)
}

func (c withoutCancelCtx) String() string {
return fmt.Sprintf("%s", c.ctx) + ".WithoutCancel"
}
14 changes: 0 additions & 14 deletions logctx/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/bir/iken/logctx"
)
Expand Down Expand Up @@ -41,16 +40,3 @@ func TestLogContext(t *testing.T) {
assert.Equal(t, id, logctx.GetID(ctx))
assert.Equal(t, id, logctx.GetID(ctx2))
}

func TestWithoutCancel(t *testing.T) {
assert.Panics(t, func() { logctx.WithoutCancel(nil) })
ctx := logctx.WithoutCancel(context.Background())

deadline, ok := ctx.Deadline()
assert.False(t, ok)
assert.Zero(t, deadline)

s, ok := ctx.(interface{ String() string })
require.True(t, ok)
assert.Equal(t, "context.Background.WithoutCancel", s.String())
}

0 comments on commit 307c8a1

Please sign in to comment.