specification

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2025 License: Apache-2.0 Imports: 3 Imported by: 5

README

go-specification

Simple Specification Pattern for Go.

Usage

package main

import (
	"github.com/davfer/go-specification"
	"fmt"
)

type User struct {
	Name string
	Age  int
}

func main() {
	specGt := specification.Attr{
		Name:       "Age",
		Value:      20,
		Compatison: specification.ComparisonGt,
	}
	specLt := specification.Attr{
		Name:       "Age",
		Value:      50,
		Compatison: specification.ComparisonLt,
	}
	spec := specification.And{Operands: []specification.Criteria{specGt, specLt}}
	users := []User{
		{Name: "John", Age: 20},
		{Name: "Jane", Age: 30},
		{Name: "Jack", Age: 40},
		{Name: "Jill", Age: 50},
	}

	var result []User
	for _, user := range users {
		if spec.IsSatisfiedBy(user) {
			result = append(result, user)
		}
	}

	fmt.Println(result) // [{Name: "Jane", Age: 30}, {Name: "Jack", Age: 40}]
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoResolverFound = fmt.Errorf("no resolver found")

ErrNoResolverFound is the error returned when no resolver is found for a given criteria

Functions

This section is empty.

Types

type And

type And struct {
	Operands []Criteria
}

func (And) IsSatisfiedBy

func (a And) IsSatisfiedBy(value any) bool

type Attr

type Attr struct {
	Name       string
	Value      any
	Comparison Comparator
}

func (Attr) IsSatisfiedBy

func (a Attr) IsSatisfiedBy(obj any) bool

type Comparator

type Comparator string
const (
	ComparisonEq  Comparator = "eq"
	ComparisonNe  Comparator = "ne"
	ComparisonGt  Comparator = "gt"
	ComparisonGte Comparator = "gte"
	ComparisonLt  Comparator = "lt"
	ComparisonLte Comparator = "lte"
)

type Converter added in v0.0.2

type Converter[K any] struct {
	Resolvers []Resolver[K]
}

Converter is a helper to convert a Criteria to a different implementation with a set of resolvers

func (Converter[K]) Convert added in v0.0.2

func (c Converter[K]) Convert(criteria Criteria, subject any) (k K, err error)

type Criteria

type Criteria interface {
	// IsSatisfiedBy returns true if the value satisfies the condition.
	IsSatisfiedBy(any) bool
}

Criteria is the interface that wraps the basic method to check if a value satisfies a condition.

type CriteriaPrimitive added in v0.0.2

type CriteriaPrimitive interface {
	Criteria
	GetPrimitive() Criteria
}

CriteriaPrimitive is a helper interface to allow the conversion of a Criteria to a primitive Criteria. Useful to work with custom criteria types.

type Not

type Not struct {
	Operand Criteria
}

func (Not) IsSatisfiedBy

func (n Not) IsSatisfiedBy(value any) bool

type Or

type Or struct {
	Operands []Criteria
}

func (Or) IsSatisfiedBy

func (o Or) IsSatisfiedBy(value any) bool

type Repository added in v0.0.2

type Repository[K any] interface {
	Match(context.Context, Criteria) ([]K, error)
	MatchOne(context.Context, Criteria) (K, error)
}

Repository is the interface that wraps the basic methods to match entities.

type Resolver added in v0.0.2

type Resolver[K any] interface {
	Resolve(Converter[K], Criteria, any) (K, bool)
}

Resolver is a helper interface to allow the conversion of a Criteria to a different implementation

Directories

Path Synopsis
examples
mongo command
users command
v2

Jump to

Keyboard shortcuts

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