This tool generates implementation for given code(interface, function definition, etc.), for example when you want to implement caching wrapper for given interface you can pass your interface to go-pattern-implement
and get implementation.
> cat inputs/cache
type Repo interface {
Get(context.Context, string) (User, error)
}
> cat inputs/cache | go-pattern-implement implement cache --package user
type Cache struct {
r user.Repo
cache *cache.Cache
}
func New(r user.Repo, expiration, cleanupInterval time.Duration) *Cache {
return &Cache{r: r, cache: cache.New(expiration, cleanupInterval)}
}
func (r *Cache) Get(ctx context.Context, arg string) (user.User, error) {
key := arg
cachedItem, found := r.cache.Get(key)
if found {
user, ok := cachedItem.(user.User)
if !ok {
return user.User{}, errors.New("invalid object in cache")
}
return user, nil
}
user := r.r.Get(ctx, arg)
if err != nil {
return user.User{}, err
}
r.cache.Set(key, user, cache.DefaultExpiration)
return user, nil
}
go install github.com/relardev/go-pattern-implement@latest
- NeoVim - link
List all implementations
go-pattern-implement list
List only available implementations for given input
cat inputs/prometheus | go-pattern-implement list --available
Implement a pattern
cat inputs/prometheus | go-pattern-implement implement prometheus --package asdf
- Metrics
- Prometheus
- StatsD
- Tracing
- Cache
- Store
- Semaphore
- Basic
- With Cancel
- With Waitgroup
- With Waitgroup and Cancel
- File getter
- Slog
- Log
- Filter
- Whole call
- Result
- Param
- Paralellisation
- Batching
- Throttle
- Error
- No error
- Retry
- Renew (for example token)
- Auto run tests on commits