Skip to content
/ pkg Public
generated from ZEISS/template-go

A curated collection of Go packages that make the life easier

License

Notifications You must be signed in to change notification settings

ZEISS/pkg

Folders and files

NameName
Last commit message
Last commit date
Jul 30, 2024
Feb 17, 2025
Aug 2, 2024
Sep 22, 2024
Oct 10, 2024
Nov 19, 2024
Oct 25, 2024
Feb 17, 2025
Dec 3, 2024
Aug 1, 2024
Nov 20, 2024
Jan 11, 2025
Aug 15, 2024
Feb 16, 2025
Dec 23, 2024
Feb 17, 2025
Sep 9, 2024
Jan 6, 2025
Jan 9, 2025
Feb 10, 2025
Feb 25, 2025
Aug 20, 2024
Jan 6, 2025
Aug 26, 2024
Oct 8, 2024
Jan 15, 2025
Jul 30, 2024
Aug 1, 2024
Dec 3, 2024
Oct 9, 2024
Aug 2, 2024
Feb 14, 2025
Nov 19, 2024
Jan 22, 2025
Aug 20, 2024
Feb 12, 2025
Jul 30, 2024
Jul 30, 2024
Jul 30, 2024
Jul 30, 2024
Jul 30, 2024
Feb 13, 2025
Feb 17, 2025
Feb 14, 2025
Jul 31, 2024
Jul 31, 2024
Dec 16, 2024
Jul 31, 2024
Feb 14, 2025
Jul 30, 2024
Feb 26, 2025
Feb 26, 2025

Repository files navigation

Pkg

Test & Lint Go Reference Go Report Card Taylor Swift Volkswagen License

pkg is a collection of Go packages that make the life of a Go developers at ZEISS easier.

Open in GitHub Codespaces

Installation

go get github.com/zeiss/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.

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.

SomeStruct {
  Field: cast.Value(req.Field),
}

Return a default value of a nil pointer.

type Foo struct {}
cast.Zero(Foo) // &Foo{}

Converting values

There is also always the case to covert a value to a specific other value.

// 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.

// Is the ternary operator.
utilx.IfElse(cond, 100, 0)

Databases

There are also more complex tools like the Database interface which enables to easliy implement database wrappers.

// 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.

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.

// 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.

authzStore, err := authx.NewStore(fgaClient, authz.NewWriteTx())
if err != nil {
  return err
}

License

MIT