Skip to content

Commit

Permalink
staff
Browse files Browse the repository at this point in the history
  • Loading branch information
dengsgo committed Oct 28, 2023
1 parent 0b86051 commit 39b235e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
4 changes: 3 additions & 1 deletion cmd/decorator/buildtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ func main() {
}
}

//go:decor logging
func test(v ...string) string {
return ""
}

//go:decor-linter required:{a, b, c}
//go:decor-linter required: {a={"hello", "world"}, b=[,34], c}
//go:decor-linter nonzero: {a}
func logging(ctx *decor.Context, s string, a int, b bool) {
ctx.TargetDo()
}
Expand Down
67 changes: 66 additions & 1 deletion cmd/decorator/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import (
"go/printer"
"go/token"
"strings"
"unicode"
"unicode/utf8"
)

var (
errUsedDecorSyntaxErrorLossFunc = errors.New("syntax error using decorator: miss decorator name")
errUsedDecorSyntaxError = errors.New("syntax error using decorator")
errCalledDecorNotDecorator = errors.New("used decor is not a decorator function")
)

func isDecoratorFunc(fd *ast.FuncDecl, pkgName string) bool {
Expand All @@ -33,6 +41,63 @@ func isDecoratorFunc(fd *ast.FuncDecl, pkgName string) bool {
return strings.TrimSpace(buffer.String()) == fmt.Sprintf("*%s.Context", pkgName)
}

func parseDecorAndParameters(s string) (string, map[string]string, error) {
// s like:
// function
// function#{}
// function#{key=""}
// function#{key="", name=""}
// function#{key="", name="", age=100}
// function#{key="", name="", age=100, b = false}
if s == "" {
return "", nil, errUsedDecorSyntaxErrorLossFunc
}

callName, pStr, _ := strings.Cut(s, "#")
callName = cleanSpaceChar(callName)
if callName == "" {
return callName, nil, errUsedDecorSyntaxErrorLossFunc
}
p := map[string]string{}
if pStr == "" {
return callName, p, nil
}
if pStr[0] != '{' || pStr[len(pStr)-1] != '}' {
return callName, nil, errUsedDecorSyntaxError
}
if len(pStr) == 2 {
return callName, p, nil
}
if len(pStr) < 5 {
return callName, p, errUsedDecorSyntaxError
}
//str := pStr[1 : len(pStr)-1]
//for {
// if isLet() {
//
// }
//}

return callName, p, nil
}

func cleanSpaceChar(s string) string {
bf := bytes.NewBuffer([]byte{})
for len(s) > 0 {
r, size := utf8.DecodeRuneInString(s)
s = s[size:]
if unicode.IsSpace(r) {
continue
}
bf.WriteRune(r)
}
return bf.String()
}

func isLet() {

}

func checkDecorAndGetParam(pkgPath, funName string, annotationMap map[string]string) (args string, error error) {
decl, file, err := pkgILoader.findFunc(pkgPath, funName)
if err != nil {
Expand All @@ -45,7 +110,7 @@ func checkDecorAndGetParam(pkgPath, funName string, annotationMap map[string]str
}
m := collDeclFuncParamsAnfTypes(decl)
if len(m) < 1 {
return "", errors.New("used decor is not a decorator function")
return "", errCalledDecorNotDecorator
}
for _, v := range m {
if v.index == 0 && v.typ != fmt.Sprintf("*%s.Context", pkgName) {
Expand Down
6 changes: 3 additions & 3 deletions cmd/decorator/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ type _packageInfo struct {
}
Match,
GoFiles,
Imports,
Deps []string
Imports, // TODO remove -find
Deps []string // TODO remove -find
}

func getPackageInfo(pkgPath string) (*_packageInfo, error) {
command := []string{"go", "list", "-json"}
command := []string{"go", "list", "-json", "-find"}
if pkgPath != "" && pkgPath != "main" {
command = append(command, pkgPath)
}
Expand Down

0 comments on commit 39b235e

Please sign in to comment.