swap

package module
v0.0.0-...-6fcfc91 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2020 License: MIT Imports: 2 Imported by: 0

README

go-swap

Testing utility for swapping out private field members without the need of dependency inversion of control.

Example
ex := example{
    nested: nestedFields{
        subPrivateField: "old",
    },
}

myPointer := swap.Init(&ex).
    Find("nested", func(p unsafe.Pointer) interface{} {
        return (*nestedFields)(p)
    }).
    Find("subPrivateFieldOne", func(p unsafe.Pointer) interface{} {
        return (*string)(p)
    }).Pointer()

// Assert type
str := myPointer.(*string)

// Reassign new value to pointer
*str = "new"

// swap.example{nested:swap.nestedFields{subPrivateFieldOne:"old", subPrivateFieldTwo:0, subPrivateFieldThree:0, subPrivateFieldFour:0, subPrivateFieldFive:false, subPrivateFieldSix:0, subPrivateFieldSeven:0}}
// swap.example{nested:swap.nestedFields{subPrivateFieldOne:"new", subPrivateFieldTwo:0, subPrivateFieldThree:0, subPrivateFieldFour:0, subPrivateFieldFive:false, subPrivateFieldSix:0, subPrivateFieldSeven:0}}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Swap

type Swap interface {
	Find(field string, castFn func(p unsafe.Pointer) interface{}) Swap
	Pointer() unsafe.Pointer
}

type Swapper

type Swapper struct {
	// contains filtered or unexported fields
}

func Init

func Init(obj interface{}) *Swapper

func (*Swapper) Find

func (s *Swapper) Find(field string, castFn func(p unsafe.Pointer) interface{}) *Swapper

func (*Swapper) Pointer

func (s *Swapper) Pointer() interface{}

Jump to

Keyboard shortcuts

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