connector

package
v0.6.8-rc.4 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2023 License: Apache-2.0 Imports: 1 Imported by: 0

README

connector-go

Build Status Doc Status Go Report Card

A Golang implementation of the Connector concept.

Examples:

Basic Code:

import "github.com/hofstadter-io/connector-go"

func main () {
    conn := connector.New("my-connector")
    f, b, m := foo{do:"goo"},boo{do:"be friendly"},moo{do:"farm to table"}
    conn.Add(f, []interface{}{b,m})

    for _, named := range conn.Named() {
        named.Name()
    }

    for _, item := range conn.Items() {
        doer, ok := item.(Doer)
        if ok {
            doer.Do()
        }
    }

    for _, item := range conn.Get((*Talker)(nil)) {
        talker := item.(Talker)
        talker.Say()
    }
}


type Doer interface {
    Do() string
}

type Talker interface {
    Say() string
}

type foo struct {
    do string
}

func (f *foo) Do() string {
    return f.do
}
func (f *foo) Name() string {
    return "foo"
}

type boo struct {
    do string
}

func (b *boo) Do() string {
    return b.do
}
func (b *boo) Name() string {
    return "Casper"
}
func (b *boo) Say() string {
    return "Boooooo"
}

type moo struct {
    do string
}

func (m *moo) Do() string {
    return m.do
}
func (m *moo) Name() string {
    return "Cow"
}
func (m *moo) Say() string {
    return "MoooOOO"
}

Documentation

Overview

Package connector ...

Implements the Connector concept in Golang.

import "github.com/hofstadter-io/connector-go"

func main () {
	conn := connector.New("my-connector")
	f, b, m := foo{do:"goo"},boo{do:"be friendly"},moo{do:"farm to table"}
	conn.Add(f, []interface{}{b,m})

	for _, named := range conn.Named() {
		named.Name()
	}

	for _, item := range conn.Items() {
		doer := item.(Doer)
		doer.Do()
	}

	typ := reflect.TypeOf((*Talker)(nil)).Elem()
	for _, item := range conn.Get(typ) {
		talker := item.(Talker)
		talker.Say()
	}
}

type Doer interface {
	Do() string
}

type Talker interface {
	Say() string
}

type foo struct {
	do string
}

func (f *foo) Do() string {
	return f.do
}
func (f *foo) Name() string {
	return "foo"
}

type boo struct {
	do string
}

func (b *boo) Do() string {
	return b.do
}
func (b *boo) Name() string {
	return "Casper"
}
func (b *boo) Say() string {
	return "Boooooo"
}

type moo struct {
	do string
}

func (m *moo) Do() string {
	return m.do
}
func (m *moo) Name() string {
	return "Cow"
}
func (m *moo) Say() string {
	return "MoooOOO"
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Addable

type Addable interface {
	Add(...interface{})
}

Addable - things which can be Add()-ed to

type Base

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

Base is the base implementation

func New

func New(name string, items ...interface{}) *Base

New creates a new Base (i.e. Connector) with the given name. Name must have a non-empty value. It should also be unique, but that is not enforced.

func (*Base) Add

func (B *Base) Add(in ...interface{})

Add adds items to a Connector. Input may be a single object, slice, or any mix.

func (*Base) Clear

func (B *Base) Clear()

Clear clears all items from this Connector. Will not effect connectors which this had been added to.

func (*Base) Connect

func (B *Base) Connect(c Connector)

Connect is the main usage function.

Connect() recursively passes a Connector object to all items so that they may consume or use any items in the Connector. Typically, we build up a root Connector and then call Connect with itself as the argument.

func (*Base) Del

func (B *Base) Del(out interface{})

Del deletes an item or list of items from the connector.

func (*Base) Get

func (B *Base) Get(in interface{}) []interface{}

Get will return items which implement a given type or interface.

Once a number of items have been added, we will want to retrieve some subset of those. The Get() Function will return all items that match the given type

func (*Base) Items

func (B *Base) Items() []interface{}

Items returns all Items

func (*Base) Name

func (B *Base) Name() string

Name returns the name of the Base (i.e. Connector)

func (*Base) Named

func (B *Base) Named() []Named

Named returns the name of the Base (i.e. Connector)

type Clearable

type Clearable interface {
	Clear()
}

Clearable - things which are Clear()-able

type Connectable

type Connectable interface {
	Connect(Connector)
}

Connectable - things which consume Connectors

type Connector

type Connector interface {
	// Connect() recursively passes a Connector object to all items
	// so that they may consume or use any items in the Connector.
	// Typically, we build up a root Connector and then
	// call Connect with itself as the argument.
	Connect(Connector)

	// Extracts all the items which match a given type. Go Get()'em !!
	Get(interface{}) []interface{}
}

Connector is all the things, put together.

type Deletable

type Deletable interface {
	Del(interface{})
}

Deletable - things which can be Del()-ed from

type Filterable

type Filterable interface {
	Filter(func(interface{}) bool) []interface{}
}

Filterable - things that are Filter(func(interface{})bool)-able

type Gettable

type Gettable interface {
	Get(interface{}) []interface{}
}

Gettable - things which can be Get()-ed from, by type

type Indexed

type Indexed interface {
	Lookup(name string) interface{}
}

Indexed - things that have something to Lookup(string)

type Itemizer

type Itemizer interface {
	Items() []interface{}
}

Itemizer - things which have Items()

type Named

type Named interface {
	Name() string
}

Named - things which have a Name()

type Namer

type Namer interface {
	Named() []Named
}

Namer - things which hold things which have a Name()

type Searchable

type Searchable interface {
	Find(interface{})
}

Searchable - things that have something to Find(interface)

type Stored

type Stored interface {
	Addable
	Gettable
	Deletable
	Clearable
}

Stored - hings that hold other things [Add(),Get(),Del(),Clear()]

Jump to

Keyboard shortcuts

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