Skip to content

Commit

Permalink
feat: add GetBackwardValueToSend api to get a single backward value t…
Browse files Browse the repository at this point in the history
…o send from the given context (bytedance#164)
  • Loading branch information
jayantxie authored Feb 2, 2024
1 parent a5eedbe commit 5e26950
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cloud/metainfo/backward.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ func SendBackwardValuesFromMap(ctx context.Context, kvs map[string]string) (ok b
return
}

// GetBackwardValueToSend gets a value associated with the given key that is set by
// `SendBackwardValue`, `SendBackwardValues` or `SendBackwardValuesFromMap`.
func GetBackwardValueToSend(ctx context.Context, key string) (val string, ok bool) {
if p, exist := ctx.Value(bwCtxKeySend).(*bwCtxValue); exist {
val, ok = p.get(key)
}
return
}

// AllBackwardValuesToSend retrieves all key-values pairs set by `SendBackwardValue`
// or `SendBackwardValues` from the given context.
// This function is designed for frameworks, common developers should not use it.
Expand Down
4 changes: 4 additions & 0 deletions cloud/metainfo/backward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func TestWithBackwardValues5(t *testing.T) {
ok = metainfo.SendBackwardValues(ctx3, "key", "send2", "key1")
assert(t, !ok)

val, ok = metainfo.GetBackwardValueToSend(ctx3, "key")
assert(t, ok)
assert(t, val == "send0")

m = metainfo.RecvAllBackwardValues(ctx3)
assert(t, len(m) == 2)
assert(t, m["key"] == "recv0" && m["key1"] == "recv1", m)
Expand Down

0 comments on commit 5e26950

Please sign in to comment.