optr

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2025 License: Apache-2.0 Imports: 0 Imported by: 1

README

optr

Package optr provides utility functions for working with optional values represented as pointers in Go. It simplifies common operations like mapping, cloning, and handling default values.

Features

  • Create pointers from non-pointer values.

    value := optr.Some(42)
    fmt.Println(*value) // Output: 42
    
  • Create new values from existing pointers.

    original := optr.Some(42)
    copy := optr.Cloned(original)
    fmt.Println(*copy) // Output: 42
    fmt.Println(copy != original) // Output: true
    
  • Apply transformations to pointer values.

    value := optr.Some(42)
    result := optr.Map(value, func(v int) string { return fmt.Sprintf("Value: %d", v) })
    fmt.Println(*result) // Output: "Value: 42"
    
  • Handle default values and fallbacks with ease.

    value := optr.Or(nil, func() (int, bool) {
      return 42, true
    })
    fmt.Println(*value) // Output: 42
    
  • Support for functions that may fail, returning errors where applicable.

    value, err := optr.TryAnd(Some(42), func(v int) (string, bool, error) {
      if v > 40 {
        return "Greater than 40", true, nil
      }
      return "", false, fmt.Errorf("value is not greater than 40")
    })
    if err != nil {
      fmt.Println("Error:", err)
    } else {
      fmt.Println(*value) // Output: "Greater than 40"
    }
    

Installation

Install the package using go get:

go get github.com/s2-streamstore/optr@latest

Documentation

Head over to pkg.go.dev for detailed documentation and package reference.

Feedback

We use Github Issues to track feature requests and issues with the package. If you wish to provide feedback, report a bug or request a feature, feel free to open a Github issue.

Contributing

Developers are welcome to submit Pull Requests on the repository. If there is no tracking issue for the bug or feature request corresponding to the PR, we encourage you to open one for discussion before submitting the PR.

Reach out to us

Join our Discord server. We would love to hear from you.

You can also email us at hi@s2.dev.

License

This project is licensed under the Apache-2.0 License.

Documentation

Overview

Package optr provides utility functions for working with optional values represented as pointers in Go. It simplifies common operations like mapping, cloning, and handling default values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func And

func And[T, R any](v *T, f func(T) (R, bool)) *R

And applies the function f to the value pointed to by v if v is not nil. If f returns false, it returns nil. Otherwise, it returns a pointer to the result.

Example:

value := Some(42)
result := And(value, func(v int) (string, bool) {
    if v > 40 {
        return "Greater than 40", true
    }
    return "", false
})
fmt.Println(*result) // Output: "Greater than 40"

func AndTry

func AndTry[T, R any](v *T, f func(T) (R, bool, error)) (*R, error)

TryAnd applies the function f to the value pointed to by v if v is not nil. If f returns false or an error, it returns (nil, error). Otherwise, it returns a pointer to the result and a nil error.

Example:

value := Some(42)
result, err := TryAnd(value, func(v int) (string, bool, error) {
    if v > 40 {
        return "Greater than 40", true, nil
    }
    return "", false, fmt.Errorf("value is not greater than 40")
})
if err != nil {
    fmt.Println("Error:", err)
} else if result != nil {
    fmt.Println(*result) // Output: "Greater than 40"
}

func Cloned

func Cloned[T any](v *T) *T

Cloned creates a new pointer by copying the value pointed to by v. If v is nil, it returns nil.

Example:

original := Some(42)
copy := Cloned(original)
fmt.Println(*copy) // Output: 42

func Map

func Map[T, R any](v *T, f func(T) R) *R

Map applies the function f to the value pointed to by v and returns a pointer to the result. If v is nil, it returns nil.

Example:

value := Some(42)
result := Map(value, func(v int) string { return fmt.Sprintf("Value: %d", v) })
fmt.Println(*result) // Output: "Value: 42"

func Or

func Or[T any](v *T, f func() (T, bool)) *T

Or returns v if it is not nil. Otherwise, it calls f and returns a pointer to its result. If f returns false, it returns nil.

Example:

value := Or(nil, func() (int, bool) {
    return 42, true
})
fmt.Println(*value) // Output: 42

func OrTry

func OrTry[T any](v *T, f func() (T, bool, error)) (*T, error)

OrTry returns v if it is not nil. Otherwise, it calls f and returns a pointer to its result along with an error, if any. If f returns false or an error, it returns nil and the error (if any).

Example:

value, err := OrTry(nil, func() (int, bool, error) {
    return 42, true, nil
})
if err != nil {
    fmt.Println("Error:", err)
} else if value != nil {
    fmt.Println(*value) // Output: 42
} else {
    fmt.Println("No value")
}

func Some

func Some[T any](v T) *T

Some returns a pointer to the given value v. This function is useful for creating optional values from non-pointer types.

Example:

value := Some(42)
fmt.Println(*value) // Output: 42

func TryMap

func TryMap[T, R any](v *T, f func(T) (R, error)) (*R, error)

TryMap applies the function f to the value pointed to by v and returns a pointer to the result or an error. If v is nil, it returns nil.

Example:

value := Some(42)
result, err := TryMap(value, func(v int) (string, error) {
    return fmt.Sprintf("Value: %d", v), nil
})
if err != nil {
    fmt.Println("Error:", err)
} else {
    fmt.Println(*result) // Output: "Value: 42"
}

func UnwrapOr

func UnwrapOr[T any](v *T, f func() T) T

UnwrapOr returns the value pointed to by v if v is not nil. Otherwise, it calls f and returns its result.

Example:

value := UnwrapOr(nil, func() int {
    return 42
})
fmt.Println(value) // Output: 42

func UnwrapOrTry

func UnwrapOrTry[T any](v *T, f func() (T, error)) (T, error)

UnwrapOrTry returns the value pointed to by v if v is not nil. Otherwise, it calls f and returns its result along with any error. If f returns an error, that error is returned.

Example:

value, err := UnwrapOrTry(nil, func() (int, error) {
    return 42, nil
})
if err != nil {
    fmt.Println("Error:", err)
} else {
    fmt.Println(value) // Output: 42
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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