Documentation
¶
Overview ¶
Package metadata provides a map of named any elements with generic support for type-safe Get and nil-safe Set. Metadata keys often function as optional fields in a struct, and therefore a CamelCase naming convention is typical. Provides default support for "Name", "Doc", "File" standard keys.
Index ¶
- func Doc(obj any) string
- func File(obj any) *os.File
- func Filename(obj any) string
- func Get[T any](obj any, key string) (T, error)
- func GetFromData[T any](md Data, key string) (T, error)
- func Name(obj any) string
- func Set(obj any, key string, value any) error
- func SetDoc(obj any, doc string)
- func SetFile(obj any, file *os.File)
- func SetFilename(obj any, file string)
- func SetName(obj any, name string)
- type Data
- type Metadataer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Filename ¶ added in v0.3.8
Filename returns the "Filename" standard key value (empty if not set).
func Get ¶
Get gets metadata value of given type from given object, if it implements the Metadata() method. Must pass a pointer to the object. Returns error if not present or item is a different type.
func GetFromData ¶ added in v0.3.8
GetFromData gets metadata value of given type from given Data. Returns error if not present or item is a different type.
func Set ¶ added in v0.3.8
Set sets metadata value on given object, if it implements the Metadata() method. Returns error if no Metadata on object. Must pass a pointer to the object.
func SetFilename ¶ added in v0.3.8
SetFilename sets the "Filename" standard key.
Types ¶
type Data ¶
Data is metadata as a map of named any elements with generic support for type-safe Get and nil-safe Set. Metadata keys often function as optional fields in a struct, and therefore a CamelCase naming convention is typical. Provides default support for "Name" and "Doc" standard keys. In general it is good practice to provide access functions that establish standard key names, to avoid issues with typos.
func GetData ¶ added in v0.3.8
GetData gets the Data from given object, if it implements the Metadata() method. Returns nil if it does not. Must pass a pointer to the object.
type Metadataer ¶ added in v0.3.8
type Metadataer interface {
Metadata() *Data
}
Metadataer is an interface for a type that returns associated metadata.Data using a Metadata() method. To be able to set metadata, the method should be defined with a pointer receiver.