fix(metadata): nil-Metadata from NewMetadata(nil), missing float32 in GetFloat; add doc comments and tests

NewMetadata(nil) previously returned a nil Metadata interface, causing
panics on direct .Get()/.IsExists() calls. Now returns an empty map.
GetFloat added float32→float64 promotion, matching GetString behavior.
Doc comments for all 13 exported symbols across both packages.
41 unit tests (11 metadata + 30 util) at 100% coverage.
This commit is contained in:
ginuerzh
2026-05-24 20:59:30 +08:00
parent dc99f4731f
commit a9d94e7009
4 changed files with 560 additions and 3 deletions
+7 -3
View File
@@ -1,3 +1,5 @@
// Package metadata provides the default implementation of the core/metadata.Metadata
// interface using a map[string]any with case-insensitive keys.
package metadata
import (
@@ -8,10 +10,9 @@ import (
type mapMetadata map[string]any
// NewMetadata creates a new Metadata from the given map. Keys are lowercased for
// case-insensitive lookup. If m is nil, an empty Metadata is returned.
func NewMetadata(m map[string]any) metadata.Metadata {
if m == nil {
return nil
}
md := make(map[string]any)
for k, v := range m {
md[strings.ToLower(k)] = v
@@ -19,15 +20,18 @@ func NewMetadata(m map[string]any) metadata.Metadata {
return mapMetadata(md)
}
// IsExists reports whether the key is present in the metadata.
func (m mapMetadata) IsExists(key string) bool {
_, ok := m[strings.ToLower(key)]
return ok
}
// Set stores a value for the given key.
func (m mapMetadata) Set(key string, value any) {
m[strings.ToLower(key)] = value
}
// Get retrieves the value for the given key, or nil if the key is not present.
func (m mapMetadata) Get(key string) any {
if m != nil {
return m[strings.ToLower(key)]