ita

package module
v0.0.0-...-00d04c1 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2016 License: MIT Imports: 2 Imported by: 5

README

ita GoDocCoverage StatusBuild Status

ita is a Go(Golang) library that provides a clean API for dynamically calling structs methods.

The name ita is a swahili word for call.

Motivation

I needed a clean API for calling struct methods, that will need to avoid unnecessary panics. I identified some of the the panics I was to avoid, and return errors instead.

  • Call with too few input arguments. This happens when the method arguments are more than what are provided to the method.

  • Call with too many input arguments. This happens when the method arguments are less than the values passed to the method

  • using zero Value argument. This happens when one of the arguments has zero value or Invalid.

  • call of nil function. This happens when the method is nil

So, for my use case I had no reason to panic on any of those scenarions. Instead I wanted to have an error raised so I can make other choices rather than recover from the panic( Did I say I almost fainted after a good dose of panics!)

So, Ita works with structs methods only. And its my intetion to try to give meaningful error messages rather than panicking.

But be warned, IT IS YOUR JOB TO CHECK FOR ERRORS

The latest error encountered is always available via the Error() method of Struct instance.

features

  • call methods by name
  • easy access to results
  • support varidaic functions
  • reduced unnecessary panics, returns errors instead( Please see Motivation section)
  • Method chaining (Coming soon)

Installation

go get github.com/gernest/ita

How to use

package main

import (
	"fmt"

	"github.com/gernest/ita"
)

type Hello struct{}

func (h *Hello) Say() string {
	return "hello"
}

func (h *Hello) Who(name string) string {
	return name
}
func (h *Hello) Double(word string) (string, string) {
	return word, word
}

func main() {
	hell := ita.New(&Hello{})

	// call Who
	who := hell.Call("Who", "gernest")
	
	// If you want to check errors
	if who.Error()!=nil{
		// do something
	}

	// call Say
	say := hell.Call("Say")

	// call double
	double := hell.Call("Double", "!")

	// get the returned results for calling Who
	name, _ := who.GetResults().First()

	// get the result returned by Say
	message, _ := say.GetResults().First()

	// get results returned by double
	doubleResult := double.GetResults()

	firstDouble, _ := doubleResult.First()
	lastDouble, _ := doubleResult.Last()

	fmt.Printf("%s %s %s%s", message, name, firstDouble, lastDouble)

}

You can see tests for more examples.

Contributing

Start with clicking the star button to make the author and his neighbors happy. Then fork it and submit a pull request for whatever change you want to be added to this project.

Or Open an issue for any questions.

Author

twitter : @gernesti

Roadmap

  • Add support for assiging results to values provided by the user.

Licence

This project is released under MIT licence see LICENCE for more details.

Documentation

Overview

Package ita provides a clean API for dynamically calling structs methods

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	//ErrMethodNotFound is returned when there is no method found
	ErrMethodNotFound = errors.New("ita: method not found")

	//ErrZeroValue is returend when the vales is not valid
	ErrZeroValue = errors.New("ita: zero value argument")

	//ErrTooManyArgs is returned when arguments required are more than those passed to the method
	ErrTooManyArgs = errors.New("ita: the method has more arguments than the ones provided")

	//ErrTooFewArgs is returned when arguments required are less than those supplied to the method
	ErrTooFewArgs = errors.New("ita: you have provided more arguments than required by the method")
)

Functions

This section is empty.

Types

type Result

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

Result stores the returned values from dynamically callling struct methods

func (*Result) First

func (r *Result) First() (interface{}, bool)

First returns the first result returned by caling a method

For instance if you have method Foo

func (s *MyStruct)Foo()( a string, err error){...}

First will return a but in interface form, meaning you will have to manualy cast the result to string. For automatic casting, please see Map, and MapTo

Example
var rst []reflect.Value
out := []int{1, 2, 3}
for _, o := range out {
	rst = append(rst, reflect.ValueOf(o))
}
result := &Result{}
result.Set(rst)

fmt.Println(result.First())
Output:

1 true

func (*Result) Get

func (r *Result) Get(index int) (val interface{}, ok bool)

Get retrieves the result by Index, It will not panic if the index is out of range. instead it returns a nil value and ok is set to false.

Example
var rst []reflect.Value
out := []int{1, 2, 3}
for _, o := range out {
	rst = append(rst, reflect.ValueOf(o))
}
result := &Result{}
result.Set(rst)

fmt.Println(result.Get(0))
fmt.Println(result.Get(5))
Output:

1 true
<nil> false

func (*Result) HasIndex

func (r *Result) HasIndex(index int) bool

HasIndex returns true if the result has the given index.

func (*Result) IsNil

func (r *Result) IsNil() bool

IsNil checks if there is no result, or the result was nil

func (*Result) Last

func (r *Result) Last() (interface{}, bool)

Last returns the last value returned by calling a method.

func (*Result) Len

func (r *Result) Len() int

Len returns the number of results. If a method returns tow values then Len() will return 2.

func (*Result) Set

func (r *Result) Set(rst []reflect.Value)

Set stores rst in the Result object

type Struct

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

Struct wraps the struct that we want to call its methods

Example
package main

import (
	"fmt"
)

type Hello struct{}

func (h *Hello) Say() string {
	return "hello"
}

func (h *Hello) Who(say string) string {
	return say
}

func main() {
	hell := New(&Hello{})

	who := hell.Call("Who", "gernest")

	say := hell.Call("Say")

	first, _ := say.GetResults().First()
	second, _ := who.GetResults().First()
	fmt.Println(first, second)

}
Output:

hello gernest

func New

func New(v interface{}) *Struct

New wraps v into a Struct and returns the struct ready for plumbing.

func (*Struct) Call

func (s *Struct) Call(method string, a ...interface{}) *Struct

Call calls method by nameand passing any arguments to the unserlying struct. This supports variadic methods and also methods that requires no arguments.

func (*Struct) Error

func (s *Struct) Error() error

Error returns the most recent error encountered while calling Call(). Be wise to remember to check this just incase to be sure errors are under control.

func (*Struct) GetResults

func (s *Struct) GetResults() *Result

GetResults returns the results of the last Call.

Jump to

Keyboard shortcuts

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