reflectshape

package module
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2023 License: MIT Imports: 10 Imported by: 5

README

reflect-shape

reflect shape

Motivation

see _examples/motivation/main.go

Note

  • ⚠ v0.4 is completely incompatible with previous version implementations

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	DocTruncationSize = 10
)

Functions

func IsZeroRecursive added in v0.4.0

func IsZeroRecursive(rt reflect.Type, rv reflect.Value) bool

Types

type Config added in v0.4.0

type Config struct {
	SkipComments       bool // if true, skip extracting argNames and comments
	FillArgNames       bool // func(context.Context, int) -> func(ctx context.Context, arg0 int)
	FillReturnNames    bool // func() (int, error) -> func() (ret0, err)
	IncludeGoTestFiles bool

	DocTruncationSize int

	Fset *token.FileSet
	// contains filtered or unexported fields
}
Example
package main

import (
	"fmt"

	reflectshape "github.com/podhmo/reflect-shape"
)

// User is the object for User.
type User struct {
	// name of User.
	Name string
	Age  int // age of User.
}

// Hello function.
func Hello(user *User /* greeting target */) {
	fmt.Println("Hello", user.Name)
}

func main() {
	cfg := reflectshape.Config{IncludeGoTestFiles: true}
	shape := cfg.Extract(User{})

	fmt.Printf("%s %s %s %q\n", shape.Name, shape.Kind, shape.Package.Path, shape.Struct().Doc())
	for _, f := range shape.Struct().Fields() {
		fmt.Printf("-- %s %q\n", f.Name, f.Doc)
	}

	shape2 := cfg.Extract(Hello)
	fmt.Printf("%s %s %s %q\n", shape2.Name, shape2.Kind, shape2.Package.Path, shape2.Func().Doc())
	for _, a := range shape2.Func().Args() {
		fmt.Printf("-- %s %q\n", a.Name, a.Doc)
	}

}
Output:

User struct github.com/podhmo/reflect-shape_test "User is the object for User."
-- Name "name of User."
-- Age "age of User."
Hello func github.com/podhmo/reflect-shape_test "Hello function."
-- user "greeting target"

func (*Config) Extract added in v0.4.0

func (c *Config) Extract(ob interface{}) *Shape

func (*Config) Visited added in v0.4.0

func (c *Config) Visited() map[ID]*Shape

type Extractor

type Extractor struct {
	Config *Config
	Lookup *metadata.Lookup
	// contains filtered or unexported fields
}

func (*Extractor) Extract

func (e *Extractor) Extract(ob interface{}) *Shape

func (*Extractor) Visited added in v0.4.0

func (e *Extractor) Visited() map[ID]*Shape

type Field added in v0.4.0

type Field struct {
	reflect.StructField
	Shape *Shape
	Doc   string
}

func (*Field) String added in v0.4.0

func (f *Field) String() string

type FieldList added in v0.4.0

type FieldList []*Field

func (FieldList) Len added in v0.4.0

func (fl FieldList) Len() int

func (FieldList) String added in v0.4.0

func (fl FieldList) String() string

type Func added in v0.4.0

type Func struct {
	Shape *Shape
	// contains filtered or unexported fields
}

func (*Func) Args added in v0.4.0

func (f *Func) Args() VarList

func (*Func) Doc added in v0.4.0

func (f *Func) Doc() string

func (*Func) IsMethod added in v0.4.0

func (f *Func) IsMethod() bool

func (*Func) IsVariadic added in v0.4.0

func (f *Func) IsVariadic() bool

func (*Func) Name added in v0.4.0

func (f *Func) Name() string

func (*Func) Pos added in v0.4.2

func (f *Func) Pos() token.Pos

func (*Func) Recv added in v0.4.0

func (f *Func) Recv() string

func (*Func) Returns added in v0.4.0

func (f *Func) Returns() VarList

func (*Func) String added in v0.4.0

func (f *Func) String() string

type ID added in v0.4.0

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

type Interface

type Interface struct {
	Shape *Shape
	// contains filtered or unexported fields
}

func (*Interface) Doc added in v0.4.0

func (iface *Interface) Doc() string

func (*Interface) Methods

func (iface *Interface) Methods() VarList

func (*Interface) Name added in v0.4.0

func (iface *Interface) Name() string

func (*Interface) Pos added in v0.4.2

func (iface *Interface) Pos() token.Pos

func (*Interface) String added in v0.4.0

func (iface *Interface) String() string

type Named added in v0.4.0

type Named struct {
	Shape *Shape
	// contains filtered or unexported fields
}

func (*Named) Doc added in v0.4.0

func (t *Named) Doc() string

func (*Named) Name added in v0.4.0

func (t *Named) Name() string

func (*Named) Pos added in v0.4.2

func (t *Named) Pos() token.Pos

func (*Named) String added in v0.4.0

func (t *Named) String() string

type Package added in v0.4.0

type Package struct {
	Name string
	Path string
	// contains filtered or unexported fields
}

func (*Package) Scope added in v0.4.0

func (p *Package) Scope() *Scope

type Scope added in v0.4.0

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

func (*Scope) Names added in v0.4.0

func (s *Scope) Names() []string

func (*Scope) NamesWithMethod added in v0.4.0

func (s *Scope) NamesWithMethod() []string

type Shape

type Shape struct {
	Name     string
	Kind     reflect.Kind
	IsMethod bool

	ID           ID
	Type         reflect.Type
	DefaultValue reflect.Value

	Number  int // If all shapes are from the same extractor, this value can be used as ID
	Lv      int // pointer level. v is 0, *v is 1.
	Package *Package
	// contains filtered or unexported fields
}

func (*Shape) Equal added in v0.4.0

func (s *Shape) Equal(another *Shape) bool

func (*Shape) FullName added in v0.4.0

func (s *Shape) FullName() string

func (*Shape) Func added in v0.4.0

func (s *Shape) Func() *Func

func (*Shape) Interface added in v0.4.0

func (s *Shape) Interface() *Interface

func (*Shape) Named added in v0.4.0

func (s *Shape) Named() *Named

func (*Shape) String added in v0.4.0

func (s *Shape) String() string

func (*Shape) Struct added in v0.4.0

func (s *Shape) Struct() *Struct

type Struct

type Struct struct {
	Shape *Shape
	// contains filtered or unexported fields
}

func (*Struct) Doc added in v0.4.0

func (s *Struct) Doc() string

func (*Struct) Fields

func (s *Struct) Fields() FieldList

func (*Struct) Name added in v0.4.0

func (s *Struct) Name() string

func (*Struct) Pos added in v0.4.2

func (s *Struct) Pos() token.Pos

func (*Struct) String added in v0.4.0

func (s *Struct) String() string

type Var added in v0.4.0

type Var struct {
	Name  string
	Shape *Shape
	Doc   string
}

func (*Var) String added in v0.4.0

func (v *Var) String() string

type VarList added in v0.4.0

type VarList []*Var

func (VarList) Len added in v0.4.0

func (vl VarList) Len() int

func (VarList) String added in v0.4.0

func (vl VarList) String() string

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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