2021-05-22 14:14:59 +00:00
|
|
|
package beeParser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2021-05-23 14:04:23 +00:00
|
|
|
type sampleFormatter struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *sampleFormatter) Format(field *StructField) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-05-23 02:34:39 +00:00
|
|
|
func ExampleStructParser() {
|
2021-05-22 14:14:59 +00:00
|
|
|
const src = `
|
|
|
|
package p
|
2021-05-23 02:34:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
)
|
2021-05-22 14:14:59 +00:00
|
|
|
|
|
|
|
type StructB struct {
|
|
|
|
Field1 string
|
|
|
|
}
|
|
|
|
type StructA struct {
|
|
|
|
Field1 string
|
2021-05-23 02:34:39 +00:00
|
|
|
Field2 struct{
|
|
|
|
a string
|
|
|
|
b string
|
|
|
|
}
|
2021-05-22 14:14:59 +00:00
|
|
|
Field3 []string
|
|
|
|
Field4 map[string]string
|
|
|
|
Field5 http.SameSite
|
2021-05-23 02:34:39 +00:00
|
|
|
Field6 func(int)
|
|
|
|
Field7 StructB
|
2021-05-22 14:14:59 +00:00
|
|
|
}
|
|
|
|
`
|
2021-05-23 14:04:23 +00:00
|
|
|
formatter := &sampleFormatter{}
|
|
|
|
|
|
|
|
cg, err := NewStructParser("src.go", src, "StructA", formatter)
|
2021-05-22 14:14:59 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := cg.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(b))
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// {
|
2021-05-23 02:34:39 +00:00
|
|
|
// "Field1": "",
|
|
|
|
// "Field2": {
|
|
|
|
// "a": "",
|
|
|
|
// "b": ""
|
|
|
|
// },
|
|
|
|
// "Field3": "",
|
|
|
|
// "Field4": "",
|
|
|
|
// "Field5": "",
|
|
|
|
// "Field6": "",
|
|
|
|
// "Field7": {
|
|
|
|
// "Field1": ""
|
|
|
|
// }
|
2021-05-22 14:14:59 +00:00
|
|
|
// }
|
|
|
|
}
|