fapi

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2020 License: Zlib Imports: 7 Imported by: 0

README

xirho/fapi

Package fapi creates a generic public API for xirho function types.

Fapi generates an abstracted set and get layer over function parameters of Flag, List, Int, Angle, Real, Complex, Vec3, Affine, Func, and FuncList types from package xirho. There is a corresponding type in fapi for each, meaning that type switches can enumerate every possibility to use the complete API of any xirho function.

Typical use of package fapi will look something like this:

api := fapi.For(someFunc)
for _, param := range api {
    switch p := param.(type) {
    case fapi.Flag:
        // p.Get(), p.Set(), p.Name()
    case fapi.List:
        // p.Get(), p.Set(), p.Name(), p.String(), p.Opts()
    // ...
    default:
        panic("unknown parameter type")
    }
}

For a more complete example, see xirho/encoding.Unmarshal.

Documentation

Overview

Package fapi creates a generic public API for xirho function types.

The For function uses reflection to gather a list of modifiable parameters. This can be used to implement serialization formats or to provide user interfaces for functions. Parameter types are based on semantics rather than on representation to allow for more natural user interfaces.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Affine

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

Affine is an affine transform function parameter.

func (Affine) Get

func (p Affine) Get() xirho.Affine

Get gets the affine transform value.

func (Affine) Name

func (p Affine) Name() string

Name returns the parameter name.

func (Affine) Set

func (p Affine) Set(v xirho.Affine) error

Set sets the affine transform value.

type Angle

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

Angle is an angle function parameter. External interfaces wrap its value into the interval (-pi, pi].

func (Angle) Get

func (p Angle) Get() xirho.Angle

Get gets the angle value.

func (Angle) Name

func (p Angle) Name() string

Name returns the parameter name.

func (Angle) Set

func (p Angle) Set(v xirho.Angle) error

Set sets the angle value wrapped into the interval (-pi, pi].

type Complex

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

Complex is an unconstrained function parameter in R^2.

func (Complex) Get

func (p Complex) Get() xirho.Complex

Get gets the complex value.

func (Complex) Name

func (p Complex) Name() string

Name returns the parameter name.

func (Complex) Set

func (p Complex) Set(v xirho.Complex) error

Set sets the complex value.

type Flag

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

Flag is a boolean function parameter.

func (Flag) Get

func (p Flag) Get() xirho.Flag

Get gets the current parameter value.

func (Flag) Name

func (p Flag) Name() string

Name returns the parameter name.

func (Flag) Set

func (p Flag) Set(v xirho.Flag) error

Set sets the parameter value. The returned error is always nil.

type Func

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

Func is a function parameter that is itself a function. After the parameter name, a Func field may include an additional comma-separated tag containing the string "optional". Func fields marked optional may be set to nil. For example:

type Example struct {
	NillableFunc `xirho:"func,optional"`
}

func (Func) Get

func (p Func) Get() xirho.F

Get gets the function value.

func (Func) IsOptional

func (p Func) IsOptional() bool

IsOptional returns whether the function may be set to nil.

func (Func) Name

func (p Func) Name() string

Name returns the parameter name.

func (Func) Set

func (p Func) Set(v xirho.F) error

Set sets the function value. If the parameter is not optional and v.F is nil, an error of type NotOptional is returned instead.

type FuncList

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

FuncList is a function parameter holding a list of functions.

func (FuncList) Append

func (p FuncList) Append(v ...xirho.F)

Append appends functions to the list.

func (FuncList) Get

func (p FuncList) Get() xirho.FuncList

Get returns a copy of the function list value.

func (FuncList) Name

func (p FuncList) Name() string

Name returns the parameter name.

func (FuncList) Set

func (p FuncList) Set(v xirho.FuncList) error

Set sets the function list value.

type Int

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

Int is an integer function parameter. After the parameter name, an Int field may include two additional comma-separated tags to define the lowest and highest permitted values. For example, to define a parameter allowing the user to choose any integer in [-3, 12], do:

type Example struct {
	P xirho.Int `xirho:"p,-3,12"`
}

func (Int) Bounded

func (p Int) Bounded() bool

Bounded returns whether the parameter is bounded.

func (Int) Bounds

func (p Int) Bounds() (lo, hi xirho.Int)

Bounds returns the parameter bounds. If the parameter is not bounded, the returned bounds are the minimum and maximum values of int64.

func (Int) Get

func (p Int) Get() xirho.Int

Get gets the int value.

func (Int) Name

func (p Int) Name() string

Name returns the parameter name.

func (Int) Set

func (p Int) Set(v xirho.Int) error

