mirror of
https://github.com/beego/bee.git
synced 2024-11-21 23:50:54 +00:00
supports field formatter
This commit is contained in:
parent
b168005893
commit
e0e24c8e7f
@ -10,6 +10,11 @@ import (
|
|||||||
"go/types"
|
"go/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// FieldFormatter transfers the field value to expected format
|
||||||
|
type FieldFormatter interface {
|
||||||
|
Format(field *StructField) string
|
||||||
|
}
|
||||||
|
|
||||||
// StructField defines struct field
|
// StructField defines struct field
|
||||||
type StructField struct {
|
type StructField struct {
|
||||||
Name string
|
Name string
|
||||||
@ -18,6 +23,7 @@ type StructField struct {
|
|||||||
Comment string
|
Comment string
|
||||||
Doc string
|
Doc string
|
||||||
Tag string
|
Tag string
|
||||||
|
FormatFunc func(field *StructField) string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Key returns the key of the field
|
// Key returns the key of the field
|
||||||
@ -32,7 +38,7 @@ func (sf *StructField) Value() interface{} {
|
|||||||
return sf.NestedType.ToKV()
|
return sf.NestedType.ToKV()
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return sf.FormatFunc(sf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StructNode defines struct node
|
// StructNode defines struct node
|
||||||
@ -52,15 +58,16 @@ func (sn *StructNode) ToKV() map[string]interface{} {
|
|||||||
|
|
||||||
// StructParser parses structs in given file or string
|
// StructParser parses structs in given file or string
|
||||||
type StructParser struct {
|
type StructParser struct {
|
||||||
MainStruct *StructNode
|
MainStruct *StructNode
|
||||||
Info types.Info
|
Info types.Info
|
||||||
|
FieldFormatter FieldFormatter
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStructParser is the constructor of StructParser
|
// NewStructParser is the constructor of StructParser
|
||||||
// filePath and src follow the same rule with go/parser.ParseFile
|
// filePath and src follow the same rule with go/parser.ParseFile
|
||||||
// If src != nil, ParseFile parses the source from src and the filename is only used when recording position information. The type of the argument for the src parameter must be string, []byte, or io.Reader. If src == nil, ParseFile parses the file specified by filename.
|
// If src != nil, ParseFile parses the source from src and the filename is only used when recording position information. The type of the argument for the src parameter must be string, []byte, or io.Reader. If src == nil, ParseFile parses the file specified by filename.
|
||||||
// rootStruct is the root struct we want to use
|
// rootStruct is the root struct we want to use
|
||||||
func NewStructParser(filePath string, src interface{}, rootStruct string) (*StructParser, error) {
|
func NewStructParser(filePath string, src interface{}, rootStruct string, formatter FieldFormatter) (*StructParser, error) {
|
||||||
fset := token.NewFileSet()
|
fset := token.NewFileSet()
|
||||||
f, err := parser.ParseFile(fset, filePath, src, parser.ParseComments)
|
f, err := parser.ParseFile(fset, filePath, src, parser.ParseComments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -81,7 +88,8 @@ func NewStructParser(filePath string, src interface{}, rootStruct string) (*Stru
|
|||||||
}
|
}
|
||||||
|
|
||||||
cg := &StructParser{
|
cg := &StructParser{
|
||||||
Info: info,
|
FieldFormatter: formatter,
|
||||||
|
Info: info,
|
||||||
}
|
}
|
||||||
|
|
||||||
ast.Inspect(f, func(n ast.Node) bool {
|
ast.Inspect(f, func(n ast.Node) bool {
|
||||||
@ -165,6 +173,7 @@ func (cg *StructParser) ParseField(field *ast.Field) *StructField {
|
|||||||
Comment: fieldComment,
|
Comment: fieldComment,
|
||||||
Doc: fieldDoc,
|
Doc: fieldDoc,
|
||||||
NestedType: nestedStruct,
|
NestedType: nestedStruct,
|
||||||
|
FormatFunc: cg.FieldFormatter.Format,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,13 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type sampleFormatter struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *sampleFormatter) Format(field *StructField) string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleStructParser() {
|
func ExampleStructParser() {
|
||||||
const src = `
|
const src = `
|
||||||
package p
|
package p
|
||||||
@ -28,9 +35,10 @@ type StructA struct {
|
|||||||
Field6 func(int)
|
Field6 func(int)
|
||||||
Field7 StructB
|
Field7 StructB
|
||||||
}
|
}
|
||||||
|
|
||||||
`
|
`
|
||||||
cg, err := NewStructParser("src.go", src, "StructA")
|
formatter := &sampleFormatter{}
|
||||||
|
|
||||||
|
cg, err := NewStructParser("src.go", src, "StructA", formatter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user