From 912d0c6a29fd70c7ab866e15f787fc5a4d6d2235 Mon Sep 17 00:00:00 2001 From: Softonik Date: Mon, 22 Jan 2024 03:33:04 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A7=D0=B8=D1=81=D1=82=D0=BA=D0=B0:=20Func=20?= =?UTF-8?q?=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=87=D0=B8=D0=BA?= =?UTF-8?q?=D0=B8=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81=D0=B5=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=B2=20=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9?= =?UTF-8?q?=20=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/service/func.go | 122 +++++++++++++++++++++++++++++++++++++++++ pkg/service/service.go | 117 --------------------------------------- 2 files changed, 122 insertions(+), 117 deletions(-) create mode 100644 pkg/service/func.go diff --git a/pkg/service/func.go b/pkg/service/func.go new file mode 100644 index 0000000..def4724 --- /dev/null +++ b/pkg/service/func.go @@ -0,0 +1,122 @@ +package service + +import ( + "go/ast" + "strings" +) + +func handleFuncDecl(decl ast.Decl) string { + fd := decl.(*ast.FuncDecl) + if shouldSkipFunction(fd.Type) { + return "" + } + code := "" + name := "" + ft := handleFuncDeclType(fd.Type) + code += ft + code += " " + name = handleFuncDeclName(fd.Name) + if name == "NewController" { + return "" + } + code += name + code += "(" + fp := handleFuncDeclParams(fd.Type) + code += fp + + addFunctionDeclaration(ft + " " + name + "(" + fp + ");") + + code += ") {" + code += handleBlockStmt(fd.Body) + code += "}" + return code +} +func shouldSkipFunction(t *ast.FuncType) (res bool) { + r := t.Results + if r == nil { + return + } + + l := r.List + if len(l) != 1 { + return + } + + p := l[0] + if p == nil { + return + } + + pt := p.Type + if pt == nil { + return + } + + i, ok := pt.(*ast.Ident) + if !ok { + return + } + + if i.Name == "ShouldBeSkipped" { + return true + } + return +} + +func handleFuncDeclType(t *ast.FuncType) string { + code := "" + if t.Results == nil { + return "void" + } + + fl := t.Results + if fl.NumFields() == 0 { + panic("handleFuncDeclType: fl.NumFields() == 0") + } + + switch ft := fl.List[0].Type.(type) { + case *ast.Ident: + code += handleIdentExpr(ft) + } + + return code +} + +func handleFuncDeclName(ident *ast.Ident) string { + code := "" + if ident == nil { + return code + } + code += ident.Name + if val, ok := mapping[code]; ok { + code = val + } + return code +} + +func handleFuncDeclParams(t *ast.FuncType) string { + code := "" + if t.Params == nil || t.Params.List == nil { + return code + } + values := make([]string, 0) + for _, field := range t.Params.List { + ftype := "" + switch ft := field.Type.(type) { + case *ast.Ident: + ftype = handleIdentExpr(ft) + } + for _, names := range field.Names { + values = append(values, ftype+" "+names.Name) + } + } + code += strings.Join(values, ",") + return code +} + +func addFunctionDeclaration(f string) { + dlock.Lock() + defer dlock.Unlock() + + funcDeclarations = append(funcDeclarations, f) +} diff --git a/pkg/service/service.go b/pkg/service/service.go index 966ea90..9b5138c 100644 --- a/pkg/service/service.go +++ b/pkg/service/service.go @@ -6,7 +6,6 @@ import ( "go/parser" "go/token" "io" - "strings" "sync" ) @@ -154,13 +153,6 @@ func (s *defaultService) printGoHelperDeclarations() { s.out.Write([]byte("\n")) } -func addFunctionDeclaration(f string) { - dlock.Lock() - defer dlock.Unlock() - - funcDeclarations = append(funcDeclarations, f) -} - func handleDecl(id int, decl ast.Decl, dst chan<- string, done chan<- bool) { code := "" switch d := decl.(type) { @@ -174,115 +166,6 @@ func handleDecl(id int, decl ast.Decl, dst chan<- string, done chan<- bool) { done <- true } -func handleFuncDecl(decl ast.Decl) string { - fd := decl.(*ast.FuncDecl) - if shouldSkipFunction(fd.Type) { - return "" - } - code := "" - name := "" - ft := handleFuncDeclType(fd.Type) - code += ft - code += " " - name = handleFuncDeclName(fd.Name) - if name == "NewController" { - return "" - } - code += name - code += "(" - fp := handleFuncDeclParams(fd.Type) - code += fp - - addFunctionDeclaration(ft + " " + name + "(" + fp + ");") - - code += ") {" - code += handleBlockStmt(fd.Body) - code += "}" - return code -} -func shouldSkipFunction(t *ast.FuncType) (res bool) { - r := t.Results - if r == nil { - return - } - - l := r.List - if len(l) != 1 { - return - } - - p := l[0] - if p == nil { - return - } - - pt := p.Type - if pt == nil { - return - } - - i, ok := pt.(*ast.Ident) - if !ok { - return - } - - if i.Name == "ShouldBeSkipped" { - return true - } - return -} - -func handleFuncDeclParams(t *ast.FuncType) string { - code := "" - if t.Params == nil || t.Params.List == nil { - return code - } - values := make([]string, 0) - for _, field := range t.Params.List { - ftype := "" - switch ft := field.Type.(type) { - case *ast.Ident: - ftype = handleIdentExpr(ft) - } - for _, names := range field.Names { - values = append(values, ftype+" "+names.Name) - } - } - code += strings.Join(values, ",") - return code -} - -func handleFuncDeclName(ident *ast.Ident) string { - code := "" - if ident == nil { - return code - } - code += ident.Name - if val, ok := mapping[code]; ok { - code = val - } - return code -} - -func handleFuncDeclType(t *ast.FuncType) string { - code := "" - if t.Results == nil { - return "void" - } - - fl := t.Results - if fl.NumFields() == 0 { - panic("handleFuncDeclType: fl.NumFields() == 0") - } - - switch ft := fl.List[0].Type.(type) { - case *ast.Ident: - code += handleIdentExpr(ft) - } - - return code -} - func handleGenDecl(decl ast.Decl) string { gd := decl.(*ast.GenDecl) code := ""