1
0
mirror of https://github.com/beego/bee.git synced 2024-11-14 22:10:55 +00:00
bee/parser/formatter.go

90 lines
2.2 KiB
Go
Raw Normal View History

2021-05-25 02:53:12 +00:00
package beeParser
2021-06-25 15:15:46 +00:00
import (
"encoding/json"
2021-06-28 17:05:17 +00:00
"io/ioutil"
"strings"
2021-05-25 02:53:12 +00:00
"github.com/talos-systems/talos/pkg/machinery/config/encoder"
2021-06-25 15:15:46 +00:00
)
type JsonFormatter struct {
}
func (f *JsonFormatter) FieldFormatFunc(field *StructField) ([]byte, error) {
2021-06-28 17:05:17 +00:00
annotation := NewAnnotation(field.Doc+field.Comment, field.Name, field.Type)
2021-06-25 15:15:46 +00:00
res := map[string]interface{}{}
if field.NestedType != nil {
res[annotation.Key] = field.NestedType
} else {
res[annotation.Key] = annotation.Default
}
return json.Marshal(res)
}
func (f *JsonFormatter) StructFormatFunc(node *StructNode) ([]byte, error) {
return json.Marshal(node.Fields)
}
func (f *JsonFormatter) Marshal(node *StructNode) ([]byte, error) {
return json.MarshalIndent(node, "", " ")
}
type YamlFormatter struct {
}
var result encoder.Doc
2021-06-25 15:15:46 +00:00
type Result map[string]interface{}
2021-06-25 15:15:46 +00:00
func (c Result) Doc() *encoder.Doc {
return &result
2021-06-25 15:15:46 +00:00
}
2021-06-28 17:05:17 +00:00
func (f *YamlFormatter) FieldFormatFunc(field *StructField) ([]byte, error) {
2021-06-28 17:05:17 +00:00
annotation := NewAnnotation(field.Doc+field.Comment, field.Name, field.Type)
res := Result{}
// add head comment for this field
res.Doc().Comments[encoder.HeadComment] = annotation.Description
2021-06-25 15:15:46 +00:00
if field.NestedType != nil {
2021-06-28 17:05:17 +00:00
// nestedType format result as this field value
b, err := field.NestedType.FormatFunc(field.NestedType)
if err != nil {
return nil, err
}
res[annotation.Key] = string(b)
2021-06-25 15:15:46 +00:00
} else {
res[annotation.Key] = annotation.Default
2021-06-25 15:15:46 +00:00
}
2021-06-28 17:05:17 +00:00
encoder := encoder.NewEncoder(&res, []encoder.Option{
encoder.WithComments(encoder.CommentsAll),
}...)
2021-06-28 17:05:17 +00:00
encodeByte, err := encoder.Encode()
if err != nil {
return nil, err
}
// when field.NestedType != nil, the key and nested value strings are encoded with "|"
// remove "|" by string replace
encodeByte = []byte(strings.Replace(string(encodeByte), annotation.Key+": |", annotation.Key+":", 1))
return encodeByte, nil
2021-05-25 02:53:12 +00:00
}
func (f *YamlFormatter) StructFormatFunc(node *StructNode) ([]byte, error) {
2021-06-25 15:15:46 +00:00
res := make([]byte, 0)
for _, f := range node.Fields {
b, _ := f.FormatFunc(f)
res = append(res, b...)
2021-05-25 02:53:12 +00:00
}
2021-06-25 15:15:46 +00:00
return res, nil
2021-05-25 02:53:12 +00:00
}
func (f *YamlFormatter) Marshal(node *StructNode) ([]byte, error) {
2021-06-28 17:05:17 +00:00
res, err := node.FormatFunc(node)
if err != nil {
return nil, err
}
ioutil.WriteFile(node.Name+".yaml", res, 0667)
return res, nil
2021-05-25 02:53:12 +00:00
}