Browse Source

Merge pull request #787 from Codexiaoyi/parse-config

add formatter to format field
pull/788/head
Ming Deng 2 months ago
committed by GitHub
parent
commit
7a772542fb
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      parser/annotator.go
  2. 45
      parser/annotator_test.go
  3. 20
      parser/formatter.go
  4. 33
      parser/formatter_test.go

21
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
}

45
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)
}
}

20
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{}}
}

33
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)
}
Loading…
Cancel
Save