safe

package module
v1.0.15 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: GPL-3.0 Imports: 9 Imported by: 2

README

safetypes

Rust like result and option implementation for golang

Examples

import "github.com/JPratama7/safe"
Option
import "github.com/JPratama7/safe"

func checkUnwrap(opt safe.Option[int]) {
    if opt.IsSome() {
        println(opt.Unwrap())
    } else {
        panic("poor option :(")
    }
}
import "github.com/JPratama7/safe"

func checkUnwrapOr(opt safe.Option[int]) {
    println(opt.UnwrapOr(10))
}
import "github.com/JPratama7/safe"

func retrunOption(some bool) (opt safe.Option[int]) {
    if some {
        return opt.Some(7)
    }
    return opt.None()
}
import "github.com/JPratama7/safe"

type Test struct {
    Field safe.Option[int]
}

func jsonMarshal(t Test) {
    res := safe.AsResult(json.Marshal(s))
    if res.IsOk() {
        // if some: "Test{Field: 7}"
        // if none: "Test{Field: {}}"
        println(res.Unwrap())
    } else {
        panic(res.Error())
    }
}
Result
func checkUnwrap(res safe.Result[int]) {
    if res.IsOk() {
        println(res.Unwrap())
    } else {
        panic(res.Error())
    }
}
import "github.com/JPratama7/safe"

func retrunResult(some bool) (res safe.Result[int]) {
    if some {
        return res.Ok(7)
    }
    return res.Err("some fancy error msg")
}
Benchmark
go test -bench=.
goos: linux
goarch: amd64
pkg: github.com/JPratama7/safe
cpu: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx  
BenchmarkOkSlicesStruct-8               1000000000               0.4642 ns/op          0 B/op          0 allocs/op
BenchmarkOkSlicesString-8               1000000000               0.4443 ns/op          0 B/op          0 allocs/op
BenchmarkOkSlicesInt-8                  1000000000               0.4812 ns/op          0 B/op          0 allocs/op
BenchmarkOkMapIntString-8               1000000000               0.4609 ns/op          0 B/op          0 allocs/op
BenchmarkResult_Err-8                   1000000000               0.4570 ns/op          0 B/op          0 allocs/op
BenchmarkResult_Ok-8                    1000000000               0.4541 ns/op          0 B/op          0 allocs/op
BenchmarkResultTestOk-8                 1000000000               0.4341 ns/op          0 B/op          0 allocs/op
BenchmarkAsResultEmptyErr-8             1000000000               0.4380 ns/op          0 B/op          0 allocs/op
BenchmarkAsResultEmptyNoErr-8           1000000000               0.4588 ns/op          0 B/op          0 allocs/op
BenchmarkResult_OkInt-8                 1000000000               0.4546 ns/op          0 B/op          0 allocs/op
BenchmarkResult_EmptyInt-8              1000000000               0.4531 ns/op          0 B/op          0 allocs/op
BenchmarkResult_OkString-8              1000000000               0.4597 ns/op          0 B/op          0 allocs/op
BenchmarkResult_EmptyString-8           1000000000               0.4449 ns/op          0 B/op          0 allocs/op
BenchmarkOption_Some-8                  1000000000               0.4482 ns/op          0 B/op          0 allocs/op
BenchmarkOption_None-8                  1000000000               0.4482 ns/op          0 B/op          0 allocs/op
BenchmarkOption_IsNone-8                1000000000               0.4710 ns/op          0 B/op          0 allocs/op
BenchmarkOption_IsSome-8                1000000000               0.4466 ns/op          0 B/op          0 allocs/op
BenchmarkErrorCheck-8                   1000000000               0.4711 ns/op          0 B/op          0 allocs/op
PASS
ok      github.com/JPratama7/safe       9.108s
Note

Error and None methods usable as structless but it doesn't infere types so instead of using safetypes.None[T]() and safetypes.Err[T]("") you could use them as how in examples above

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ByteCheck = []byte("{}")

Functions

func IsNotEmpty added in v1.0.14

func IsNotEmpty(data any) bool

Types

type Option

type Option[T any] struct {
	// contains filtered or unexported fields
}

func None

func None[T any]() (o Option[T])

func Some

func Some[T any](value T) (o Option[T])

func (Option[T]) Expect added in v1.0.9

func (o Option[T]) Expect(err string) T

func (*Option[T]) IsNone

func (o *Option[T]) IsNone() (res bool)

func (*Option[T]) IsSome

func (o *Option[T]) IsSome() (res bool)

func (Option[T]) MarshalBSON

func (o Option[T]) MarshalBSON() ([]byte, error)

func (Option[T]) MarshalJSON

func (o Option[T]) MarshalJSON() ([]byte, error)

func (*Option[T]) None added in v1.0.6

func (o *Option[T]) None()

func (*Option[T]) Some added in v1.0.6

func (o *Option[T]) Some(value T)

func (*Option[T]) UnmarshalBSON

func (o *Option[T]) UnmarshalBSON(data []byte) error

func (*Option[T]) UnmarshalJSON

func (o *Option[T]) UnmarshalJSON(data []byte) error

func (Option[T]) Unwrap

func (o Option[T]) Unwrap() T

func (Option[T]) UnwrapOr

func (o Option[T]) UnwrapOr(or T) T

type Result

type Result[T any] struct {
	// contains filtered or unexported fields
}

func AsResult

func AsResult[T any](value T, err error) (res Result[T])

func Err

func Err[T any](err string) (res Result[T])

func Ok

func Ok[T any](value T) (res Result[T])

func (*Result[T]) Err added in v1.0.6

func (r *Result[T]) Err() Option[error]

func (*Result[T]) Error

func (r *Result[T]) Error() error

func (Result[T]) Expect added in v1.0.8

func (r Result[T]) Expect(err string) T

func (*Result[T]) IsErr

func (r *Result[T]) IsErr() (res bool)

func (*Result[T]) IsOk

func (r *Result[T]) IsOk() (res bool)

func (Result[T]) MarshalBSON

func (r Result[T]) MarshalBSON() ([]byte, error)

func (Result[T]) MarshalJSON

func (r Result[T]) MarshalJSON() ([]byte, error)

func (*Result[T]) Ok added in v1.0.6

func (r *Result[T]) Ok() Option[T]

func (*Result[T]) UnmarshalBSON

func (r *Result[T]) UnmarshalBSON(data []byte) error

func (*Result[T]) UnmarshalJSON

func (r *Result[T]) UnmarshalJSON(data []byte) error

func (Result[T]) Unwrap

func (r Result[T]) Unwrap() T

func (*Result[T]) UnwrapErr added in v1.0.13

func (r *Result[T]) UnwrapErr() error

func (Result[T]) UnwrapOr

func (r Result[T]) UnwrapOr(or T) T

type Valuer added in v1.0.11

type Valuer interface {
	Unwrap() any
	UnwrapOr(any) any
	Expect(string) any
}

Jump to

Keyboard shortcuts

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