Skip to content

Commit 890c3c8

Browse files
author
Dean Karn
authored
Add contextext package (#15)
1 parent a0a7339 commit 890c3c8

File tree

5 files changed

+86
-4
lines changed

5 files changed

+86
-4
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
branches:
55
- master
66
pull_request:
7+
types: [opened, edited, reopened, synchronize]
78
jobs:
89
test:
910
strategy:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pkg
22

3-
![Project status](https://img.shields.io/badge/version-5.6.2-green.svg)
3+
![Project status](https://img.shields.io/badge/version-5.7.0-green.svg)
44
[![Build Status](https://travis-ci.org/go-playground/pkg.svg?branch=master)](https://travis-ci.org/go-playground/pkg)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pkg/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pkg?branch=master)
66
[![GoDoc](https://godoc.org/github.com/go-playground/pkg?status.svg)](https://pkg.go.dev/mod/github.com/go-playground/pkg/v5)

context/context.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package contextext
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
)
8+
9+
var _ context.Context = (*detachedContext)(nil)
10+
11+
type detachedContext struct {
12+
parent context.Context
13+
}
14+
15+
// Detach returns a new context which continues to have access to its parent values but
16+
// is no longer bound/attached to the parents timeouts nor deadlines.
17+
//
18+
// If a nil context is passed in a new Background context will be used as the parent.
19+
//
20+
// This is useful for when you wish to pass along values such as span or logging information but do not want the
21+
// current operation to be cancelled despite what upstream code/callers think.
22+
func Detach(parent context.Context) detachedContext {
23+
if parent == nil {
24+
return detachedContext{parent: context.Background()}
25+
}
26+
return detachedContext{parent: parent}
27+
}
28+
29+
func (c detachedContext) Deadline() (deadline time.Time, ok bool) {
30+
return
31+
}
32+
33+
func (c detachedContext) Done() <-chan struct{} {
34+
return nil
35+
}
36+
37+
func (c detachedContext) Err() error {
38+
return nil
39+
}
40+
41+
func (c detachedContext) Value(key interface{}) interface{} {
42+
return c.parent.Value(key)
43+
}
44+
45+
func (c detachedContext) String() string {
46+
return fmt.Sprintf("%s.Detached", c.parent)
47+
}

context/context_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package contextext
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestDetach(t *testing.T) {
10+
11+
key := &struct{ name string }{name: "key"}
12+
ctx := context.WithValue(context.Background(), key, 13)
13+
ctx, cancel := context.WithTimeout(ctx, time.Nanosecond)
14+
cancel() // cancel ensuring context has been canceled
15+
16+
select {
17+
case <-ctx.Done():
18+
default:
19+
t.Fatal("expected context to be cancelled")
20+
}
21+
22+
ctx = Detach(ctx)
23+
24+
select {
25+
case <-ctx.Done():
26+
t.Fatal("expected context to be detached from parents cancellation")
27+
default:
28+
rawValue := ctx.Value(key)
29+
n, ok := rawValue.(int)
30+
if !ok || n != 13 {
31+
t.Fatalf("expected integer woth value of 13 but got %v", rawValue)
32+
}
33+
}
34+
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module github.com/go-playground/pkg/v5
22

3+
go 1.18
4+
35
require (
46
github.com/go-playground/assert/v2 v2.2.0
57
github.com/go-playground/form/v4 v4.2.0
6-
)
7-
8-
go 1.18
8+
)

0 commit comments

Comments
 (0)