argjoy

package module
v0.0.0-...-1ffcd6b Latest Latest
Warning

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

Go to latest
Published: May 7, 2017 License: MIT Imports: 4 Imported by: 10

README

argjoy

Build Status GoDoc

Golang module allowing you to call a method using callbacks to translate arguments. Also allows optional arguments.

Extremely basic example:

package main

import (
    "fmt"
    "github.com/lunixbochs/argjoy"
)

// optC is just a variable name. The opt prefix is not required.
func test(a, b, optC int) int {
    return a + b + optC
}

func main() {
    aj := argjoy.NewArgjoy(argjoy.StrToInt)
    // Enables optional arguments, where unpassed arguments are zeroed.
    aj.Optional = true

    // The following is effectively: out := test(1, 2, 0)
    out, err := aj.Call(test, "1", "2")
    if err != nil {
        panic(err)
    }
    // out is []interface{} so you need to do a type assert
    fmt.Println(out[0].(int))
}

Custom argument decoder example (use argjoy.StrToInt for a more robust version of this):

func strToInt(arg interface{}, vals []interface{}) (err error) {
    if v, ok := val[0].(string); ok {
        if a, ok := arg.(*int); ok {
            *a, err = strconv.Atoi(v)
            return
        }
    }
    return argjoy.NoMatch
}

Why?

Reduces duplicate decoding logic. Repeated decoding like v, err := strconv.Atoi(); if err != nil is completely eliminated.

Type-safe command-line parsing. Flag, Cobra, etc only reduce to a list of strings, so the rest of the decoding was up to you.

Syscall argument decoding via metaprogramming (used in Usercorn).

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoMatch = &NoMatchErr{}

Functions

func IntToInt

func IntToInt(arg interface{}, vals []interface{}) (err error)

Codec to convert between any builtin int types. Throws bounds and sign errors as appropriate.

func RadStrToInt

func RadStrToInt(arg interface{}, vals []interface{}) error

Codec to convert radix notations like hex, octal, binary to int. Throws bounds and sign errors as appropriate.

func StrToInt

func StrToInt(arg interface{}, vals []interface{}) error

Codec to convert a string to any int type (base 10 forced). Throws bounds and sign errors as appropriate.

Types

type ArgCountErr

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

func (ArgCountErr) Error

func (e ArgCountErr) Error() string

type Argjoy

type Argjoy struct {
	Codecs []CodecFunc

	// Determines whether unpassed arguments are optional.
	// If this is false, invoking Call() with insufficient number of arguments
	// will return ArgCountErr
	Optional bool
}

func NewArgjoy

func NewArgjoy(codecs ...CodecFunc) *Argjoy

Any codecs passed to NewArgjoy will be passed to Register() on the new instance.

func (*Argjoy) Call

func (a *Argjoy) Call(fn interface{}, vals ...interface{}) ([]interface{}, error)

Call fn(vals...), using registered codecs to convert argument types. Returns []interface{} of target function's return values. Will return an error value if any codec fails. Panics if fn is not a valid function.

func (*Argjoy) Convert

func (a *Argjoy) Convert(in []reflect.Type, variadic bool, vals ...interface{}) ([]reflect.Value, error)

Given the arguments to a function as per reflect.In(), produce a list of arguments suitable for invoking reflect.Call(fn, args). If fn is variadic, you must instead use reflect.CallSlice(fn, args).

func (*Argjoy) Register

func (a *Argjoy) Register(codec CodecFunc) error

Registers a new codec function which will be used to convert arguments during Call()

type CodecFunc

type CodecFunc func(arg interface{}, vals []interface{}) error

type NoMatchErr

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

func (NoMatchErr) Error

func (e NoMatchErr) Error() string

Jump to

Keyboard shortcuts

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