github.com/podhmo/reflect-shape@v0.4.3/example_test.go (about)

     1  package reflectshape_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	reflectshape "github.com/podhmo/reflect-shape"
     7  )
     8  
     9  // User is the object for User.
    10  type User struct {
    11  	// name of User.
    12  	Name string
    13  	Age  int // age of User.
    14  }
    15  
    16  // Hello function.
    17  func Hello(user *User /* greeting target */) {
    18  	fmt.Println("Hello", user.Name)
    19  }
    20  
    21  func ExampleConfig() {
    22  	cfg := reflectshape.Config{IncludeGoTestFiles: true}
    23  	shape := cfg.Extract(User{})
    24  
    25  	fmt.Printf("%s %s %s %q\n", shape.Name, shape.Kind, shape.Package.Path, shape.Struct().Doc())
    26  	for _, f := range shape.Struct().Fields() {
    27  		fmt.Printf("-- %s %q\n", f.Name, f.Doc)
    28  	}
    29  
    30  	shape2 := cfg.Extract(Hello)
    31  	fmt.Printf("%s %s %s %q\n", shape2.Name, shape2.Kind, shape2.Package.Path, shape2.Func().Doc())
    32  	for _, a := range shape2.Func().Args() {
    33  		fmt.Printf("-- %s %q\n", a.Name, a.Doc)
    34  	}
    35  
    36  	// Output:
    37  	// User struct github.com/podhmo/reflect-shape_test "User is the object for User."
    38  	// -- Name "name of User."
    39  	// -- Age "age of User."
    40  	// Hello func github.com/podhmo/reflect-shape_test "Hello function."
    41  	// -- user "greeting target"
    42  }