r

package module
v0.0.0-...-519f5af Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2024 License: MIT Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

r

r is a library that helps you handle exceptions in a type-safe manner in Golang. It is inspired by Rust's Result type, and built on top of mo library.

It provides a ResultX(Result ~ Result5) type that can be used to handle exceptions in a type-safe manner.

You can define your own result type that composited with the ResultX type and use it as the return type of your internal implementation. This can help you define the normal and exceptional return values of your function in a proper way. By utilizing the Unpack method, you can easily handle the normal and exceptional return values of your function.

Install

go get github.com/akishichinibu/r

Usage

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/akishichinibu/r"
)

// define you own error types
type UserNotFoundErr struct {
	_      string
	UserID string
}

var _ error = UserNotFoundErr{}

func (e UserNotFoundErr) Error() string {
	return fmt.Sprintf("user not found: userID: %s", e.UserID)
}

type UserRestrictedErr struct {
	_      string
	UserID string
	Reason string
}

var _ error = UserRestrictedErr{}

func (e UserRestrictedErr) Error() string {
	return fmt.Sprintf("user restricted: userID: %s, reason: %s", e.UserID, e.Reason)
}

type GetUserBalanceResult struct {
	r.Result2[int, UserNotFoundErr, UserRestrictedErr]
}

// write your internal implementation
func getUserBalance(userID string) (GetUserBalanceResult, error) {
	var r GetUserBalanceResult
	if userID == "invalid" {
		r.FromFailure1(UserNotFoundErr{
			UserID: userID,
		})
		return r, nil
	}
	if userID == "restricted" {
		r.FromFailure2(UserRestrictedErr{
			UserID: userID,
			Reason: "dummpy reason",
		})
		return r, nil
	}
	if userID == "badguy" {
		return r, fmt.Errorf("internal error")
	}
	r.FromSuccess(1000)
	return r, nil
}

// write a service function
func serviceGetUserBalance(userID string) error {
	result, err := getUserBalance(userID)
	if err != nil {
		return err
	}

	balance, known, ok := result.Unpack()
	if ok {
		fmt.Printf("balance: %d\n", balance)
		return nil
	}

	if e1, ok := known.Failure1(); ok {
		return fmt.Errorf("service failed: %w", e1)
	}

	if e2, ok := known.Failure2(); ok {
		return fmt.Errorf("service failed: %w", e2)
	}

	return nil
}

