reflectsource

package
Version: v0.0.0-...-93f0716 Latest Latest
Warning

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

Go to latest
Published: May 2, 2020 License: MIT Imports: 12 Imported by: 4

Documentation

Overview

Package sourcereflect implements run-time source reflection, allowing a program to look up string representation of objects from the underlying .go source files.

Specifically, it implements ability to get name of caller funcs and their parameters. It also implements functionality to get a string containing source code of provided func.

In order to succeed, it expects the program's source code to be available in normal location. It's intended to be used for development purposes, or for experimental programs.

Example
var thisIsAFunkyVarName int
var name string = GetExprAsString(thisIsAFunkyVarName)
fmt.Println("Name of var:", name)
fmt.Println("Some func name:", GetExprAsString(strings.HasPrefix))
fmt.Println("Name of second arg:", getMySecondArgExprAsString(5, thisIsAFunkyVarName))
Output:

Name of var: thisIsAFunkyVarName
Some func name: strings.HasPrefix
Name of second arg: thisIsAFunkyVarName
Example (GetLineStartEndIndicies)
b := []byte(`this

this is a longer line
and
stuff
last`)

for lineIndex := 0; ; lineIndex++ {
	s, e := getLineStartEndIndicies(b, lineIndex)
	fmt.Printf("%v: [%v, %v]\n", lineIndex, s, e)
	if s == -1 {
		break
	}
}
Output:

0: [0, 4]
1: [5, 5]
2: [6, 27]
3: [28, 31]
4: [32, 37]
5: [38, 42]
6: [-1, -1]
Example (Nil)
var f func()

fmt.Println(GetSourceAsString(f))
Output:

nil
Example (TrickyCases)
var thisIsAFunkyVarName int
fmt.Println("1 2 3 4:", getMySecondArgExprAsString(1, 2), getMySecondArgExprAsString(3, 4)) // TODO: This should be 2, 4, not 2, 2
fmt.Println("Name of second arg:",                                                          // TODO: This should work
	getMySecondArgExprAsString(5, thisIsAFunkyVarName))
Output:

1 2 3 4: 2 2
Name of second arg: <expr not found>
Example (Two)
f := func(a int, b int) int {
	c := a + b
	return c
}

fmt.Println(GetSourceAsString(f))
Output:

func(a int, b int) int {
	c := a + b
	return c
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetExprAsString

func GetExprAsString(_ interface{}) string

GetExprAsString gets the expression as a string.

Example
var thisIsAFunkyVarName int

fmt.Println("Name of var:", GetExprAsString(thisIsAFunkyVarName))
Output:

Name of var: thisIsAFunkyVarName

func GetFuncValueSourceAsString

func GetFuncValueSourceAsString(fv reflect.Value) string

GetFuncValueSourceAsString returns the source of the func value fv.

Example
f := func(a int, b int) int {
	c := a + b
	return c
}

fv := reflect.ValueOf(f)

fmt.Println(GetFuncValueSourceAsString(fv))
Output:

func(a int, b int) int {
	c := a + b
	return c
}

func GetParentArgExprAllAsString

func GetParentArgExprAllAsString() []string

GetParentArgExprAllAsString gets all argument expressions of parent func call as a string.

func GetParentArgExprAsString

func GetParentArgExprAsString(argIndex uint32) string

GetParentArgExprAsString gets the argIndex argument expression of parent func call as a string.

func GetParentFuncArgsAsString

func GetParentFuncArgsAsString(args ...interface{}) string

GetParentFuncArgsAsString gets the parent func with its args as a string.

func GetParentFuncAsString

func GetParentFuncAsString() string

GetParentFuncAsString gets the parent func as a string.

func GetSourceAsString

func GetSourceAsString(f interface{}) string

GetSourceAsString returns the source of the func f.

Example
var f func()
f1 := func() {
	panic(123)
}
f2 := func() {
	println("Hello from anon func!") // Comments are currently not preserved.
}
if 5*5 > 30 {
	f = f1
} else {
	f = f2
}

fmt.Println(GetSourceAsString(f))
Output:

func() {
	println("Hello from anon func!")
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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