contexter

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2022 License: MIT Imports: 6 Imported by: 1

README

GoDoc

Contexter

A dirty piece of code for fetching context.Context from the stack when it cannot be passed normally.

But why?

I turned out to need access to context.Context from a MarshalJSON() method and found out there is no way to pass it easily. This is one of my attempts at passing ctx across the stack.

So if your method that accepts a context.Context calls json.Marshal(), it is likely the MarshalJSON() methods will be able to fetch the context using contexter.Context().

My soul doesn't hurt enough

More stack based golang dark magic can be found online:

It doesn't work

There can be various reasons why this doesn't work, or stopped working.

  • The method receiving a context.Context has been inlined and its parameters aren't available
  • The variable containing the context.Context isn't used and was overwritten
  • You're into a different goroutine (which means a new stack)
  • Something changed in Go's runtime, stack display format, etc
  • The architecture you're using does something that's not accounted for in this module

This is not an exact science, and using this package may result in the end of the world, or your program crashing in ways go can't recover. You've been warned.

Usage

ctx := contexter.Context()

This will automatically fetch the closest context.Context object found in the stack that was passed as a context.Context object, or nil if none were found.

var ctx context.Context
if contexter.Find(&ctx) {
	// use ctx
}

This alternative version can find other kind of interfaces on the stack.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Context

func Context() context.Context

Context will return the first instance of context.Context found in the calling stack

func Find

func Find(i interface{}) bool

Types

This section is empty.

Jump to

Keyboard shortcuts

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