metadata

package
v0.3.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2025 License: BSD-3-Clause Imports: 4 Imported by: 20

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Doc added in v0.3.8

func Doc(obj any) string

Doc returns the "Doc" standard key value (empty if not set).

func File added in v0.3.8

func File(obj any) *os.File

File returns the "File" standard key value (nil if not set).

func Filename added in v0.3.8

func Filename(obj any) string

Filename returns the "Filename" standard key value (empty if not set).

func Get

func Get[T any](obj any, key string) (T, error)

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

func GetFromData[T any](md Data, key string) (T, error)

GetFromData gets metadata value of given type from given Data. Returns error if not present or item is a different type.

func Name added in v0.3.8

func Name(obj any) string

Name returns the "Name" standard key value (empty if not set).

func Set added in v0.3.8

func Set(obj any, key string, value any) error

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 SetDoc added in v0.3.8

func SetDoc(obj any, doc string)

SetDoc sets the "Doc" standard key.

func SetFile added in v0.3.8

func SetFile(obj any, file *os.File)

SetFile sets the "File" standard key for *os.File.

func SetFilename added in v0.3.8

func SetFilename(obj any, file string)

SetFilename sets the "Filename" standard key.

func SetName added in v0.3.8

func SetName(obj any, name string)

SetName sets the "Name" standard key.

Types

type Data

type Data map[string]any

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 Copy added in v0.3.8

func Copy(to, src any) *Data

Copy copies metadata from source Must pass a pointer to the object.

func GetData added in v0.3.8

func GetData(obj any) *Data

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.

func (*Data) Copy

func (md *Data) Copy(src Data)

Copy does a shallow copy of metadata from source. Any pointer-based values will still point to the same underlying data as the source, but the two maps remain distinct. It uses maps.Copy.

func (*Data) Set

func (md *Data) Set(key string, value any)

Set sets key to given value, ensuring that the map is created if not previously.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL