specification

package
v0.0.0-...-416ecfb Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: MIT Imports: 1 Imported by: 0

README

Specification pattern

This package provides a simple implementation of the specification pattern.

The specification pattern is a software design pattern that allows us to define a business rule that can be applied to a set of objects.

The pattern is useful when we need to filter a collection of objects based on a set of rules.

How to use

You can see an example by link.

# Example print:

$> User Alice satisfies the specification
$> User Bob does not satisfy the specification: Specification failed: User Bob's name does not start with 'A'
$> User Charlie does not satisfy the specification: Specification failed: User Charlie's name does not start with 'A'
$> Filtered users: [0xc0001080c0]

References

[!TIP]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter[T any](list []*T, spec Specification[T]) ([]*T, error)

Filter returns a new slice containing only the elements that satisfy the given specification.

Types

type AndSpecification

type AndSpecification[T any] struct {
	// contains filtered or unexported fields
}

AndSpecification is a composite specification that represents the logical AND of two other specifications.

func NewAndSpecification

func NewAndSpecification[T any](specs ...Specification[T]) *AndSpecification[T]

func (*AndSpecification[T]) IsSatisfiedBy

func (a *AndSpecification[T]) IsSatisfiedBy(account *T) error

type NotSpecification

type NotSpecification[T any] struct {
	// contains filtered or unexported fields
}

NotSpecification is a composite specification that represents the logical NOT of another specification.

func NewNotSpecification

func NewNotSpecification[T any](spec Specification[T]) *NotSpecification[T]

func (*NotSpecification[T]) IsSatisfiedBy

func (n *NotSpecification[T]) IsSatisfiedBy(account *T) error

type OrSpecification

type OrSpecification[T any] struct {
	// contains filtered or unexported fields
}

OrSpecification is a composite specification that represents the logical OR of two other specifications.

func NewOrSpecification

func NewOrSpecification[T any](specs ...Specification[T]) *OrSpecification[T]

func (*OrSpecification[T]) IsSatisfiedBy

func (o *OrSpecification[T]) IsSatisfiedBy(account *T) error

type Specification

type Specification[T any] interface {
	IsSatisfiedBy(*T) error
}

Specification is the interface that wraps the IsSatisfiedBy method.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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