mirror of
https://github.com/beego/bee.git
synced 2025-06-14 03:10:40 +00:00
fix yaml formatter,delete xml formatter
This commit is contained in:
@ -75,6 +75,9 @@ func (a *Annotation) Annotate(annotation string) map[string]interface{} {
|
||||
return results
|
||||
}
|
||||
|
||||
//create new annotation
|
||||
//parse "Key","Default","Description" by annotation
|
||||
//the type of "Key" and "Description" is string, "Default" is interface{}
|
||||
func NewAnnotation(annotation string) *Annotation {
|
||||
a := &Annotation{}
|
||||
kvs := a.Annotate(annotation)
|
||||
|
@ -2,9 +2,8 @@ package beeParser
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
"github.com/talos-systems/talos/pkg/machinery/config/encoder"
|
||||
)
|
||||
|
||||
type JsonFormatter struct {
|
||||
@ -12,6 +11,9 @@ type JsonFormatter struct {
|
||||
|
||||
func (f *JsonFormatter) FieldFormatFunc(field *StructField) ([]byte, error) {
|
||||
annotation := NewAnnotation(field.Doc + field.Comment)
|
||||
if annotation.Key == "" {
|
||||
annotation.Key = field.Name
|
||||
}
|
||||
res := map[string]interface{}{}
|
||||
if field.NestedType != nil {
|
||||
res[annotation.Key] = field.NestedType
|
||||
@ -32,66 +34,42 @@ func (f *JsonFormatter) Marshal(node *StructNode) ([]byte, error) {
|
||||
type YamlFormatter struct {
|
||||
}
|
||||
|
||||
var result encoder.Doc
|
||||
|
||||
type Result map[string]interface{}
|
||||
|
||||
func (c Result) Doc() *encoder.Doc {
|
||||
return &result
|
||||
}
|
||||
func (f *YamlFormatter) FieldFormatFunc(field *StructField) ([]byte, error) {
|
||||
annotation := NewAnnotation(field.Doc + field.Comment)
|
||||
res := map[string]interface{}{}
|
||||
if annotation.Key == "" {
|
||||
annotation.Key = field.Name
|
||||
}
|
||||
res := Result{}
|
||||
// add head comment for this field
|
||||
res.Doc().Comments[encoder.HeadComment] = annotation.Description
|
||||
if field.NestedType != nil {
|
||||
res[annotation.Key] = field.NestedType
|
||||
b, _ := field.NestedType.FormatFunc(field.NestedType)
|
||||
res[annotation.Key] = string(b)
|
||||
} else {
|
||||
res[annotation.Key] = annotation.Default
|
||||
}
|
||||
return yaml.Marshal(res)
|
||||
encoder := encoder.NewEncoder(&res, []encoder.Option{
|
||||
encoder.WithComments(encoder.CommentsAll),
|
||||
}...)
|
||||
return encoder.Encode()
|
||||
}
|
||||
|
||||
func (f *YamlFormatter) StructFormatFunc(node *StructNode) ([]byte, error) {
|
||||
return yaml.Marshal(node.Fields)
|
||||
}
|
||||
|
||||
func (f *YamlFormatter) Marshal(node *StructNode) ([]byte, error) {
|
||||
return yaml.Marshal(node)
|
||||
}
|
||||
|
||||
type XmlFormatter struct {
|
||||
}
|
||||
|
||||
func (f *XmlFormatter) FieldFormatFunc(field *StructField) ([]byte, error) {
|
||||
annotation := NewAnnotation(field.Doc + field.Comment)
|
||||
if field.NestedType != nil {
|
||||
type xmlStruct struct {
|
||||
XMLName xml.Name
|
||||
Default interface{} `xml:",innerxml"`
|
||||
Description string `xml:",comment"`
|
||||
}
|
||||
b, _ := field.NestedType.FormatFunc(field.NestedType)
|
||||
return xml.Marshal(&xmlStruct{
|
||||
XMLName: xml.Name{Local: annotation.Key},
|
||||
Description: annotation.Description,
|
||||
Default: b,
|
||||
})
|
||||
} else {
|
||||
type xmlStruct struct {
|
||||
XMLName xml.Name
|
||||
Default interface{} `xml:",chardata"`
|
||||
Description string `xml:",comment"`
|
||||
}
|
||||
return xml.Marshal(&xmlStruct{
|
||||
XMLName: xml.Name{Local: annotation.Key},
|
||||
Description: annotation.Description,
|
||||
Default: annotation.Default,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (f *XmlFormatter) StructFormatFunc(node *StructNode) ([]byte, error) {
|
||||
res := make([]byte, 0)
|
||||
for _, f := range node.Fields {
|
||||
b, _ := f.FormatFunc(f)
|
||||
res = append(res, b...)
|
||||
res = append(res, '\n')
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (f *XmlFormatter) Marshal(node *StructNode) ([]byte, error) {
|
||||
func (f *YamlFormatter) Marshal(node *StructNode) ([]byte, error) {
|
||||
return node.FormatFunc(node)
|
||||
}
|
||||
|
@ -8,27 +8,36 @@ import (
|
||||
const src = `
|
||||
package p
|
||||
|
||||
type StructB struct {
|
||||
// @Key FieldB1
|
||||
FieldB1 interface{}
|
||||
}
|
||||
|
||||
type StructA struct {
|
||||
// @Key Field1
|
||||
// @Default test
|
||||
// @Description ddddddd
|
||||
// @Description comment of field1
|
||||
Field1 string
|
||||
// @Key Field2
|
||||
// @Description comment of field2
|
||||
Field2 struct{
|
||||
// @Key a
|
||||
// @Default https://github.com/beego/bee
|
||||
// https://github.com/beego
|
||||
// @Description comment of a of field2
|
||||
a string
|
||||
// @Key b
|
||||
// @Default https://github.com/beego/bee https://github.com/beego
|
||||
b string
|
||||
// @Description comment of b of field2
|
||||
b map[int]string
|
||||
}
|
||||
// @Key Field3
|
||||
// @Default 1
|
||||
// @Description comment of field3
|
||||
Field3 int
|
||||
// @Key Field4
|
||||
// @Default false
|
||||
Field4 bool
|
||||
// @Key NestField
|
||||
// @Description comment of NestField
|
||||
NestField StructB
|
||||
}
|
||||
`
|
||||
|
||||
@ -64,10 +73,17 @@ func ExampleJsonFormatter() {
|
||||
// ]
|
||||
// },
|
||||
// {
|
||||
// "Field3": 1
|
||||
// "Field3": null
|
||||
// },
|
||||
// {
|
||||
// "Field4": false
|
||||
// },
|
||||
// {
|
||||
// "NestField": [
|
||||
// {
|
||||
// "FieldB1": null
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
//]
|
||||
}
|
||||
@ -86,41 +102,19 @@ func ExampleYamlFormatter() {
|
||||
fmt.Println(string(b))
|
||||
|
||||
// Output:
|
||||
//|
|
||||
// - |
|
||||
// Field1: test
|
||||
// - |
|
||||
// Field2: |
|
||||
// - |
|
||||
// a:
|
||||
// - https://github.com/beego/bee
|
||||
// - https://github.com/beego
|
||||
// - |
|
||||
// b: https://github.com/beego/bee https://github.com/beego
|
||||
// - |
|
||||
// Field3: 1
|
||||
// - |
|
||||
// Field4: false
|
||||
}
|
||||
|
||||
func ExampleXmlFormatter() {
|
||||
sp, err := NewStructParser("src.go", src, "StructA", &XmlFormatter{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
b, err := sp.Marshal()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(b))
|
||||
|
||||
// Output:
|
||||
//<Field1>test<!--ddddddd--></Field1>
|
||||
//<Field2><a></a>
|
||||
//<b>https://github.com/beego/bee https://github.com/beego</b>
|
||||
//</Field2>
|
||||
//<Field3>1</Field3>
|
||||
//<Field4>false</Field4>
|
||||
// # comment of field1
|
||||
// Field1: test
|
||||
// # comment of b of field2
|
||||
// Field2: |
|
||||
// # comment of a of field2
|
||||
// a:
|
||||
// - https://github.com/beego/bee
|
||||
// - https://github.com/beego
|
||||
// # comment of b of field2
|
||||
// b: https://github.com/beego/bee https://github.com/beego
|
||||
// # comment of field3
|
||||
// Field3: null
|
||||
// Field4: false
|
||||
// NestField: |
|
||||
// FieldB1: null
|
||||
}
|
||||
|
Reference in New Issue
Block a user