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

feat: enable original key to be returned for metadata property fields #88

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 34 additions & 0 deletions metadata/properties.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2024 The Dapr 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 metadata

// Properties contains metadata properties, as a key-value dictionary
type Properties map[string]string

// GetProperty returns a property from the metadata, with support for case-insensitive keys and aliases.
func (p Properties) GetProperty(keys ...string) (val string, ok bool) {
return GetMetadataProperty(p, keys...)
}

// GetPropertyWithMatchedKey returns a property from the metadata, with support for case-insensitive keys and aliases,
// while returning the original matching metadata field key.
func (p Properties) GetPropertyWithMatchedKey(keys ...string) (key string, val string, ok bool) {
return GetMetadataPropertyWithMatchedKey(p, keys...)
}

// Decode decodes metadata into a struct.
// This is an extension of mitchellh/mapstructure which also supports decoding durations.
func (p Properties) Decode(result any) error {
return decodeMetadataMap(p, result)
}
17 changes: 14 additions & 3 deletions metadata/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,24 @@ import (

// GetMetadataProperty returns a property from the metadata map, with support for case-insensitive keys and aliases.
func GetMetadataProperty(props map[string]string, keys ...string) (val string, ok bool) {
_, val, ok = GetMetadataPropertyWithMatchedKey(props, keys...)
return val, ok
}

// GetMetadataPropertyWithMatchedKey returns a property from the metadata map, with support for case-insensitive keys and aliases,
// while returning the original matching metadata field key.
func GetMetadataPropertyWithMatchedKey(props map[string]string, keys ...string) (key string, val string, ok bool) {
lcProps := make(map[string]string, len(props))
for k, v := range props {
lcProps[strings.ToLower(k)] = v
}
for _, k := range keys {
val, ok = lcProps[strings.ToLower(k)]
if ok {
return val, true
return k, val, true
}
}
return "", false
return "", "", false
}

// DecodeMetadata decodes a component metadata into a struct.
Expand All @@ -55,8 +62,12 @@ func DecodeMetadata(input any, result any) error {
return fmt.Errorf("input object cannot be cast to map[string]string: %w", err)
}

return decodeMetadataMap(inputMap, result)
}

func decodeMetadataMap(inputMap map[string]string, result any) error {
// Handle aliases
err = resolveAliases(inputMap, reflect.TypeOf(result))
err := resolveAliases(inputMap, reflect.TypeOf(result))
if err != nil {
return fmt.Errorf("failed to resolve aliases: %w", err)
}
Expand Down
44 changes: 44 additions & 0 deletions metadata/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,47 @@ func TestResolveAliases(t *testing.T) {
})
}
}

func TestGetMetadataPropertyWithMatchedKey(t *testing.T) {
props := map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"emptyKey": "",
}

t.Run("Existing key", func(t *testing.T) {
key, val, ok := GetMetadataPropertyWithMatchedKey(props, "key1", "key2")
assert.True(t, ok)
assert.Equal(t, "key1", key)
assert.Equal(t, "value1", val)
})

t.Run("Case-insensitive matching", func(t *testing.T) {
key, val, ok := GetMetadataPropertyWithMatchedKey(props, "KEY1")
assert.True(t, ok)
assert.Equal(t, "KEY1", key)
assert.Equal(t, "value1", val)
})

t.Run("Non-existing key", func(t *testing.T) {
key, val, ok := GetMetadataPropertyWithMatchedKey(props, "key4")
assert.False(t, ok)
assert.Equal(t, "", key)
assert.Equal(t, "", val)
})

t.Run("Empty properties", func(t *testing.T) {
key, val, ok := GetMetadataPropertyWithMatchedKey(nil, "key1")
assert.False(t, ok)
assert.Equal(t, "", key)
assert.Equal(t, "", val)
})

t.Run("Value is empty", func(t *testing.T) {
key, val, ok := GetMetadataPropertyWithMatchedKey(props, "EmptyKey")
assert.True(t, ok)
assert.Equal(t, "EmptyKey", key)
assert.Equal(t, "", val)
})
}
Loading