goconsume

package module
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2021 License: BSD-3-Clause Imports: 2 Imported by: 24

README

goconsume

This repo is now deprecated in favor of github.com/keep94/consume

Documentation

Overview

This package is now deprecated in favor of github.com/keep94/consume.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustCanConsume

func MustCanConsume(c Consumer)

MustCanConsume panics if c cannot consume

Types

type Applier added in v1.1.0

type Applier interface {

	// Apply applies the chained filter and map functions to what ptr points
	// to while leaving it unchanged. Apply returns nil if ptr should be
	// filtered out; returns ptr itself; or returns a pointer to a new value.
	// If Apply returns a pointer to a new value, the new value gets
	// overwritten with each call to Apply.
	Apply(ptr interface{}) interface{}
	// contains filtered or unexported methods
}

Interface Applier represents zero or more functions like the ones passed to MapFilter chained together.

func NewApplier added in v1.1.0

func NewApplier(funcs ...interface{}) Applier

NewApplier creates an Applier from multiple functions like the ones passed to MapFilter chained together. The returned Applier can be passed as a parameter to MapFilter or to NewApplier.

type ConsumeFinalizer

type ConsumeFinalizer interface {
	Consumer

	// Caller calls Finalize after it is done passing values to this consumer.
	// Once caller calls Finalize(), CanConsume() returns false and Consume()
	// panics.
	Finalize()
}

ConsumeFinalizer adds a Finalize method to Consumer.

func Page

func Page(
	zeroBasedPageNo int,
	itemsPerPage int,
	aValueSlicePointer interface{},
	morePages *bool) ConsumeFinalizer

Page returns a consumer that does pagination. The items in page fetched get stored in the slice pointed to by aValueSlicePointer. If there are more pages after page fetched, Page sets morePages to true; otherwise, it sets morePages to false. Note that the values stored at aValueSlicePointer and morePages are undefined until caller calls Finalize() on returned ConsumeFinalizer.

type Consumer

type Consumer interface {

	// CanConsume returns true if this instance can consume a value.
	// Once CanConsume returns false, it should always return false.
	CanConsume() bool

	// Consume consumes the value that ptr points to. Consume panics if
	// CanConsume() returns false.
	Consume(ptr interface{})
}

Consumer consumes values. The values that Consumer consumes must support assignment.

func AppendPtrsTo

func AppendPtrsTo(aPointerSlicePointer interface{}) Consumer

AppendPtrsTo returns a Consumer that appends consumed values to the slice pointed to by aPointerSlicePointer. Each time the returned Consumer consumes a value, it allocates a new value on the heap, copies the consumed value to that allocated value, and finally appends the pointer to the newly allocated value to the slice pointed to by aPointerSlicePointer. aPointerSlicePointer is a pointer to a slice of pointers to values supporting assignment. The CanConsume method of returned consumer always returns true.

func AppendTo

func AppendTo(aValueSlicePointer interface{}) Consumer

AppendTo returns a Consumer that appends consumed values to the slice pointed to by aValueSlicePointer. aValueSlicePointer is a pointer to a slice of values supporting assignment. The CanConsume method of returned consumer always returns true.

func Compose

func Compose(consumers ...Consumer) Consumer

Compose returns the consumers passed to it as a single Consumer. When returned consumer consumes a value, each consumer passed in that is able to consume a value consumes that value. CanConsume() of returned consumer returns false when the CanConsume() method of each consumer passed in returns false.

func ComposeWithCopy deprecated

func ComposeWithCopy(consumers []Consumer, valuePtr interface{}) Consumer

ComposeWithCopy is like Compose except that ComposeWithCopy passes a separate copy using assignment of the value being consumed to each of the consumers. valuePtr is a pointer to the type of value being consumed. Callers generally pass nil for it like this: (*TypeBeingConsumed)(nil).

Deprecated: Use MapFilter.

func Copy deprecated

func Copy(consumer Consumer, valuePtr interface{}) Consumer

Copy returns a consumer that copies the value it consumes using assignment and passes that copy onto consumer. valuePtr is a pointer to the type of value being consumed. Callers generally pass nil for it like this: (*TypeBeingConsumed)(nil).

Deprecated: Use MapFilter.

func Filter deprecated

func Filter(consumer Consumer, filter FilterFunc) Consumer

