apiote

gott

https://git.apiote.xyz/git/gott.git
Table of Contents

gott is a Railway Oriented Programming library for Go.

Warning
Version 1 of gott is deprecated, please use version 2 which uses generics

In ROP a program is a chain of functions wrapped in blocks resembling track switches. It’s a simplification of an Either monad.

gott provides N types of blocks:

  • Bind, which wraps a function that continues on the happy track or switches to the sad track

>------+-------→
        \
         \
>---------+----→
  • Map, which wraps a function that will alwayc continue on the happy track

>--------------→
>--------------→
  • Tee, which wraps a function performing side effects and can switch to the sad track

         _
         |
>--------++----→
           \
            \
>------------+-→
  • SafeTee, which is to Tee what Map is to Bind

         _
         |
>--------+-----→
>--------------→
  • Recover, which wraps a function that tries to return to the happy Path

>--------+-----→
        /
       /
>-----+--------→
  • Catch, which switches to the sad track in case of a panic

  • Handle, which does different things depending on which track the processing is

Usage

Provided functions are methods of Result and return Result so that they can be chained.

Usage can be seen in tests, the simplest being

import (
	"apiote.xyz/p/gott"
)
func divide5(by …​interface{}) (interface{}, error) {
	if b := by[0].(int); b == 0 {
		return Tuple(by), errors.New("divideByZero")
	} else {
		return 5 / b, nil
	}
func main() {
	s, e := NewResult(5).Bind(divide5).Finish()
	// s == 1; e == nil
}

Mirrors

The canonical repository for this project is https://git.apiote.xyz/gott.git it’s mirrored at https://notabug.org/apiote/gott

Mirrors exist solely for the sake of the code and any additional functions provided by third-party services (including but not limited to issues and pull requests) will not be used and will be ignored.

License

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.