complete

package module
v0.0.0-...-17e6c0e Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2017 License: GPL-3.0 Imports: 1 Imported by: 26

README

Complete

Complete is a general purpose completion handler system that is mobile compatible with gomobile. Clients can follow the Completionable interface to write their own completion handlers in their native language which will be executed upon completion in Go.

Complete also provides a generic Result struct that can hold any kind of Go data structure, even if it is not supported by the native language. It's up to the Go library or program to provide type assertion methods to convert the generic Result object into its actual type.

Usage

iOS

Because functions cannot be passed directly to go, each target language must implement their own completion handler class that implements the complete.Completionable interface/protocol.

Here is an example completion handler in Swift:

class CompletionHandler: GoCompleteCompletionableProtocol {
    
    var success: ((result: GoCompleteResult!) -> Void)!
    var failure: ((msg: String!, error: NSError!) -> Void)!

    @objc func onSuccess(p0: GoCompleteResult!) {
        self.success(result: p0)
    }
    
    @objc func onFailure(p0: String!, p1: NSError!) {
        self.failure(msg: p0, error: p1)
    }
}

func onSuccess(success: ((result: GoCompleteResult!) -> Void)!, failure: ((msg: String!, error: NSError!) -> Void)!) -> CompletionHandler {
    let handler = CompletionHandler()
    handler.success = success
    handler.failure = failure
    return handler
}
Login example

Here is an example in Swift of logging inwith a completion handler:

        // Login
        Login("picard", "123456", false, onSuccess({ (result) in
            print("Logged in!")
            }, failure: { (msg, error) in
                print("Failed to log in: \(msg)")
        }))
Android

Here is an example completion handler in Java. There may be better way to implement your completion handler:

public class CompletionHandler implements Handlers.Completionable {

    private Success success;
    private Failure failure;

    public void OnSuccess(Result result) {
        this.success.call(result);
    }
    public void OnFailure(String error) {
        this.failure.call(error);
    }

    public static CompletionHandler onSuccess(Success success, Failure failure) {
        CompletionHandler handler = new CompletionHandler();
        handler.success = success;
        handler.failure = failure;

        return handler;
    }
}

// Used for calling the success handler.
interface Success {
    void call(Result result);
}

// Used for calling the failure handler.
interface Failure {
    void call(String error);
}
Login example

Here is an example in Java of logging in with a completion handler:

// Login
Login("picard", "123456", false,
    CompletionHandler.onSuccess(
        new Success() {
            @Override
            public void call(Complete.Result result) {
                System.out.println("Logged in!");
            }
        },
        new Failure() {
            @Override
            public void call(String error) {
                System.out.println(error);
            }
        }
    )
);

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompletionHandler

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

CompletionHandler is a Go-compatible completion handler that implements Completionable.

func NewCompletion

func NewCompletion(success successHandler, failure failureHandler) *CompletionHandler

NewCompletion returns an empty completion handler. AddHandler is required for these handlers.

func (*CompletionHandler) AddHandlers

func (c *CompletionHandler) AddHandlers(success successHandler, failure failureHandler)

AddHandlers adds success/failure handlers to a completion handler

func (*CompletionHandler) OnFailure

func (c *CompletionHandler) OnFailure(msg string)

OnFailure is called when a task fails.

func (*CompletionHandler) OnSuccess

func (c *CompletionHandler) OnSuccess(r *Result)

OnSuccess is called when a task succeeds.

type Completionable

type Completionable interface {
	OnSuccess(*Result)
	OnFailure(string)
}

Completionable interface is used to implement your own mobile-compatible completion handlers.

type Iteratable

type Iteratable interface {
	Next() bool
	Value() Result
}

Iteratable is used to iterate through a list of Results.

type Result

type Result struct {
	DataType string
	Data     interface{}
}

Result is a generic struct used with completion handlers.

func GetResults

func GetResults(results ResultArray) []Result

GetResults is a Go-compatible way to get an array/slice of Result objects.

func NewResult

func NewResult(data interface{}) Result

NewResult returns a new result struct.

type ResultArray

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

ResultArray implements iteratable to allow mobile-compatible array passing.

func NewResultIterator

func NewResultIterator(data []Result) *ResultArray

NewResultIterator returns a new iterator for Result arrays.

func (*ResultArray) Next

func (d *ResultArray) Next() bool

Next returns true is there is additional results in the array.

func (*ResultArray) Value

func (d *ResultArray) Value() Result

Value gets the current value of the iterator

Jump to

Keyboard shortcuts

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