helper

package
v2.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 1 Imported by: 0

README

helper

Example

package main

import (
	"fmt"

	"github.com/go-kratos-ecosystem/components/v2/helper"
)

type User struct {
	Name string
	Age  int
}

func main() {
	user := &User{Name: "foo"}

	// Tap
	user = helper.Tap(user, func(u *User) {
		u.Name = "bar"
		u.Age = 18
	})
	fmt.Println(user)
	// output:
	// &{bar 18}

	// With
	user = helper.With(user, func(u *User) *User {
		u.Name = "baz"
		u.Age = 19
		return u
	})
	fmt.Println(user)
	// output:
	// &{baz 19}

	// When
	user = helper.When(user, true, func(u *User) *User {
		u.Name = "Flc"
		u.Age = 20
		return u
	})

	fmt.Println(user)
	// output:
	// &{Flc 20}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain

func Chain[T any](fns ...func(T) T) func(T) T

Chain is a reverse Pipe

Chain(m1, m2, m3)(value) => m1(m2(m3(value)))

func ChainWithErr added in v2.9.0

func ChainWithErr[T any](fns ...func(T) (T, error)) func(T) (T, error)

ChainWithErr is a reverse PipeWithErr

ChainWithErr(m1, m2, m3)(value) => m1(m2(m3(value)))

func If added in v2.9.0

func If[T any](condition bool, trueVal T, falseVal T) T

func Pipe added in v2.9.0

func Pipe[T any](fns ...func(T) T) func(T) T

Pipe is a function that takes a value and returns a value

Pipe(m1, m2, m3)(value) => m3(m2(m1(value)))

func PipeWithErr added in v2.9.0

func PipeWithErr[T any](fns ...func(T) (T, error)) func(T) (T, error)

PipeWithErr is a function that takes a value and returns a value and an error

PipeWithErr(m1, m2, m3)(value) => m3(m2(m1(value)))

func Scan

func Scan(src any, dest any) error

func Tap

func Tap[T any](value T, callbacks ...func(T)) T

func When

func When[T any](value T, condition bool, callbacks ...func(T) T) T

func With

func With[T any](value T, callbacks ...func(T) T) T

Types

type Proxy

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

func NewProxy

func NewProxy[T any](target T) *Proxy[T]

func (*Proxy[T]) Tap

func (p *Proxy[T]) Tap(callbacks ...func(T)) *Proxy[T]

func (*Proxy[T]) Target

func (p *Proxy[T]) Target() T

func (*Proxy[T]) Unless

func (p *Proxy[T]) Unless(condition bool, callbacks ...func(T) T) *Proxy[T]

func (*Proxy[T]) When

func (p *Proxy[T]) When(condition bool, callbacks ...func(T) T) *Proxy[T]

func (*Proxy[T]) With

func (p *Proxy[T]) With(callbacks ...func(T) T) *Proxy[T]

Jump to

Keyboard shortcuts

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