Set sets the int value. If the Int is bounded and v is out of its bounds, an error of type OutOfBoundsInt is returned instead.

type List

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

List is a function parameter to choose among a list of strings. After the parameter name, a List field may include any number of additional comma-separated tags to define the display names of each option. For example, to define a parameter allowing the user to choose between "fast", "accurate", or "balanced", do:

type Example struct {
	Method xirho.List `xirho:"method,fast,accurate,balanced"`
}

func (List) Get

func (p List) Get() xirho.List

Get gets the list integer value.

func (List) Name

func (p List) Name() string

Name returns the parameter name.

func (List) Opts

func (p List) Opts() []string

Opts returns a copy of the list's options.

func (List) Set

func (p List) Set(v xirho.List) error

Set sets the list value. If v is less than zero or larger than the number of available options, an error of type OutOfBoundsInt is returned instead.

func (List) String

func (p List) String() string

String gets the list's selected string.

type NotFinite

type NotFinite struct {
	// Param is the parameter which the caller attempted to set.
	Param Param
}

NotFinite is an error returned when attempting to set an Angle, Real, Complex, Vec3, or Affine parameter to a value with a component that is not finite.

func (NotFinite) Error

func (err NotFinite) Error() string

type NotOptional

type NotOptional struct {
	// Param is the parameter which the caller attempted to set.
	Param Param
}

NotOptional is an error returned when attempting to set a Func to nil when the Func is not marked as optional.

func (NotOptional) Error

func (err NotOptional) Error() string

Error returns a formatted error message.

type OutOfBoundsInt

type OutOfBoundsInt struct {
	// Param is the parameter which the caller attempted to set.
	Param Param
	// Value is the value which the caller attempted to use.
	Value int64
	// Lo and Hi are the minimum and maximum allowed values, inclusive.
	Lo, Hi int64
}

OutOfBoundsInt is an error returned when attempting to set an Int or List to a value which is out of the parameter's defined limits.

func (OutOfBoundsInt) Error

func (err OutOfBoundsInt) Error() string

Error returns a formatted error message.

type OutOfBoundsReal

type OutOfBoundsReal struct {
	// Param is the parameter which the caller attempted to set.
	Param Param
	// Value is the value which the caller attempted to use.
	Value float64
	// Lo and Hi are the minimum and maximum allowed values, inclusive.
	Lo, Hi float64
}

OutOfBoundsReal is an error returned when attempting to set a Real to a value which is out of the parameter's defined limits.

func (OutOfBoundsReal) Error

func (err OutOfBoundsReal) Error() string

Error returns a formatted error message.

type Param

type Param interface {
	// Name returns the parameter name.
	Name() string
	// contains filtered or unexported methods
}

Param is a user-controlled function parameter. Each implementing type has a setter and a getter for a corresponding xirho.Param type.

func For

func For(f xirho.F) []Param

For collects a xirho.F's exported parameters. Each returned parameter has its name set according to the first comma-separated section of the field's "xirho" struct tag, defaulting to the field name. E.g., the JuliaN variation is defined as such:

type JuliaN struct {
	Power xirho.Int  `xirho:"power"`
	Dist  xirho.Real `xirho:"dist"`
}

Certain parameters provide additional options; see the documentation for each for details.

type Real

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

Real is a floating-point function parameter. After the parameter name, a Real field may include two additional comma-separated tags to define the lowest and highest permitted values. For example, to define a parameter allowing the user to choose any real in [-2π, 2π], do:

type Example struct {
	P xirho.Real `xirho:"p,-6.283185307179586,6.283185307179586"`
}

func (Real) Bounded

func (p Real) Bounded() bool

Bounded returns whether the parameter is bounded.

func (Real) Bounds

func (p Real) Bounds() (lo, hi xirho.Real)

Bounds returns the parameter bounds. If the parameter is not bounded, the returned bounds are -inf and +inf.

func (Real) Get

func (p Real) Get() xirho.Real

Get gets the real value.

func (Real) Name

func (p Real) Name() string

Name returns the parameter name.

func (Real) Set

func (p Real) Set(v xirho.Real) error

Set sets the real value. If the Real is bounded and v is out of its bounds, an error of type OutOfBoundsReal is returned instead.

type Vec3

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

Vec3 is an unconstrained function parameter in R^3.

func (Vec3) Get

func (p Vec3) Get() xirho.Vec3

Get gets the vector value.

func (Vec3) Name

func (p Vec3) Name() string

Name returns the parameter name.

func (Vec3) Set

func (p Vec3) Set(v xirho.Vec3) error

Set sets the vector value.

Jump to

Keyboard shortcuts

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