pkgsyms

package module
v0.0.0-...-ccea36f Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2020 License: MIT Imports: 4 Imported by: 0

README

pkgsyms

Extension to the reflect package with an API similar to the plugin package. It lets you access packages and their exported constants, functions, variables and types by name.

Documentation

Overview

Package pkgsyms extends the reflect package so that any symbols exported by a package are accessible by name to another package.

Note that the inability to get a type or global function by its name is not an oversight of the reflect package- it's a strength that allows the compiler and runtime to omit functions, and possibly types, that aren't referenced from anywhere during compilation.

In order for the pkgsyms package to work, users of the pkgsyms package must run a go generate command that essentially subverts the compiler's ability to eliminate dead code by making all exported names accessible outside of the package.

Index

Constants

This section is empty.

Variables

View Source
var Pkg = Of("github.com/skillian/pkgsyms")

Functions

This section is empty.

Types

type Const

type Const struct {
	// contains filtered or unexported fields
}

Const holds the value of a constant. Unlike Go compile-time constants, because we're actually holding onto values at runtime, these "constants" have actual types.

func MakeConst

func MakeConst(name string, value interface{}) Const

MakeConst creates a Const Symbol.

func (Const) Get

func (c Const) Get() interface{}

Get the value of the constant.

func (Const) Name

func (c Const) Name() string

Name of the Constant

type Func

type Func struct {
	// contains filtered or unexported fields
}

Func is a global function Symbol.

func MakeFunc

func MakeFunc(name string, fval interface{}) Func

MakeFunc creates a Func Symbol.

func (Func) Get

func (f Func) Get() interface{}

Get the function value

func (Func) Name

func (f Func) Name() string

Name of the function

type NotFound

type NotFound struct {
	Pkg string
	Sym string
}

NotFound is returned when a symbol is not found in a package.

func (NotFound) Error

func (nf NotFound) Error() string

type Package

type Package struct {
	// Name of the package.  Do not mutate this string.
	Name string

	// Symbols exported by the package
	Symbols
}

Package defines a package. It includes the package name and its exported symbols.

func Lookup

func Lookup(name string) (*Package, error)

Lookup a package by its name.

func Of

func Of(name string) *Package

Of gets the Package definition of the package with the given name.

type Symbol

type Symbol interface {
	// Name is the exported name of the symbol
	Name() string

	// Get the value associated with the symbol.
	Get() interface{}
}

Symbol is an exported constant, function, type or variable.

Symbols have names and values. What you get by calling their Get function depends on the Symbol implementation. Unlike the plugin package which leaves its Symbol definitions directly available to type assertions, you should instead try to type-assert against the result of the Get function.

type Symbols

type Symbols struct {
	// contains filtered or unexported fields
}

Symbols are exported names in a package which can include things like constants, functions, types and variables.

func MakeSymbols

func MakeSymbols(capacity int) Symbols

MakeSymbols creates a collection of symbols

func (*Symbols) Add

func (syms *Symbols) Add(ss ...Symbol)

Add zero or more symbols to the set. Symbols are only added if they haven't already been defined.

func (*Symbols) Lookup

func (syms *Symbols) Lookup(name string) (Symbol, error)

Lookup a symbol in the set. This function is meant to resemble the plugin package's Lookup function.

type Type

type Type struct {
	// contains filtered or unexported fields
}

Type holds a reflect.Type defined in the package.

func MakeType

func MakeType(name string, pval interface{}) Type

MakeType creates a Type from a pointer to a value of the proper type. For example:

MakeType("MyInterface", (*MyInterface)(nil))

creates a Type that references the unwrapped MyInterface and not a pointer to MyInterface. The pointer is necessary because of how interfaces work in Go.

func (Type) Get

func (t Type) Get() interface{}

Get the reflect.Type wrapped by this type.

func (Type) Name

func (t Type) Name() string

Name of the type

func (Type) Type

func (t Type) Type() reflect.Type

Type is like Get, but keeps it as a reflect.Type.

type Var

type Var struct {
	// contains filtered or unexported fields
}

Var is a Symbol that wraps a variable.

func MakeVar

func MakeVar(name string, addr interface{}) Var

MakeVar creates a variable symbol

func (Var) Get

func (v Var) Get() interface{}

Get the value of the variable

func (Var) Name

func (v Var) Name() string

Name of the variable

func (Var) Set

func (v Var) Set(val interface{})

Set the value of the variable.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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