Skip to content

Commit

Permalink
code refractor and unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Jimil Desai <jimildesai42@gmail.com>
  • Loading branch information
jimil749 committed Jul 30, 2021
1 parent 5ca037a commit eaa3683
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 40 deletions.
40 changes: 0 additions & 40 deletions pkg/appctx/appctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,13 @@ package appctx

import (
"context"
"unsafe"

"github.com/rs/zerolog"
)

// DeletingSharedResource flags to a storage a shared resource is being deleted not by the owner.
var DeletingSharedResource struct{}

type emptyCtx int

type valueCtx struct {
context.Context
key, val interface{}
}

type iface struct {
itab, data uintptr
}

// GetKeyValues retrieves all the key-value pairs from the provided context.
func GetKeyValues(ctx context.Context) map[interface{}]interface{} {
m := make(map[interface{}]interface{})
getKeyValue(ctx, m)
return m
}

// PutKeyValues puts
func PutKeyValues(m map[interface{}]interface{}) context.Context {
ctx := context.Background()
for key, value := range m {
ctx = context.WithValue(ctx, key, value)
}
return ctx
}

func getKeyValue(ctx context.Context, m map[interface{}]interface{}) {
ictx := *(*iface)(unsafe.Pointer(&ctx))
if ictx.data == 0 || int(*(*emptyCtx)(unsafe.Pointer(ictx.data))) == 0 {
return
}
valCtx := (*valueCtx)(unsafe.Pointer(ictx.data))
if valCtx != nil && valCtx.key != nil {
m[valCtx.key] = valCtx.val
}
getKeyValue(valCtx.Context, m)
}

// WithLogger returns a context with an associated logger.
func WithLogger(ctx context.Context, l *zerolog.Logger) context.Context {
return l.WithContext(ctx)
Expand Down
63 changes: 63 additions & 0 deletions pkg/appctx/ctxmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2018-2021 CERN
//
// Licensed 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package appctx

import (
"context"
"unsafe"
)

type emptyCtx int

type valueCtx struct {
context.Context
key, val interface{}
}

type iface struct {
itab, data uintptr //nolint
}

// GetKeyValues retrieves all the key-value pairs from the provided context.
func GetKeyValues(ctx context.Context) map[interface{}]interface{} {
m := make(map[interface{}]interface{})
getKeyValue(ctx, m)
return m
}

// PutKeyValues puts
func PutKeyValues(m map[interface{}]interface{}) context.Context {
ctx := context.Background()
for key, value := range m {
ctx = context.WithValue(ctx, key, value)
}
return ctx
}

func getKeyValue(ctx context.Context, m map[interface{}]interface{}) {
ictx := *(*iface)(unsafe.Pointer(&ctx))
if ictx.data == 0 || int(*(*emptyCtx)(unsafe.Pointer(ictx.data))) == 0 {
return
}
valCtx := (*valueCtx)(unsafe.Pointer(ictx.data))
if valCtx != nil && valCtx.key != nil {
m[valCtx.key] = valCtx.val
}
getKeyValue(valCtx.Context, m)
}
90 changes: 90 additions & 0 deletions pkg/appctx/ctxmap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2018-2021 CERN
//
// Licensed 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package appctx

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetKeyValues(t *testing.T) {
tests := []struct {
name string
ctx context.Context
m map[interface{}]interface{}
}{
{
"Background context",
context.Background(),
map[interface{}]interface{}{},
},
{
"Context with Values",
context.WithValue(context.Background(), "key", "value"), //nolint
map[interface{}]interface{}{
"key": "value",
},
},
{
"Nested Context with Values",
context.WithValue(context.WithValue(context.Background(), "key", "value"), "key2", "value2"), //nolint
map[interface{}]interface{}{
"key": "value",
"key2": "value2",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
kvMap := GetKeyValues(tt.ctx)
assert.Equal(t, tt.m, kvMap)
})
}
}

func TestPutKeyValues(t *testing.T) {
tests := []struct {
name string
m map[interface{}]interface{}
ctx context.Context
}{
{
"empty context",
map[interface{}]interface{}{},
context.Background(),
},
{
"single kv pair",
map[interface{}]interface{}{
"key": "value",
},
context.WithValue(context.Background(), "key", "value"), //nolint
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := PutKeyValues(tt.m)
assert.Equal(t, tt.ctx, ctx)
})
}
}

0 comments on commit eaa3683

Please sign in to comment.