dynamic

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2022 License: MIT Imports: 14 Imported by: 0

README

XGo/Dynamic

this package provides some dynamic features using reflect and go/ast.

Features

Function Headers

Examples: https://gowalker.org/github.com/Kretech/xgo/dynamic#_ex_btn_GetFuncHeader

Variable Names

https://gowalker.org/github.com/Kretech/xgo/p#VarName

Compact function from Php

https://gowalker.org/github.com/Kretech/xgo/p#Compact

Variable scopes in goroutine

https://gowalker.org/github.com/Kretech/xgo/p#G

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallerName added in v0.5.8

func CallerName(short bool) string

func CallerNameSkip added in v0.5.8

func CallerNameSkip(skip int, short bool) string

func VarName added in v0.5.7

func VarName(args ...interface{}) []string

func VarNameDepth added in v0.5.7

func VarNameDepth(skip int, args ...interface{}) []string

Types

type FuncHeader

type FuncHeader struct {
	Doc  string // docs above func
	Name string
	In   []*Parameter
	Out  []*Parameter
}

FuncHeader contains function info

func GetFuncHeader

func GetFuncHeader(originFunc interface{}) (fh FuncHeader, err error)

GetFuncHeader return function header cached in funcPC @fixme according the comment of reflect/Value.Pointer pc is not unique for simple function

Example
// // Person ...
// type Person struct{}
//
// // comment
// func (this Person) Name() string {
// 	return `noname`
// }
//
// // Say can say something
// func (this Person) Say(c string) string {
// 	return this.Name() + ` : ` + c
// }

h, _ := GetFuncHeader(Person{}.Say)
log.Println(h.Name)
//: Say-fm

log.Println(h.Doc)
//: // Say can say something

for _, param := range append(h.In, h.Out...) {
	log.Println(param.Name, param.RType)
	//: c string
	//:  string
}
Output:

func GetFuncHeaderNoCache

func GetFuncHeaderNoCache(originFunc interface{}) (fh FuncHeader, err error)

GetFuncHeaderNoCache return function header in runtime without cache

func (*FuncHeader) Encode

func (fh *FuncHeader) Encode() string

Encode is convenient for json marshal

func (*FuncHeader) Equals

func (fh *FuncHeader) Equals(other *FuncHeader) bool

Equals return two header is equivalent for testing

type Name added in v0.5.7

type Name struct {

	// X.Y means how user call this function
	// if user called dynamic.VarName(a), X=dynamic Y=VarName
	// if user called dump.Dump(a) X=dump Y=Dump
	// set this variant used to avoid to scanner import alias
	X string
	Y string
}

func NameOf added in v0.5.8

func NameOf(x string) Name

func (Name) Compact added in v0.5.7

func (name Name) Compact(args ...interface{}) (paramNames []string, paramAndValues map[string]interface{})

func (Name) DepthCompact added in v0.5.7

func (name Name) DepthCompact(depth int, args ...interface{}) (paramNames []string, paramAndValues map[string]interface{})

func (Name) VarName added in v0.5.7

func (n Name) VarName(args ...interface{}) []string

VarName return the variable names VarName(a, b) => []string{"a", "b"}

func (Name) VarNameDepth added in v0.5.7

func (n Name) VarNameDepth(skip int, args ...interface{}) (names []string)

VarNameDepth return the variable names of args... args is a stub for call this method

how can we do this ? first, get function info by runtime.Caller second, get astFile with ast/parser last, match function parameters in ast and get the code

type Parameter

type Parameter struct {
	Name  string
	RType reflect.Type
}

Parameter contains input and output info of function

Jump to

Keyboard shortcuts

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