2021-05-23 12:16:20 +00:00
|
|
|
package beeParser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-05-24 13:59:01 +00:00
|
|
|
// field formatter by annotation
|
|
|
|
type Annotator struct{}
|
2021-05-23 12:16:20 +00:00
|
|
|
|
|
|
|
func isWhitespace(ch byte) bool { return ch == ' ' || ch == '\t' || ch == '\r' }
|
|
|
|
|
|
|
|
func handleHeadWhitespace(s string) string {
|
|
|
|
i := 0
|
|
|
|
for i < len(s) && isWhitespace(s[i]) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return s[i:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleTailWhitespace(s string) string {
|
|
|
|
i := len(s)
|
|
|
|
for i > 0 && isWhitespace(s[i-1]) {
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
return s[0:i]
|
|
|
|
}
|
|
|
|
|
|
|
|
//handle value to remove head and tail space.
|
|
|
|
func handleWhitespaceValues(values []string) []string {
|
|
|
|
res := make([]string, 0)
|
|
|
|
for _, v := range values {
|
|
|
|
v = handleHeadWhitespace(v)
|
|
|
|
v = handleTailWhitespace(v)
|
|
|
|
res = append(res, v)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
//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.
|
2021-05-24 13:59:01 +00:00
|
|
|
func (a *Annotator) Annotate(comment string) []map[string]interface{} {
|
2021-05-23 12:16:20 +00:00
|
|
|
results := make([]map[string]interface{}, 0)
|
|
|
|
//split annotation with '@'
|
|
|
|
lines := strings.Split(comment, "@")
|
|
|
|
//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")
|
2021-05-23 14:11:48 +00:00
|
|
|
annotation := make(map[string]interface{})
|
2021-05-23 12:16:20 +00:00
|
|
|
annotation[key] = handleWhitespaceValues(values)
|
|
|
|
results = append(results, annotation)
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
//parse annotation to json
|
2021-05-24 13:59:01 +00:00
|
|
|
func (a *Annotator) AnnotateToJson(comment string) (string, error) {
|
2021-05-23 12:16:20 +00:00
|
|
|
annotate := a.Annotate(comment)
|
|
|
|
result, err := json.MarshalIndent(annotate, "", " ")
|
|
|
|
return string(result), err
|
|
|
|
}
|
2021-05-24 13:59:01 +00:00
|
|
|
|
|
|
|
func (a *Annotator) Format(field *StructField) string {
|
|
|
|
f, _ := a.AnnotateToJson(field.Doc)
|
|
|
|
return f
|
|
|
|
}
|