executioncontext

package
v0.0.1-0...-6deec57 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2022 License: MIT Imports: 1 Imported by: 0

README

executioncontext GoDoc

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

Download:

go get -u github.com/bradishungry/go-api-learning/pkg/executioncontext

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

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearFlag

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

ClearFlag returns a new Context that no longer carries flag.

Example
package main

import (
	"context"
	"fmt"

	"github.com/bradishungry/go-api-learning/pkg/executioncontext"
)

func main() {
	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

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

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

Example
package main

import (
	"context"
	"fmt"

	"github.com/bradishungry/go-api-learning/pkg/executioncontext"
)

func main() {
	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

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

ToggleFlag returns a new Context with toggled flag.

Example
package main

import (
	"context"
	"fmt"

	"github.com/bradishungry/go-api-learning/pkg/executioncontext"
)

func main() {
	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

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

WithFlag returns a new Context that carries flag.

Example
package main

import (
	"context"
	"fmt"

	"github.com/bradishungry/go-api-learning/pkg/executioncontext"
)

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

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

}
Output:

true

Types

type Flag

type Flag uint8

Flag type

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

Execution context flags

func FromContext

func FromContext(ctx context.Context) Flag

FromContext returns the slice of flags stored in ctx

Example
package main

import (
	"context"
	"fmt"

	"github.com/bradishungry/go-api-learning/pkg/executioncontext"
)

func main() {
	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

Jump to

Keyboard shortcuts

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