mirror of
https://github.com/beego/bee.git
synced 2024-11-21 18:40:54 +00:00
Merge pull request #785 from Codexiaoyi/parse-config
add annotator as a field formatter by annotation
This commit is contained in:
commit
b91a3a732d
@ -5,13 +5,8 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Annotator interface {
|
||||
Annotate(string) []map[string]interface{}
|
||||
AnnotateToJson(string) (string, error)
|
||||
}
|
||||
|
||||
type Annotation struct {
|
||||
}
|
||||
// field formatter by annotation
|
||||
type Annotator struct{}
|
||||
|
||||
func isWhitespace(ch byte) bool { return ch == ' ' || ch == '\t' || ch == '\r' }
|
||||
|
||||
@ -44,7 +39,7 @@ func handleWhitespaceValues(values []string) []string {
|
||||
|
||||
//parse annotation to generate array with key and values
|
||||
//start with "@" as a key-value pair,key and values are separated by a space,wrap to distinguish values.
|
||||
func (a *Annotation) Annotate(comment string) []map[string]interface{} {
|
||||
func (a *Annotator) Annotate(comment string) []map[string]interface{} {
|
||||
results := make([]map[string]interface{}, 0)
|
||||
//split annotation with '@'
|
||||
lines := strings.Split(comment, "@")
|
||||
@ -61,8 +56,19 @@ func (a *Annotation) Annotate(comment string) []map[string]interface{} {
|
||||
}
|
||||
|
||||
//parse annotation to json
|
||||
func (a *Annotation) AnnotateToJson(comment string) (string, error) {
|
||||
func (a *Annotator) AnnotateToJson(comment string) (string, error) {
|
||||
if comment == "" {
|
||||
return "", nil
|
||||
}
|
||||
annotate := a.Annotate(comment)
|
||||
if len(annotate) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
result, err := json.MarshalIndent(annotate, "", " ")
|
||||
return string(result), err
|
||||
}
|
||||
|
||||
func (a *Annotator) Format(field *StructField) string {
|
||||
f, _ := a.AnnotateToJson(field.Doc)
|
||||
return f
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var BeeAnnotator Annotator
|
||||
var BeeAnnotator *Annotator
|
||||
|
||||
const (
|
||||
Annotation1 = `
|
||||
@ -25,7 +25,7 @@ https://github.com/beego
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
BeeAnnotator = &Annotation{}
|
||||
BeeAnnotator = &Annotator{}
|
||||
retCode := m.Run() //run test
|
||||
os.Exit(retCode)
|
||||
}
|
||||
@ -50,7 +50,7 @@ func TestAnnotate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAnnotateToJson(t *testing.T) {
|
||||
expect := `[
|
||||
expect1 := `[
|
||||
{
|
||||
"Name": [
|
||||
"Field1"
|
||||
@ -69,9 +69,11 @@ func TestAnnotateToJson(t *testing.T) {
|
||||
}
|
||||
]`
|
||||
|
||||
actual, _ := BeeAnnotator.AnnotateToJson(Annotation1)
|
||||
actual1, _ := BeeAnnotator.AnnotateToJson(Annotation1)
|
||||
actual2, _ := BeeAnnotator.AnnotateToJson("")
|
||||
|
||||
assert.Equal(t, expect, actual)
|
||||
assert.Equal(t, expect1, actual1)
|
||||
assert.Equal(t, "", actual2)
|
||||
}
|
||||
|
||||
func TestHandleWhitespaceValues(t *testing.T) {
|
||||
|
@ -3,15 +3,11 @@ package beeParser
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type sampleFormatter struct {
|
||||
}
|
||||
|
||||
func (f *sampleFormatter) Format(field *StructField) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func ExampleStructParser() {
|
||||
const src = `
|
||||
package p
|
||||
@ -36,9 +32,9 @@ type StructA struct {
|
||||
Field7 StructB
|
||||
}
|
||||
`
|
||||
formatter := &sampleFormatter{}
|
||||
annotator := &Annotator{}
|
||||
|
||||
sp, err := NewStructParser("src.go", src, "StructA", formatter)
|
||||
sp, err := NewStructParser("src.go", src, "StructA", annotator)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -66,3 +62,41 @@ type StructA struct {
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
func TestParseStructByFieldAnnotation(t *testing.T) {
|
||||
const src = `
|
||||
package p
|
||||
|
||||
type StructA struct {
|
||||
//@Name Field1
|
||||
//@DefaultValues bee test
|
||||
// beego test
|
||||
Field1 string
|
||||
}
|
||||
`
|
||||
|
||||
expect := `[
|
||||
{
|
||||
"Name": [
|
||||
"Field1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"DefaultValues": [
|
||||
"bee test",
|
||||
"beego test"
|
||||
]
|
||||
}
|
||||
]`
|
||||
|
||||
annotator := &Annotator{}
|
||||
|
||||
sp, err := NewStructParser("src.go", src, "StructA", annotator)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
actual := sp.FieldFormatter.Format(sp.MainStruct.Fields[0])
|
||||
|
||||
assert.Equal(t, expect, actual)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user