https://github.com/gabrielmellooliveira/go-spec
Lib to golang Specification Pattern
https://github.com/gabrielmellooliveira/go-spec
go golang specification-pattern
Last synced: 11 months ago
JSON representation
Lib to golang Specification Pattern
- Host: GitHub
- URL: https://github.com/gabrielmellooliveira/go-spec
- Owner: gabrielmellooliveira
- License: mit
- Created: 2023-10-01T21:08:18.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-01T23:25:14.000Z (over 2 years ago)
- Last Synced: 2023-10-03T07:02:32.924Z (over 2 years ago)
- Topics: go, golang, specification-pattern
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-spec
[![GoDoc Widget]][GoDoc]
`go-spec` is a lib to implement Specification Pattern.
Specification is to separate the statement of how to match a candidate, from the candidate object that it is matched against. As well as its usefulness in selection, it is also valuable for validation and for building to order.
## Install
`go get -u github.com/gabrielmellooliveira/go-spec`
## Examples
### Create a specification
```go
package main
import "github.com/gabrielmellooliveira/go-spec"
type User struct {
minAge int
maxAge int
}
type UserAgeSpecification struct {
Specification spec.Specification[User]
}
func NewUserAgeSpecification() spec.Specification[User] {
specification := &UserAgeSpecification{}
specification.Specification = spec.NewSpecification[User](specification.IsSatisfiedBy)
return specification.Specification
}
func (spec *UserAgeSpecification) IsSatisfiedBy(entity User) bool {
return entity.minAge == entity.maxAge
}
```
### Use a specification
```go
package main
import "github.com/gabrielmellooliveira/go-spec"
func main() {
userAgeSpecification := NewUserAgeSpecification()
user := User{minAge: 18, maxAge: 18}
if userAgeSpecification.IsSatisfiedBy(user) {
...
} else {
...
}
}
```
### Combine specifications
```go
package main
import "github.com/gabrielmellooliveira/go-spec"
func main() {
userAgeSpecification := NewUserAgeSpecification()
userRoleSpecification := NewUserRoleSpecification()
user := User{minAge: 18, maxAge: 18}
if userAgeSpecification.And(userRoleSpecification).IsSatisfiedBy(user) {
...
} else {
...
}
}
```
## License
Copyright (c) 2015-present [Gabriel Mello de Oliveira](https://github.com/gabrielmellooliveira)
Licensed under [MIT License](./LICENSE)
[GoDoc]: https://pkg.go.dev/github.com/gabrielmellooliveira/go-spec
[GoDoc Widget]: https://godoc.org/github.com/gabrielmellooliveira/go-spec?status.svg