From 64245c87fc14bf5c715d90c9212b0a80caf69ff6 Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Mon, 24 May 2021 23:18:14 +0800 Subject: [PATCH] Revert "add annotator as a field formatter by annotation" --- parser/annotator.go | 24 +++++++------------ parser/annotator_test.go | 12 ++++------ parser/parser_test.go | 52 +++++++--------------------------------- 3 files changed, 23 insertions(+), 65 deletions(-) diff --git a/parser/annotator.go b/parser/annotator.go index 4ea8382..4822e4b 100644 --- a/parser/annotator.go +++ b/parser/annotator.go @@ -5,8 +5,13 @@ import ( "strings" ) -// field formatter by annotation -type Annotator struct{} +type Annotator interface { + Annotate(string) []map[string]interface{} + AnnotateToJson(string) (string, error) +} + +type Annotation struct { +} func isWhitespace(ch byte) bool { return ch == ' ' || ch == '\t' || ch == '\r' } @@ -39,7 +44,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 *Annotator) Annotate(comment string) []map[string]interface{} { +func (a *Annotation) Annotate(comment string) []map[string]interface{} { results := make([]map[string]interface{}, 0) //split annotation with '@' lines := strings.Split(comment, "@") @@ -56,19 +61,8 @@ func (a *Annotator) Annotate(comment string) []map[string]interface{} { } //parse annotation to json -func (a *Annotator) AnnotateToJson(comment string) (string, error) { - if comment == "" { - return "", nil - } +func (a *Annotation) AnnotateToJson(comment string) (string, error) { 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 -} diff --git a/parser/annotator_test.go b/parser/annotator_test.go index 6ef10fa..9436403 100644 --- a/parser/annotator_test.go +++ b/parser/annotator_test.go @@ -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 = &Annotator{} + BeeAnnotator = &Annotation{} retCode := m.Run() //run test os.Exit(retCode) } @@ -50,7 +50,7 @@ func TestAnnotate(t *testing.T) { } func TestAnnotateToJson(t *testing.T) { - expect1 := `[ + expect := `[ { "Name": [ "Field1" @@ -69,11 +69,9 @@ func TestAnnotateToJson(t *testing.T) { } ]` - actual1, _ := BeeAnnotator.AnnotateToJson(Annotation1) - actual2, _ := BeeAnnotator.AnnotateToJson("") + actual, _ := BeeAnnotator.AnnotateToJson(Annotation1) - assert.Equal(t, expect1, actual1) - assert.Equal(t, "", actual2) + assert.Equal(t, expect, actual) } func TestHandleWhitespaceValues(t *testing.T) { diff --git a/parser/parser_test.go b/parser/parser_test.go index 315659d..9413765 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -3,11 +3,15 @@ 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 @@ -32,9 +36,9 @@ type StructA struct { Field7 StructB } ` - annotator := &Annotator{} + formatter := &sampleFormatter{} - sp, err := NewStructParser("src.go", src, "StructA", annotator) + sp, err := NewStructParser("src.go", src, "StructA", formatter) if err != nil { log.Fatal(err) } @@ -62,41 +66,3 @@ 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) -}