bee/parser/parser_test.go

86 lines
2.7 KiB
Go
Raw Normal View History

2021-05-22 14:14:59 +00:00
package beeParser
import (
"encoding/json"
2021-05-22 14:14:59 +00:00
"fmt"
"log"
)
2021-05-23 14:04:23 +00:00
type sampleFormatter struct {
Annotation Annotation
}
func (f *sampleFormatter) FieldFormatFunc(field *StructField) ([]byte, error) {
// @todo update annotationResult by parsing with annotation struct
annotationResult := field.Comment + field.Doc
return json.Marshal(&struct {
Key string
Annotation string
NestedType *StructNode `json:"NestedType,omitempty"`
}{
Key: field.Name,
Annotation: annotationResult,
NestedType: field.NestedType,
})
}
func (f *sampleFormatter) StructFormatFunc(node *StructNode) ([]byte, error) {
return json.Marshal(&struct {
Key string
Fields []*StructField `json:"Fields,omitempty"`
}{
Key: node.Name,
Fields: node.Fields,
})
}
func (f *sampleFormatter) Marshal(node *StructNode) ([]byte, error) {
return json.Marshal(node)
}
func ExampleJSONMarshal() {
2021-05-22 14:14:59 +00:00
const src = `
package p
import (
"net/http"
)
2021-05-22 14:14:59 +00:00
type StructB struct {
Field1 string
}
type StructA struct {
// doc
Field1 string //comment
// @Name Field1
// @Path https://github.com/beego/bee
// https://github.com/beego
Field2 struct{
a string
b string
}
2021-05-22 14:14:59 +00:00
Field3 []string
Field4 map[string]string
Field5 http.SameSite
Field6 func(int)
Field7 StructB
2021-05-22 14:14:59 +00:00
}
`
formatter := &sampleFormatter{}
2021-05-23 14:04:23 +00:00
sp, err := NewStructParser("src.go", src, "StructA", formatter)
2021-05-22 14:14:59 +00:00
if err != nil {
log.Fatal(err)
}
b, err := sp.Marshal()
2021-05-22 14:14:59 +00:00
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
// Output:
2021-06-13 02:20:47 +00:00
// "{\"Key\":\"StructA\",\"Fields\":[\"{\\\"Key\\\":\\\"Field1\\\",\\\"Annotation\\\":\\\"comment\\\\ndoc\\\\n\\\"}\",\"{\\\"Key\\\":\\\"Field2\\\",\\\"Annotation\\\":\\\"@Name Field1\\\\n@Path https://github.com/beego/bee\\\\n\\\\t\\\\t https://github.com/beego\\\\n\\\",\\\"NestedType\\\":\\\"{\\\\\\\"Key\\\\\\\":\\\\\\\"\\\\\\\",\\\\\\\"Fields\\\\\\\":[\\\\\\\"{\\\\\\\\\\\\\\\"Key\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"a\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"Annotation\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\"}\\\\\\\",\\\\\\\"{\\\\\\\\\\\\\\\"Key\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"b\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"Annotation\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\"}\\\\\\\"]}\\\"}\",\"{\\\"Key\\\":\\\"Field3\\\",\\\"Annotation\\\":\\\"\\\"}\",\"{\\\"Key\\\":\\\"Field4\\\",\\\"Annotation\\\":\\\"\\\"}\",\"{\\\"Key\\\":\\\"Field5\\\",\\\"Annotation\\\":\\\"\\\"}\",\"{\\\"Key\\\":\\\"Field6\\\",\\\"Annotation\\\":\\\"\\\"}\",\"{\\\"Key\\\":\\\"Field7\\\",\\\"Annotation\\\":\\\"\\\",\\\"NestedType\\\":\\\"{\\\\\\\"Key\\\\\\\":\\\\\\\"StructB\\\\\\\",\\\\\\\"Fields\\\\\\\":[\\\\\\\"{\\\\\\\\\\\\\\\"Key\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"Field1\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"Annotation\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\"}\\\\\\\"]}\\\"}\"]}"
2021-05-22 14:14:59 +00:00
}