mirror of
				https://github.com/beego/bee.git
				synced 2025-10-31 07:23:22 +00:00 
			
		
		
		
	Merge pull request #787 from Codexiaoyi/parse-config
add formatter to format field
This commit is contained in:
		| @@ -1,13 +1,11 @@ | |||||||
| package beeParser | package beeParser | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"encoding/json" |  | ||||||
| 	"strings" | 	"strings" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| type Annotator interface { | type Annotator interface { | ||||||
| 	Annotate(string) []map[string]interface{} | 	Annotate(string) map[string]interface{} | ||||||
| 	AnnotateToJson(string) (string, error) |  | ||||||
| } | } | ||||||
|  |  | ||||||
| type Annotation struct { | type Annotation struct { | ||||||
| @@ -44,25 +42,16 @@ func handleWhitespaceValues(values []string) []string { | |||||||
|  |  | ||||||
| //parse annotation to generate array with key and values | //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. | //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 *Annotation) Annotate(annotation string) map[string]interface{} { | ||||||
| 	results := make([]map[string]interface{}, 0) | 	results := make(map[string]interface{}) | ||||||
| 	//split annotation with '@' | 	//split annotation with '@' | ||||||
| 	lines := strings.Split(comment, "@") | 	lines := strings.Split(annotation, "@") | ||||||
| 	//skip first line whitespace | 	//skip first line whitespace | ||||||
| 	for _, line := range lines[1:] { | 	for _, line := range lines[1:] { | ||||||
| 		kvs := strings.Split(line, " ") | 		kvs := strings.Split(line, " ") | ||||||
| 		key := kvs[0] | 		key := kvs[0] | ||||||
| 		values := strings.Split(strings.TrimSpace(line[len(kvs[0]):]), "\n") | 		values := strings.Split(strings.TrimSpace(line[len(kvs[0]):]), "\n") | ||||||
| 		annotation := make(map[string]interface{}) | 		results[key] = handleWhitespaceValues(values) | ||||||
| 		annotation[key] = handleWhitespaceValues(values) |  | ||||||
| 		results = append(results, annotation) |  | ||||||
| 	} | 	} | ||||||
| 	return results | 	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 |  | ||||||
| } |  | ||||||
|   | |||||||
| @@ -31,15 +31,15 @@ func TestMain(m *testing.M) { | |||||||
| } | } | ||||||
|  |  | ||||||
| func TestAnnotate(t *testing.T) { | func TestAnnotate(t *testing.T) { | ||||||
| 	expect1 := []map[string]interface{}{ | 	expect1 := map[string]interface{}{ | ||||||
| 		{"Name": []string{"Field1"}}, | 		"Name": []string{"Field1"}, | ||||||
| 		{"Type": []string{"string"}}, | 		"Type": []string{"string"}, | ||||||
| 		{"Path": []string{"https://github.com/beego/bee", "https://github.com/beego"}}, | 		"Path": []string{"https://github.com/beego/bee", "https://github.com/beego"}, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	expect2 := []map[string]interface{}{ | 	expect2 := map[string]interface{}{ | ||||||
| 		{"Number": []string{"2"}}, | 		"Number":   []string{"2"}, | ||||||
| 		{"Projects": []string{"https://github.com/beego/bee", "", "https://github.com/beego"}}, | 		"Projects": []string{"https://github.com/beego/bee", "", "https://github.com/beego"}, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	actual := BeeAnnotator.Annotate(Annotation1) | 	actual := BeeAnnotator.Annotate(Annotation1) | ||||||
| @@ -49,31 +49,6 @@ func TestAnnotate(t *testing.T) { | |||||||
| 	assert.Equal(t, expect2, actual2) | 	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) { | func TestHandleWhitespaceValues(t *testing.T) { | ||||||
| 	src := []string{ | 	src := []string{ | ||||||
| 		"    beego", | 		"    beego", | ||||||
| @@ -100,9 +75,3 @@ func BenchmarkAnnotate(b *testing.B) { | |||||||
| 		BeeAnnotator.Annotate(Annotation1) | 		BeeAnnotator.Annotate(Annotation1) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func BenchmarkAnnotateToJson(b *testing.B) { |  | ||||||
| 	for i := 0; i < b.N; i++ { |  | ||||||
| 		BeeAnnotator.AnnotateToJson(Annotation1) |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
|   | |||||||
							
								
								
									
										20
									
								
								parser/formatter.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								parser/formatter.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								parser/formatter_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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) | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user
	 Ming Deng
					Ming Deng