-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Binbin Li <[email protected]> Co-authored-by: Susan Shi <[email protected]>
- Loading branch information
Showing
5 changed files
with
141 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
Copyright The Ratify Authors. | ||
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. | ||
*/ | ||
|
||
package context | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
type contextKey string | ||
|
||
const contextKeyNamespace = contextKey("namespace") | ||
|
||
// SetContextWithNamespace embeds namespace to the context. | ||
func SetContextWithNamespace(ctx context.Context, namespace string) context.Context { | ||
return context.WithValue(ctx, contextKeyNamespace, namespace) | ||
} | ||
|
||
// CreateCacheKey creates a new cache key prefixed with embedded namespace. | ||
func CreateCacheKey(ctx context.Context, key string) string { | ||
namespace := ctx.Value(contextKeyNamespace) | ||
if namespace == nil { | ||
return key | ||
} | ||
|
||
namespaceStr := namespace.(string) | ||
if namespaceStr == "" { | ||
return key | ||
} | ||
return fmt.Sprintf("%s:%s", namespaceStr, key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright The Ratify Authors. | ||
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. | ||
*/ | ||
|
||
package context | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
const ( | ||
testNamespace = "testNamespace" | ||
testKey = "testKey" | ||
) | ||
|
||
func TestSetContext(t *testing.T) { | ||
ctx := context.Background() | ||
ctx = SetContextWithNamespace(ctx, testNamespace) | ||
namespace := ctx.Value(contextKeyNamespace).(string) | ||
if namespace != testNamespace { | ||
t.Fatalf("expected namespace %s, got %s", testNamespace, namespace) | ||
} | ||
} | ||
|
||
func TestCreateCacheKey(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
key string | ||
namespaceSet bool | ||
namespace string | ||
expectedKey string | ||
}{ | ||
{ | ||
name: "no namespace", | ||
key: testKey, | ||
namespaceSet: false, | ||
expectedKey: testKey, | ||
}, | ||
{ | ||
name: "empty namespace", | ||
key: testKey, | ||
namespaceSet: true, | ||
namespace: "", | ||
expectedKey: testKey, | ||
}, | ||
{ | ||
name: "with namespace", | ||
key: testKey, | ||
namespaceSet: true, | ||
namespace: testNamespace, | ||
expectedKey: "testNamespace:testKey", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ctx := context.Background() | ||
if tc.namespaceSet { | ||
ctx = SetContextWithNamespace(ctx, tc.namespace) | ||
} | ||
|
||
key := CreateCacheKey(ctx, tc.key) | ||
if key != tc.expectedKey { | ||
t.Fatalf("expected key %s, got %s", tc.expectedKey, key) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters