go-api-boilerplate: github.com/vardius/go-api-boilerplate/pkg/executioncontext Index | Examples | Files

package executioncontext

import "github.com/vardius/go-api-boilerplate/pkg/executioncontext"

Package executioncontext provides context helper function so set and get execution flags

Index

Examples

Package Files

doc.go execution_context.go flag.go

func ClearFlag Uses

func ClearFlag(ctx context.Context, flag Flag) context.Context

ClearFlag returns a new Context that no longer carries flag.

Code:

ctx := context.Background()
ctx = executioncontext.WithFlag(ctx, executioncontext.LIVE)
ctx = executioncontext.WithFlag(ctx, executioncontext.REPLAY)

ctx = executioncontext.ClearFlag(ctx, executioncontext.LIVE)

fmt.Printf("%v\n", executioncontext.Has(ctx, executioncontext.LIVE))
fmt.Printf("%v\n", executioncontext.Has(ctx, executioncontext.REPLAY))

Output:

false
true

func Has Uses

func Has(ctx context.Context, flag Flag) bool

has returns the bool value based on flag occurrence in context.

Code:

ctx := context.Background()

fmt.Printf("%v\n", executioncontext.Has(ctx, executioncontext.LIVE))

ctx = executioncontext.WithFlag(ctx, executioncontext.LIVE)

fmt.Printf("%v\n", executioncontext.Has(ctx, executioncontext.LIVE))

Output:

false
true

func ToggleFlag Uses

func ToggleFlag(ctx context.Context, flag Flag) context.Context

ToggleFlag returns a new Context with toggled flag.

Code:

ctx := context.Background()
ctx = executioncontext.WithFlag(ctx, executioncontext.LIVE)

ctx = executioncontext.ToggleFlag(ctx, executioncontext.REPLAY)

fmt.Printf("%v\n", executioncontext.Has(ctx, executioncontext.LIVE))
fmt.Printf("%v\n", executioncontext.Has(ctx, executioncontext.REPLAY))

Output:

true
true

func WithFlag Uses

func WithFlag(ctx context.Context, flag Flag) context.Context

WithFlag returns a new Context that carries flag.

Code:

ctx := context.Background()
ctx = executioncontext.WithFlag(ctx, executioncontext.LIVE)

fmt.Printf("%v\n", executioncontext.Has(ctx, executioncontext.LIVE))

Output:

true

type Flag Uses

type Flag uint8

Flag type

const (
    LIVE   Flag = 1 << iota // live events handling
    REPLAY                  // replay events handling
)

Execution context flags

func FromContext Uses

func FromContext(ctx context.Context) Flag

FromContext returns the slice of flags stored in ctx

Code:

ctx := context.Background()
flags := executioncontext.FromContext(ctx)

fmt.Printf("%v\n", flags)

ctx = executioncontext.WithFlag(ctx, executioncontext.LIVE)
ctx = executioncontext.WithFlag(ctx, executioncontext.REPLAY)
flags = executioncontext.FromContext(ctx)

fmt.Printf("%v\n", flags)

Output:

0
3

Package executioncontext imports 1 packages (graph) and is imported by 3 packages. Updated 2020-08-04. Refresh now. Tools for package owners.