Categorygithub.com/relardev/go-pattern-implement
modulepackage
0.1.0
Repository: https://github.com/relardev/go-pattern-implement.git
Documentation: pkg.go.dev

# README

Pattern Implement

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
}

Install

go install github.com/relardev/go-pattern-implement@latest

Usage

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

Patterns

  • 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
  • Goroutine Channel Hinge
  • Batching
  • Throttle
    • Error
    • No error
  • Retry

TODOs

  • Auto run tests on commits

# Packages

No description provided by the author
No description provided by the author