1
0
Fork 0

Move readme parts to their corresponding packages

This commit is contained in:
Gregory Eremin 2018-07-03 20:33:55 +02:00
parent b4e6729fb0
commit 993153693a
6 changed files with 148 additions and 114 deletions

127
README.md
View File

@ -1,117 +1,16 @@
# Gobelt # Gobelt
Gobelt is a collection of Go tools. Gobelt is a collection of Go tools that I got tired of implementing again and
again.
### Thread pool * [Config](config): Configuration manager
* [CSV](csv2): Extensions to `encoding/csv` package
```go * [File cache](filecache): File cache implementation
import "github.com/localhots/gobelt/threadpool" * [Log](log):
``` * [Pointers](ptrto): Pointers to basic types
* [Reflect](reflect2): Extensions to `reflect` package
```go * [Sets](set): A collection of packages that implement set data structure for
ctx := context.Background() basic types.
ctx, cancel = context.WithTimeout(ctx, 30*time.Second) * [ORM](sqldb): ORM wannabe
defer cancel() * [Thread pool](threadpool): Implements a thread pool
* [Time](time2): Extensions to `time` package with time freezing features
pool := threadpool.New(10)
defer pool.Close()
for i := 0; i < 1000000; i++ {
i := i
pool.Enqueue(ctx, func() {
fmt.Printf("The number is %d\n", i)
})
}
```
### File cache
```go
import "github.com/localhots/gobelt/filecache"
```
```go
ctx := context.Background()
var items []Item
filecache.Load(&items, "tmp/cache/items.json", func() {
err := conn.Query(ctx, "SELECT * FROM items").Load(&items).Error()
if err != nil {
log.Fatalf("Failed to load items: %v", err)
}
})
```
### Log
```go
import "github.com/localhots/gobelt/log"
```
```go
ctx := context.Background()
ctx = log.ContextWithFields(ctx, log.F{"email": params["email"]})
user, err := signup(ctx, params)
if err != nil {
log.Errorf(ctx, "Signup failed: %v", err)
// [ERRO] Signup failed: db: duplicate entry email=bob@example.com
return
}
log.Info(ctx, "New user signed up", log.F{"id": user.ID})
// [INFO] New user signed up email=bob@example.com id=14
```
### Set
There is a collection of packages implementing a set data type located inside
the `set` package. Implemented types include:
* `int`, `int8`, `int16`, `int32`, `int64`
* `uint`, `uint8`, `uint16`, `uint32`, `uint64`
* `string`
All the package names are type names prefixed with "set", e.g. `setuint64`.
```go
import "github.com/localhots/gobelt/set/setstring"
```
```go
s := setstring.New("one", "two")
s.Add("three")
s.Remove("one", "two").Add("four", "five")
fmt.Println("Size:", s.Len()) // 3
fmt.Println("Has one", s.Has("one")) // false
fmt.Println(s.SortedSlice()) // [three four five]
```
### Config
```go
import "github.com/localhots/gobelt/config"
```
Describe configuration structure inside a target package.
```go
package db
var conf struct {
Flavor string `toml:"flavor"`
DSN string `toml:"dsn"`
}
func init() {
config.Require("db", &conf)
}
```
Load configuration from a `main` function:
```go
package main
func main() {
config.Load("config/config.toml")
}
```

40
config/README.md Normal file
View File

@ -0,0 +1,40 @@
## Config
Package config allows other packages to require a certain section of TOML
configuration file to be parsed into its internal configuration structure. Once
config file is processed its values are distributed by the packages that
required them.
```go
import "github.com/localhots/gobelt/config"
```
Describe configuration structure inside a target package then call
`config.Require` from the init function of a package.
> Note: subgroups are not currently supported. If a package is called `s3` and
> located in `aws` parent package, call config section `aws_s3` instead of
> `aws.s3`.
```go
package db
var conf struct {
Flavor string `toml:"flavor"`
DSN string `toml:"dsn"`
}
func init() {
config.Require("db", &conf)
}
```
Load configuration from a `main` function of the app:
```go
package main
func main() {
config.Load("config/config.toml")
}
```

20
filecache/README.md Normal file
View File

@ -0,0 +1,20 @@
## File cache
Package `file_cache` implements a helper function that would cache results of
an expensive operation to a file. It would use file contents when the file exist
and won't call the function again unless the file is deleted.
```go
import "github.com/localhots/gobelt/filecache"
```
```go
ctx := context.Background()
var items []Item
filecache.Load(&items, "tmp/cache/items.json", func() {
err := conn.Query(ctx, "SELECT * FROM items").Load(&items).Error()
if err != nil {
log.Fatalf("Failed to load items: %v", err)
}
})
```

27
log/README.md Normal file
View File

@ -0,0 +1,27 @@
## Log
Package `log` wraps [logrus](https://github.com/sirupsen/logrus) with a set of
convenient short functions. It can also add values to context for future
logging.
```go
import "github.com/localhots/gobelt/log"
```
```go
ctx := context.Background()
ctx = log.ContextWithFields(ctx, log.F{"email": params["email"]})
user, err := signup(ctx, params)
if err != nil {
log.Errorf(ctx, "Signup failed: %v", err)
// [ERRO] Signup failed: db: duplicate entry email=bob@example.com
return
}
log.Info(ctx, "New user signed up", log.F{
"id": user.ID,
"name": user.Name,
})
// [INFO] New user signed up email=bob@example.com id=14 name="Bob Fierce"
```

26
set/README.md Normal file
View File

@ -0,0 +1,26 @@
## Set
Package `set` is a collection of packages implementing a set data type.
Supported types are:
* `int`, `int8`, `int16`, `int32`, `int64`
* `uint`, `uint8`, `uint16`, `uint32`, `uint64`
* `string`
All the package names are type names prefixed with "set", e.g. `setuint64`.
> Note: These packages are generated from a template. Instead of modifying each
> package individually change the template and run `make gen`.
```go
import "github.com/localhots/gobelt/set/setstring"
```
```go
s := setstring.New("one", "two")
s.Add("three")
s.Remove("one", "two").Add("four", "five")
fmt.Println("Size:", s.Len()) // 3
fmt.Println("Has one", s.Has("one")) // false
fmt.Println(s.SortedSlice()) // [three four five]
```

22
threadpool/README.md Normal file
View File

@ -0,0 +1,22 @@
## Thread pool
Package `thread_pool` implements a pool of threads, duh.
```go
import "github.com/localhots/gobelt/threadpool"
```
```go
ctx := context.Background()
ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
defer cancel()
pool := threadpool.New(10)
defer pool.Close()
for i := 0; i < 1000000; i++ {
i := i
pool.Enqueue(ctx, func() {
fmt.Printf("The number is %d\n", i)
})
}
```