Filter returns a Consumer that passes only filtered values onto consumer. If filter returns false for a value, the returned Consumer ignores that value. The CanConsume() method of returned Consumer returns true if and only if the CanConsume() method of consumer returns true.

Deprecated: Use MapFilter.

func Map deprecated

func Map(consumer Consumer, mapfunc MapFunc, valuePtr interface{}) Consumer

Map returns a Consumer that passes only mapped values onto the consumer parameter. The pointer passsed to returned Consumer gets passed as srcPtr to mapfunc. The destPtr parameter of mapfunc gets passed to the Consume method of the consumer parameter. If mapfunc returns false, the returned Consumer ignores the parameter passed to it and will not call the Consume method of the consumer parameter. valuePtr is pointer to the type of value that the consumer parameter consumes. It is used to create the value that destPtr passed to mapfunc points to. Callers generally pass nil for it like this: (*TypeBeingConsumed)(nil). CanConsume() of returned consumer returns true if and only if the CanConsume() method of the consumer parameter returns true.

Deprecated: Use MapFilter.

func MapFilter added in v1.1.0

func MapFilter(consumer Consumer, funcs ...interface{}) Consumer

MapFilter returns a Consumer that passes only filtered and mapped values onto the consumer parameter. The returned Consumer applies each function in funcs to the value passed to its Consume method. The resulting value is then passed to the Consume method of the consumer parameter. Each function in func returns a bool and takes one or two pointer arguments to values. If a function returns false, it means that the value passed to it should not be consumed. The one argument functions never change the value passed to it as they are simple filters. The 2 argument functions are mappers. They leave their first argument unchanged, but use it to set their second argument. This second argument is what gets passed to the next function in funcs or to consumer if it is the last function in funcs.

The NewApplier function can return an Applier which represents zero or more of these functions chained together. Applier instances can be passed as parameters to MapFilter just like the functions mentioned above.

Example
package main

import (
	"fmt"
	"strconv"

	"github.com/keep94/goconsume"
)

func main() {
	var evens []string
	consumer := goconsume.MapFilter(
		goconsume.AppendTo(&evens),
		func(ptr *int) bool {
			return (*ptr)%2 == 0
		},
		func(src *int, dest *string) bool {
			*dest = strconv.Itoa(*src)
			return true
		},
	)
	ints := []int{1, 2, 4}
	for _, i := range ints {
		if consumer.CanConsume() {
			consumer.Consume(&i)
		}
	}
	fmt.Println(evens)
}
Output:

[2 4]

func Nil

func Nil() Consumer

Nil returns a consumer that consumes nothing. Calling CanConsume() on returned consumer returns false, and calling Consume() on returned consumer panics.

func Slice

func Slice(consumer Consumer, start, end int) Consumer

Slice returns a Consumer that passes the start th value consumed inclusive to the end th value consumed exclusive onto consumer where start and end are zero based. The returned consumer ignores the first start values it consumes. After that it passes the values it consumes onto consumer until it has consumed end values. The CanConsume() method of returned consumer returns false if the CanConsume() method of the underlying consumer returns false or if the returned consumer has consumed end values. Note that if end <= start, the underlying consumer will never get any values.

type ConsumerFunc

type ConsumerFunc func(ptr interface{})

ConsumerFunc can always consume.

func (ConsumerFunc) CanConsume

func (c ConsumerFunc) CanConsume() bool

CanConsume always returns true.

func (ConsumerFunc) Consume

func (c ConsumerFunc) Consume(ptr interface{})

Consume invokes c, this function.

type FilterFunc deprecated

type FilterFunc func(ptr interface{}) bool

FilterFunc filters values. It returns true if the value should be included and false if value should be excluded. ptr points to the value to be filtered.

Deprecated: Use Applier.

func All deprecated

func All(filters ...FilterFunc) FilterFunc

All returns a FilterFunc that returns true for a given value if and only if all the filters passed to All return true for that same value.

Deprecated: Use NewApplier.

type MapFunc deprecated

type MapFunc func(srcPtr, destPtr interface{}) bool

MapFunc maps values. It applies a transformation to srcPtr and stores the result in destPtr while leaving srcPtr unchanged. It returns true on success or false if no transformation took place.

Deprecated: Use MapFilter.

Jump to

Keyboard shortcuts

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