From d6545036aeeb130a1c71fc5907b9f90be9acb021 Mon Sep 17 00:00:00 2001 From: Codexiaoyi Date: Tue, 25 May 2021 10:53:12 +0800 Subject: [PATCH] add formatter to format field --- parser/annotator.go | 21 +++++-------------- parser/annotator_test.go | 45 +++++++--------------------------------- parser/formatter.go | 20 ++++++++++++++++++ parser/formatter_test.go | 33 +++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 54 deletions(-) create mode 100644 parser/formatter.go create mode 100644 parser/formatter_test.go diff --git a/parser/annotator.go b/parser/annotator.go index 4822e4b..022c3d6 100644 --- a/parser/annotator.go +++ b/parser/annotator.go @@ -1,13 +1,11 @@ package beeParser import ( - "encoding/json" "strings" ) type Annotator interface { - Annotate(string) []map[string]interface{} - AnnotateToJson(string) (string, error) + Annotate(string) map[string]interface{} } type Annotation struct { @@ -44,25 +42,16 @@ 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{} { - results := make([]map[string]interface{}, 0) +func (a *Annotation) Annotate(annotation string) map[string]interface{} { + results := make(map[string]interface{}) //split annotation with '@' - lines := strings.Split(comment, "@") + lines := strings.Split(annotation, "@") //skip first line whitespace for _, line := range lines[1:] { kvs := strings.Split(line, " ") key := kvs[0] values := strings.Split(strings.TrimSpace(line[len(kvs[0]):]), "\n") - annotation := make(map[string]interface{}) - annotation[key] = handleWhitespaceValues(values) - results = append(results, annotation) + results[key] = handleWhitespaceValues(values) } return results } - -//parse annotation to json -func (a *Annotation) AnnotateToJson(comment string) (string, error) { - annotate := a.Annotate(comment) - result, err := json.MarshalIndent(annotate, "", " ") - return string(result), err -} diff --git a/parser/annotator_test.go b/parser/annotator_test.go index 9436403..d3d9ea5 100644 --- a/parser/annotator_test.go +++ b/parser/annotator_test.go @@ -31,15 +31,15 @@ func TestMain(m *testing.M) { } func TestAnnotate(t *testing.T) { - expect1 := []map[string]interface{}{ - {"Name": []string{"Field1"}}, - {"Type": []string{"string"}}, - {"Path": []string{"https://github.com/beego/bee", "https://github.com/beego"}}, + expect1 := map[string]interface{}{ + "Name": []string{"Field1"}, + "Type": []string{"string"}, + "Path": []string{"https://github.com/beego/bee", "https://github.com/beego"}, } - expect2 := []map[string]interface{}{ - {"Number": []string{"2"}}, - {"Projects": []string{"https://github.com/beego/bee", "", "https://github.com/beego"}}, + expect2 := map[string]interface{}{ + "Number": []string{"2"}, + "Projects": []string{"https://github.com/beego/bee", "", "https://github.com/beego"}, } actual := BeeAnnotator.Annotate(Annotation1) @@ -49,31 +49,6 @@ func TestAnnotate(t *testing.T) { assert.Equal(t, expect2, actual2) } -func TestAnnotateToJson(t *testing.T) { - expect := `[ - { - "Name": [ - "Field1" - ] - }, - { - "Type": [ - "string" - ] - }, - { - "Path": [ - "https://github.com/beego/bee", - "https://github.com/beego" - ] - } -]` - - actual, _ := BeeAnnotator.AnnotateToJson(Annotation1) - - assert.Equal(t, expect, actual) -} - func TestHandleWhitespaceValues(t *testing.T) { src := []string{ " beego", @@ -100,9 +75,3 @@ func BenchmarkAnnotate(b *testing.B) { BeeAnnotator.Annotate(Annotation1) } } - -func BenchmarkAnnotateToJson(b *testing.B) { - for i := 0; i < b.N; i++ { - BeeAnnotator.AnnotateToJson(Annotation1) - } -} diff --git a/parser/formatter.go b/parser/formatter.go new file mode 100644 index 0000000..f50926d --- /dev/null +++ b/parser/formatter.go @@ -0,0 +1,20 @@ +package beeParser + +import "encoding/json" + +type AnnotationFormatter struct { + Annotation Annotator +} + +func (f *AnnotationFormatter) Format(field *StructField) string { + if field.Comment == "" && field.Doc == "" { + return "" + } + kvs := f.Annotation.Annotate(field.Doc + field.Comment) + res, _ := json.Marshal(kvs) + return string(res) +} + +func NewAnnotationFormatter() *AnnotationFormatter { + return &AnnotationFormatter{Annotation: &Annotation{}} +} diff --git a/parser/formatter_test.go b/parser/formatter_test.go new file mode 100644 index 0000000..11b6312 --- /dev/null +++ b/parser/formatter_test.go @@ -0,0 +1,33 @@ +package beeParser + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFormat(t *testing.T) { + except := `{ + "Name": [ + "Field1" + ], + "Path":[ + "https://github.com/beego/bee", + "https://github.com/beego" + ], + "test":[ + "test comment" + ] + }` + + field := &StructField{ + Comment: "@test test comment", + Doc: `@Name Field1 + @Path https://github.com/beego/bee + https://github.com/beego`, + } + + actual := NewAnnotationFormatter().Format(field) + + assert.JSONEq(t, except, actual) +}