metadata

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2025 License: BSD-3-Clause Imports: 4 Imported by: 0

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

func Doc(obj any) string

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

func File

func File(obj any) *os.File

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

func Filename

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

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

func Name(obj any) string

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

func Set

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

func SetDoc(obj any, doc string)

SetDoc sets the "Doc" standard key.

func SetFile

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

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

func SetFilename

func SetFilename(obj any, file string)

SetFilename sets the "Filename" standard key.

func SetName

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

func Copy(to, src any) *Data

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

func GetData

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

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