Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
gostnops
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
gostnops
Commits
da528eb1
Commit
da528eb1
authored
Jan 11, 2020
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add: context value
parent
9f514399
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
98 additions
and
0 deletions
+98
-0
context.go
context/context.go
+73
-0
context_test.go
context/context_test.go
+25
-0
No files found.
context/context.go
0 → 100644
View file @
da528eb1
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
gxcontext
import
(
"context"
)
var
(
defaultCtxKey
=
1
)
type
Values
struct
{
m
map
[
interface
{}]
interface
{}
}
func
(
v
Values
)
Get
(
key
interface
{})
(
interface
{},
bool
)
{
i
,
b
:=
v
.
m
[
key
]
return
i
,
b
}
func
(
c
Values
)
Set
(
key
interface
{},
value
interface
{})
{
c
.
m
[
key
]
=
value
}
func
(
c
Values
)
Delete
(
key
interface
{})
{
delete
(
c
.
m
,
key
)
}
type
ValuesContext
struct
{
context
.
Context
}
func
NewValuesContext
(
ctx
context
.
Context
)
*
ValuesContext
{
if
ctx
==
nil
{
ctx
=
context
.
Background
()
}
return
&
ValuesContext
{
Context
:
context
.
WithValue
(
ctx
,
defaultCtxKey
,
Values
{
m
:
make
(
map
[
interface
{}]
interface
{},
4
)},
),
}
}
func
(
c
*
ValuesContext
)
Get
(
key
interface
{})
(
interface
{},
bool
)
{
return
c
.
Context
.
Value
(
defaultCtxKey
)
.
(
Values
)
.
Get
(
key
)
}
func
(
c
*
ValuesContext
)
Delete
(
key
interface
{})
{
c
.
Context
.
Value
(
defaultCtxKey
)
.
(
Values
)
.
Delete
(
key
)
}
func
(
c
*
ValuesContext
)
Set
(
key
interface
{},
value
interface
{})
{
c
.
Context
.
Value
(
defaultCtxKey
)
.
(
Values
)
.
Set
(
key
,
value
)
}
context/context_test.go
0 → 100644
View file @
da528eb1
package
gxcontext
import
(
"testing"
)
import
(
"github.com/stretchr/testify/assert"
)
func
TestValuesContext_General
(
t
*
testing
.
T
)
{
vc
:=
NewValuesContext
(
nil
)
assert
.
NotNil
(
t
,
vc
)
key
:=
"hello"
value
:=
"world"
v
,
ok
:=
vc
.
Get
(
key
)
assert
.
Nil
(
t
,
v
)
assert
.
False
(
t
,
ok
)
vc
.
Set
(
key
,
value
)
v
,
ok
=
vc
.
Get
(
key
)
assert
.
Equal
(
t
,
v
.
(
string
),
value
)
assert
.
True
(
t
,
ok
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment