-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
165 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,28 @@ | ||
package go_annotation | ||
|
||
import "github.com/celt237/go-annotation/internal" | ||
|
||
// GetFileDesc 获取文件描述 | ||
// fileName: 文件名 | ||
func GetFileDesc(fileName string) (*internal.FileDesc, error) { | ||
return internal.GetFileParser(fileName).Parse() | ||
} | ||
|
||
// GetFilesDescList 获取文件描述列表 | ||
// directory: 目录 | ||
func GetFilesDescList(directory string) ([]*internal.FileDesc, error) { | ||
var filesDesc []*internal.FileDesc | ||
// 读取目录下的所有文件 | ||
fileNames, err := internal.GetFileNames(directory) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, fileName := range fileNames { | ||
fileDesc, err := GetFileDesc(fileName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
filesDesc = append(filesDesc, fileDesc) | ||
} | ||
return filesDesc, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package internal | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type AnnotationParser interface { | ||
Parse(comments []string) map[string]*Annotation | ||
} | ||
|
||
func getAnnotationParser(mode AnnotationMode) AnnotationParser { | ||
switch mode { | ||
case AnnotationModeMap: | ||
return &MapAnnotationParser{} | ||
default: | ||
return &ArrayAnnotationParser{} | ||
} | ||
} | ||
|
||
type ArrayAnnotationParser struct{} | ||
|
||
func (a *ArrayAnnotationParser) Parse(comments []string) map[string]*Annotation { | ||
annotations := make(map[string]*Annotation) | ||
for _, comment := range comments { | ||
commentSlice := splitComment(comment) | ||
if len(commentSlice) == 0 { | ||
continue | ||
} | ||
name := commentSlice[0] | ||
attribute := make(map[string]string) | ||
for i := 1; i < len(commentSlice); i++ { | ||
attribute[strconv.Itoa(i-1)] = commentSlice[i] | ||
} | ||
if _, ok := annotations[name]; ok { | ||
if len(attribute) > 0 { | ||
annotations[name].Attributes = append(annotations[name].Attributes, attribute) | ||
} | ||
} else { | ||
annotation := &Annotation{Name: name, Attributes: []map[string]string{}} | ||
if len(attribute) > 0 { | ||
annotation.Attributes = append(annotation.Attributes, attribute) | ||
} | ||
annotations[name] = annotation | ||
} | ||
} | ||
return annotations | ||
} | ||
|
||
type MapAnnotationParser struct{} | ||
|
||
func (a *MapAnnotationParser) Parse(comments []string) map[string]*Annotation { | ||
annotations := make(map[string]*Annotation) | ||
for _, comment := range comments { | ||
if strings.Contains(comment, "(") { | ||
// get annotation name | ||
name := comment[:strings.Index(comment, "(")] | ||
// get attribute | ||
attributeStr := comment[strings.Index(comment, "(")+1 : strings.LastIndex(comment, ")")] | ||
attributeSlice := strings.Split(attributeStr, ",") | ||
attribute := make(map[string]string) | ||
for _, item := range attributeSlice { | ||
// get attribute name and value | ||
itemSlice := strings.Split(item, "=") | ||
if len(itemSlice) != 2 { | ||
continue | ||
} | ||
attributeName := strings.TrimSpace(itemSlice[0]) | ||
attributeValue := strings.TrimSpace(itemSlice[1]) | ||
if strings.HasPrefix(attributeValue, "\"") && strings.HasSuffix(attributeValue, "\"") { | ||
attributeValue = attributeValue[1 : len(attributeValue)-1] | ||
} | ||
attribute[attributeName] = attributeValue | ||
} | ||
if _, ok := annotations[name]; ok { | ||
if len(attribute) > 0 { | ||
annotations[name].Attributes = append(annotations[name].Attributes, attribute) | ||
} | ||
} else { | ||
annotation := &Annotation{Name: name, Attributes: []map[string]string{}} | ||
if len(attribute) > 0 { | ||
annotation.Attributes = append(annotation.Attributes, attribute) | ||
} | ||
annotations[name] = annotation | ||
} | ||
} else { | ||
annotation := &Annotation{Name: comment, Attributes: []map[string]string{}} | ||
annotations[comment] = annotation | ||
} | ||
} | ||
return annotations | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
package internal | ||
|
||
var GenFilePrefix = "gen_" | ||
|
||
var ProjectPath = "internal/test/data" | ||
|
||
const AnnotationPrefix = "@" | ||
|
||
var CurrentAnnotationMode = AnnotationModeArray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package generate | ||
|
||
type Config struct { | ||
// service文件所在目录 必传 | ||
SourcePath string `yaml:"servicePath"` | ||
|
||
// 要生成的代码文件所在目录 必传 | ||
GenFilePath string `yaml:"genFilePath"` | ||
|
||
// 模版文件地址 | ||
TemplateFile string `yaml:"templateFile"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.