Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added methods to get and put data into context #1938

Merged
merged 5 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/context-methods.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Add methods to get and put context values.

Added `GetKeyValues` and `PutKeyValues` methods to fetch/put values from/to context.

https://github.com/cs3org/reva/pull/1938
40 changes: 40 additions & 0 deletions pkg/appctx/appctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,53 @@ 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