Files
x/metadata/metadata.go
T
ginuerzh a9d94e7009 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.
2026-05-24 20:59:30 +08:00

41 lines
1.0 KiB
Go

// Package metadata provides the default implementation of the core/metadata.Metadata
// interface using a map[string]any with case-insensitive keys.
package metadata
import (
"strings"
"github.com/go-gost/core/metadata"
)
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 {
md := make(map[string]any)
for k, v := range m {
md[strings.ToLower(k)] = v
}
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)]
}
return nil
}