func main() {
	if len(os.Args) < 2 {
		fmt.Println("usage: user_service <user_id>")
		os.Exit(1)
	}
	userID := os.Args[1]
	if err := serviceGetUserBalance(userID); err != nil {
		log.Fatalln(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error1

type Error1[E1 error] struct {
	// contains filtered or unexported fields
}

Error is a type that can be used to represent an error with only one case.

func (Error1[E1]) Failure

func (e Error1[E1]) Failure() (E1, bool)

Return whether the error is in the first case.

func (*Error1[E1]) FromFailure

func (e *Error1[E1]) FromFailure(err E1)

Overwrite the error into the first case with the given error.

type Error2

type Error2[E1 error, E2 error] struct {
	// contains filtered or unexported fields
}

Code generated by github.com/akishichinibu/r. DO NOT EDIT.

Error2 is a type that can be used to represent an error with 2 cases.

func (Error2[E1, E2]) Failure1

func (e Error2[E1, E2]) Failure1() (E1, bool)

Return whether the error is in the case 1.

func (Error2[E1, E2]) Failure2

func (e Error2[E1, E2]) Failure2() (E2, bool)

Return whether the error is in the case 2.

func (*Error2[E1, E2]) FromFailure1

func (e *Error2[E1, E2]) FromFailure1(err E1)

Overwrite the error into the case 1 with the given error.

func (*Error2[E1, E2]) FromFailure2

func (e *Error2[E1, E2]) FromFailure2(err E2)

Overwrite the error into the case 2 with the given error.

type Error3

type Error3[E1 error, E2 error, E3 error] struct {
	// contains filtered or unexported fields
}

Error3 is a type that can be used to represent an error with 3 cases.

func (Error3[E1, E2, E3]) Failure1

func (e Error3[E1, E2, E3]) Failure1() (E1, bool)

Return whether the error is in the case 1.

func (Error3[E1, E2, E3]) Failure2

func (e Error3[E1, E2, E3]) Failure2() (E2, bool)

Return whether the error is in the case 2.

func (Error3[E1, E2, E3]) Failure3

func (e Error3[E1, E2, E3]) Failure3() (E3, bool)

Return whether the error is in the case 3.

func (*Error3[E1, E2, E3]) FromFailure1

func (e *Error3[E1, E2, E3]) FromFailure1(err E1)

Overwrite the error into the case 1 with the given error.

func (*Error3[E1, E2, E3]) FromFailure2

func (e *Error3[E1, E2, E3]) FromFailure2(err E2)

Overwrite the error into the case 2 with the given error.

func (*Error3[E1, E2, E3]) FromFailure3

func (e *Error3[E1, E2, E3]) FromFailure3(err E3)

Overwrite the error into the case 3 with the given error.

type Error4

type Error4[E1 error, E2 error, E3 error, E4 error] struct {
	// contains filtered or unexported fields
}

Error4 is a type that can be used to represent an error with 4 cases.

func (Error4[E1, E2, E3, E4]) Failure1

func (e Error4[E1, E2, E3, E4]) Failure1() (E1, bool)

Return whether the error is in the case 1.

func (Error4[E1, E2, E3, E4]) Failure2

func (e Error4[E1, E2, E3, E4]) Failure2() (E2, bool)

Return whether the error is in the case 2.

func (Error4[E1, E2, E3, E4]) Failure3

func (e Error4[E1, E2, E3, E4]) Failure3() (E3, bool)

Return whether the error is in the case 3.

func (Error4[E1, E2, E3, E4]) Failure4

func (e Error4[E1, E2, E3, E4]) Failure4() (E4, bool)

Return whether the error is in the case 4.

func (*Error4[E1, E2, E3, E4]) FromFailure1

func (e *Error4[E1, E2, E3, E4]) FromFailure1(err E1)

Overwrite the error into the case 1 with the given error.

func (*Error4[E1, E2, E3, E4]) FromFailure2

func (e *Error4[E1, E2, E3, E4]) FromFailure2(err E2)

Overwrite the error into the case 2 with the given error.

func (*Error4[E1, E2, E3, E4]) FromFailure3

func (e *Error4[E1, E2, E3, E4]) FromFailure3(err E3)

Overwrite the error into the case 3 with the given error.

func (*Error4[E1, E2, E3, E4]) FromFailure4

func (e *Error4[E1, E2, E3, E4]) FromFailure4(err E4)

Overwrite the error into the case 4 with the given error.

type Error5

type Error5[E1 error, E2 error, E3 error, E4 error, E5 error] struct {
	// contains filtered or unexported fields
}

Error5 is a type that can be used to represent an error with 5 cases.

func (Error5[E1, E2, E3, E4, E5]) Failure1

func (e Error5[E1, E2, E3, E4, E5]) Failure1() (E1, bool)

Return whether the error is in the case 1.

func (Error5[E1, E2, E3, E4, E5]) Failure2

func (e Error5[E1, E2, E3, E4, E5]) Failure2() (E2, bool)

Return whether the error is in the case 2.

func (Error5[E1, E2, E3, E4, E5]) Failure3

func (e Error5[E1, E2, E3, E4, E5]) Failure3() (E3, bool)

Return whether the error is in the case 3.

func (Error5[E1, E2, E3, E4, E5]) Failure4

func (e Error5[E1, E2, E3, E4, E5]) Failure4() (E4, bool)

Return whether the error is in the case 4.

func (Error5[E1, E2, E3, E4, E5]) Failure5

func (e Error5[E1, E2, E3, E4, E5]) Failure5() (E5, bool)

Return whether the error is in the case 5.

func (*Error5[E1, E2, E3, E4, E5]) FromFailure1

func (e *Error5[E1, E2, E3, E4, E5]) FromFailure1(err E1)

Overwrite the error into the case 1 with the given error.

func (*Error5[E1, E2, E3, E4, E5]) FromFailure2

func (e *Error5[E1, E2, E3, E4, E5]) FromFailure2(err E2)

Overwrite the error into the case 2 with the given error.

func (*Error5[E1, E2, E3, E4, E5]) FromFailure3

func (e *Error5[E1, E2, E3, E4, E5]) FromFailure3(err E3)

Overwrite the error into the case 3 with the given error.

func (*Error5[E1, E2, E3, E4, E5]) FromFailure4

func (e *Error5[E1, E2, E3, E4, E5]) FromFailure4(err E4)

Overwrite the error into the case 4 with the given error.

func (*Error5[E1, E2, E3, E4, E5]) FromFailure5

func (e *Error5[E1, E2, E3, E4, E5]) FromFailure5(err E5)

Overwrite the error into the case 5 with the given error.

type Result1

type Result1[T any, E1 error] struct {
	// contains filtered or unexported fields
}

Result1 is a type that can be used to represent a result that contains a value in success case and one error in failure case.

func (*Result1[T, E1]) FromFailure

func (r *Result1[T, E1]) FromFailure(err E1)

Overwrite the error of the result with the given error into the first failure case.

func (*Result1[T, E1]) FromSuccess

func (r *Result1[T, E1]) FromSuccess(value T)

Overwrite the value of the result with the given value into the success case.

func (Result1[T, E1]) IsFailure

func (r Result1[T, E1]) IsFailure() bool

Return whether the result is the failure case.

func (Result1[T, E1]) IsSuccess

func (r Result1[T, E1]) IsSuccess() bool

Return whether the result is the success case.

func (Result1[T, E1]) Unpack

func (r Result1[T, E1]) Unpack() (value T, err Error1[E1], ok bool)

Unpack the result into the value, the error, and whether the result is the success case.

type Result2

type Result2[T any, E1 error, E2 error] struct {
	// contains filtered or unexported fields
}

Code generated by github.com/akishichinibu/r. DO NOT EDIT.

Result2 is a type that can be used to represent a result that contains a value in success case and 2 error types in failure case.

func (*Result2[T, E1, E2]) FromFailure1

func (r *Result2[T, E1, E2]) FromFailure1(err E1)

Overwrite the error of the result with the given error into the failure case 1.

func (*Result2[T, E1, E2]) FromFailure2

func (r *Result2[T, E1, E2]) FromFailure2(err E2)

Overwrite the error of the result with the given error into the failure case 2.

func (*Result2[T, E1, E2]) FromSuccess

func (r *Result2[T, E1, E2]) FromSuccess(value T)

Overwrite the value of the result with the given value into the success case.

func (Result2[T, E1, E2]) IsFailure

func (r Result2[T, E1, E2]) IsFailure() bool

Return whether the result is the failure case.

func (Result2[T, E1, E2]) IsSuccess

func (r Result2[T, E1, E2]) IsSuccess() bool

Return whether the result is the success case.

func (Result2[T, E1, E2]) Unpack

func (r Result2[T, E1, E2]) Unpack() (T, Error2[E1, E2], bool)

Unpack the result into the value, the error, and whether the result is the success case.

type Result3

type Result3[T any, E1 error, E2 error, E3 error] struct {
	// contains filtered or unexported fields
}

Result3 is a type that can be used to represent a result that contains a value in success case and 3 error types in failure case.

func (*Result3[T, E1, E2, E3]) FromFailure1

func (r *Result3[T, E1, E2, E3]) FromFailure1(err E1)

Overwrite the error of the result with the given error into the failure case 1.

func (*Result3[T, E1, E2, E3]) FromFailure2

func (r *Result3[T, E1, E2, E3]) FromFailure2(err E2)

Overwrite the error of the result with the given error into the failure case 2.

func (*Result3[T, E1, E2, E3]) FromFailure3

func (r *Result3[T, E1, E2, E3]) FromFailure3(err E3)

Overwrite the error of the result with the given error into the failure case 3.

func (*Result3[T, E1, E2, E3]) FromSuccess

func (r *Result3[T, E1, E2, E3]) FromSuccess(value T)

Overwrite the value of the result with the given value into the success case.

func (Result3[T, E1, E2, E3]) IsFailure

func (r Result3[T, E1, E2, E3]) IsFailure() bool

Return whether the result is the failure case.

func (Result3[T, E1, E2, E3]) IsSuccess

func (r Result3[T, E1, E2, E3]) IsSuccess() bool

Return whether the result is the success case.

func (Result3[T, E1, E2, E3]) Unpack

func (r Result3[T, E1, E2, E3]) Unpack() (T, Error3[E1, E2, E3], bool)

Unpack the result into the value, the error, and whether the result is the success case.

type Result4

type Result4[T any, E1 error, E2 error, E3 error, E4 error] struct {
	// contains filtered or unexported fields
}

Result4 is a type that can be used to represent a result that contains a value in success case and 4 error types in failure case.

func (*Result4[T, E1, E2, E3, E4]) FromFailure1

func (r *Result4[T, E1, E2, E3, E4]) FromFailure1(err E1)

Overwrite the error of the result with the given error into the failure case 1.

func (*Result4[T, E1, E2, E3, E4]) FromFailure2

func (r *Result4[T, E1, E2, E3, E4]) FromFailure2(err E2)

Overwrite the error of the result with the given error into the failure case 2.

func (*Result4[T, E1, E2, E3, E4]) FromFailure3

func (r *Result4[T, E1, E2, E3, E4]) FromFailure3(err E3)

Overwrite the error of the result with the given error into the failure case 3.

func (*Result4[T, E1, E2, E3, E4]) FromFailure4

func (r *Result4[T, E1, E2, E3, E4]) FromFailure4(err E4)

Overwrite the error of the result with the given error into the failure case 4.

func (*Result4[T, E1, E2, E3, E4]) FromSuccess

func (r *Result4[T, E1, E2, E3, E4]) FromSuccess(value T)

Overwrite the value of the result with the given value into the success case.

func (Result4[T, E1, E2, E3, E4]) IsFailure

func (r Result4[T, E1, E2, E3, E4]) IsFailure() bool

Return whether the result is the failure case.

func (Result4[T, E1, E2, E3, E4]) IsSuccess

func (r Result4[T, E1, E2, E3, E4]) IsSuccess() bool

Return whether the result is the success case.

func (Result4[T, E1, E2, E3, E4]) Unpack

func (r Result4[T, E1, E2, E3, E4]) Unpack() (T, Error4[E1, E2, E3, E4], bool)

Unpack the result into the value, the error, and whether the result is the success case.

type Result5

type Result5[T any, E1 error, E2 error, E3 error, E4 error, E5 error] struct {
	// contains filtered or unexported fields
}

Result5 is a type that can be used to represent a result that contains a value in success case and 5 error types in failure case.

func (*Result5[T, E1, E2, E3, E4, E5]) FromFailure1

func (r *Result5[T, E1, E2, E3, E4, E5]) FromFailure1(err E1)

Overwrite the error of the result with the given error into the failure case 1.

func (*Result5[T, E1, E2, E3, E4, E5]) FromFailure2

func (r *Result5[T, E1, E2, E3, E4, E5]) FromFailure2(err E2)

Overwrite the error of the result with the given error into the failure case 2.

func (*Result5[T, E1, E2, E3, E4, E5]) FromFailure3

func (r *Result5[T, E1, E2, E3, E4, E5]) FromFailure3(err E3)

Overwrite the error of the result with the given error into the failure case 3.

func (*Result5[T, E1, E2, E3, E4, E5]) FromFailure4

func (r *Result5[T, E1, E2, E3, E4, E5]) FromFailure4(err E4)

Overwrite the error of the result with the given error into the failure case 4.

func (*Result5[T, E1, E2, E3, E4, E5]) FromFailure5

func (r *Result5[T, E1, E2, E3, E4, E5]) FromFailure5(err E5)

Overwrite the error of the result with the given error into the failure case 5.

func (*Result5[T, E1, E2, E3, E4, E5]) FromSuccess

func (r *Result5[T, E1, E2, E3, E4, E5]) FromSuccess(value T)

Overwrite the value of the result with the given value into the success case.

func (Result5[T, E1, E2, E3, E4, E5]) IsFailure

func (r Result5[T, E1, E2, E3, E4, E5]) IsFailure() bool

Return whether the result is the failure case.

func (Result5[T, E1, E2, E3, E4, E5]) IsSuccess

func (r Result5[T, E1, E2, E3, E4, E5]) IsSuccess() bool

Return whether the result is the success case.

func (Result5[T, E1, E2, E3, E4, E5]) Unpack

func (r Result5[T, E1, E2, E3, E4, E5]) Unpack() (T, Error5[E1, E2, E3, E4, E5], bool)

Unpack the result into the value, the error, and whether the result is the success case.

Directories

Path Synopsis
cmd
gen command
examples
user_service command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址