From 72ae7116a5d54a30e3ef2abe01a09775c8e616f1 Mon Sep 17 00:00:00 2001 From: Rob Clemons Date: Thu, 7 Mar 2024 13:11:57 -0600 Subject: [PATCH] feat: add method to access flag metadata (#22) --- pkg/experiment/local/client.go | 17 +++++++++++++++++ pkg/experiment/local/client_test.go | 14 ++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/pkg/experiment/local/client.go b/pkg/experiment/local/client.go index 8efe8d0..7ba17e1 100644 --- a/pkg/experiment/local/client.go +++ b/pkg/experiment/local/client.go @@ -147,6 +147,23 @@ func (c *Client) FlagsV2() (string, error) { return flagsString, nil } +// FlagMetadata returns a copy of the flag's metadata. If the flag is not found then nil is returned. +func (c *Client) FlagMetadata(flagKey string) map[string]interface{} { + c.flagsMutex.RLock() + f := c.flags[flagKey] + c.flagsMutex.RUnlock() + if f == nil { + return nil + } + + metadata := make(map[string]interface{}) + for k, v := range f.Metadata { + metadata[k] = v + } + + return metadata +} + func (c *Client) doFlagsV2() (map[string]*evaluation.Flag, error) { client := &http.Client{} endpoint, err := url.Parse("https://api.lab.amplitude.com/") diff --git a/pkg/experiment/local/client_test.go b/pkg/experiment/local/client_test.go index 761bf87..be7cc67 100644 --- a/pkg/experiment/local/client_test.go +++ b/pkg/experiment/local/client_test.go @@ -143,3 +143,17 @@ func TestEvaluateV2UnknownFlagKey(t *testing.T) { t.Fatalf("Unexpected variant %v", variant) } } + +func TestFlagMetadataUnknownFlagKey(t *testing.T) { + md := client.FlagMetadata("does-not-exist") + if md != nil { + t.Fatalf("Unexpected metadata %v", md) + } +} + +func TestFlagMetadataLocalFlagKey(t *testing.T) { + md := client.FlagMetadata("sdk-local-evaluation-ci-test") + if md["evaluationMode"] != "local" { + t.Fatalf("Unexpected metadata %v", md) + } +}