https://github.com/katallaxie/pkg
🛠️ My personal collection of Go primitives
https://github.com/katallaxie/pkg
go golang tools toolset
Last synced: 11 months ago
JSON representation
🛠️ My personal collection of Go primitives
- Host: GitHub
- URL: https://github.com/katallaxie/pkg
- Owner: katallaxie
- License: mit
- Created: 2022-08-03T11:10:03.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-08T04:54:25.000Z (11 months ago)
- Last Synced: 2025-04-08T05:29:53.350Z (11 months ago)
- Topics: go, golang, tools, toolset
- Language: Go
- Homepage: https://pkg.go.dev/github.com/katallaxie/pkg
- Size: 1.52 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Pkg
[](https://github.com/katallaxie/pkg/actions/workflows/main.yml)
[](https://pkg.go.dev/github.com/katallaxie/pkg)
[](https://goreportcard.com/report/github.com/katallaxie/pkg)
[](https://twitter.com/SwiftOnSecurity)
[](https://github.com/auchenberg/volkswagen)
[](https://opensource.org/licenses/Apache-2.0)
`pkg` is a collection of Go packages that make the life of a Gopher easier.
[](https://codespaces.new/katallaxie/pkg?quickstart=1)
## Installation
```bash
go get github.com/katallaxie/pkg
```
Go has a pretty good standard library, but there are some things that are missing. This collection of small packages is meant to fill those gaps.
## Casting values
There is the typical case of pointers you need to deference. For example in the strict interfaces generated by [oapi-codegen](https://github.com/oapi-codegen/oapi-codegen).
```go
Somestruct {
Field: cast.Ptr(req.Field),
}
```
Or the other way around, you have a pointer and you want to get the value or a default value.
```go
SomeStruct {
Field: cast.Value(req.Field),
}
```
Return a default value of a `nil` pointer.
```go
type Foo struct {}
cast.Zero(Foo) // &Foo{}
```
## Converting values
There is also always the case to covert a value to a specific other value.
```go
// String converts a value to a string.
b := true
s := cast.String(b)
fmt.Println(s) // "true"
```
There are functions to convert `int`, `string` and `bool` values.
## Operators
There is the implementation of various operators.
```go
// Is the ternary operator.
utilx.IfElse(cond, 100, 0)
```
## Optional
There is also the `Option` type which is a wrapper around a value that can be `nil`.
```go
// Option is a wrapper around a value that can be nil.
a := optional.Some(100)
fmt.Println(a.String()) // true
```
## Redux-inspired state management
This is a simple state management inspired by [Redux](https://redux.js.org/).
```go
// NoopState is a simple state of a store.
type NoopState struct {
Name string `json:"name"`
}
action := redux.NewAction(1, "foo")
state := NoopState{Name: "foo"}
store := redux.NewStore(State, action)
store.Dispatch(action)
updates := store.Subscribe()
```
## Databases
There are also more complex tools like the `Database` interface which enables to easliy implement database wrappers.
```go
// Database provides methods for transactional operations.
type Database[R, W any] interface {
// ReadTx starts a read only transaction.
ReadTx(context.Context, func(context.Context, R) error) error
// ReadWriteTx starts a read write transaction.
ReadWriteTx(context.Context, func(context.Context, W) error) error
Migrator
io.Closer
}
```
Or a simple interface to implement servers. This takes for all `signal` and `context` handling.
```go
s, _ := server.WithContext(ctx)
s.Listen(&srv{}, true)
serverErr := &server.ServerError{}
if err := s.Wait(); errors.As(err, &serverErr) {
log.Print(err)
os.Exit(1)
}
```
## FGA with OpenFGA
There is also a package to work with the OpenFGA API.
```go
// Store is an interface that provides methods for transactional operations on the authz database.
type Store[Tx any] interface {
// Allowed checks if the user is allowed to perform the operation on the object.
Allowed(context.Context, User, Object, Relation) (bool, error)
// WriteTx starts a read write transaction.
WriteTx(context.Context, func(context.Context, Tx) error) error
}
// StoreTx is an interface that provides methods for transactional operations on the authz database.
type StoreTx interface {
// WriteTuple writes a tuple to the authz database.
WriteTuple(context.Context, User, Object, Relation) error
// DeleteTuple deletes a tuple from the authz database.
DeleteTuple(context.Context, User, Object, Relation) error
}
```
This can be used with the package.
```go
authzStore, err := authx.NewStore(fgaClient, authz.NewWriteTx())
if err != nil {
return err
}
```
## License
[MIT](/LICENSE)