From 06ce2b63112e0ff8eeda2bf84b96b9b6d30ed48d Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Mon, 13 Jan 2020 17:35:37 +0800 Subject: [PATCH] refactor: rename build to append --- trial/cocago/cocago_parser.go | 42 ++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/trial/cocago/cocago_parser.go b/trial/cocago/cocago_parser.go index e9c8d13..b385b80 100644 --- a/trial/cocago/cocago_parser.go +++ b/trial/cocago/cocago_parser.go @@ -52,9 +52,9 @@ func (n *CocagoParser) Visitor(f *ast.File, fset *token.FileSet, fileName string currentStruct = trial.CodeDataStruct{} currentStruct.Name = x.Name.String() case *ast.StructType: - BuildStructType(currentStruct, x, ¤tFile) + AddStructType(currentStruct, x, ¤tFile) case *ast.FuncDecl: - BuildFunction(currentStruct, x, currentFile) + AddFunction(currentStruct, x, currentFile) } return true }) @@ -62,10 +62,7 @@ func (n *CocagoParser) Visitor(f *ast.File, fset *token.FileSet, fileName string return ¤tFile } -func BuildFunction(currentStruct trial.CodeDataStruct, x *ast.FuncDecl, currentFile trial.CodeFile) { - codeFunc := &trial.CodeFunction{ - Name: x.Name.String(), - } +func AddFunction(currentStruct trial.CodeDataStruct, x *ast.FuncDecl, currentFile trial.CodeFile) { recv := "" for _, item := range x.Recv.List { switch x := item.Type.(type) { @@ -74,17 +71,7 @@ func BuildFunction(currentStruct trial.CodeDataStruct, x *ast.FuncDecl, currentF } } - if x.Type.Params != nil { - fieldList := x.Type.Params.List - properties := BuildFieldToProperty(fieldList) - codeFunc.Parameters = append(codeFunc.Parameters, properties...) - } - - if x.Type.Results != nil { - fieldList := x.Type.Results.List - properties := BuildFieldToProperty(fieldList) - codeFunc.ReturnTypes = append(codeFunc.Parameters, properties...) - } + codeFunc := BuildFunction(x) if recv != "" { member := GetMemberFromFile(currentFile, recv) @@ -99,6 +86,25 @@ func BuildFunction(currentStruct trial.CodeDataStruct, x *ast.FuncDecl, currentF } } +func BuildFunction(x *ast.FuncDecl) *trial.CodeFunction { + codeFunc := &trial.CodeFunction{ + Name: x.Name.String(), + } + + if x.Type.Params != nil { + fieldList := x.Type.Params.List + properties := BuildFieldToProperty(fieldList) + codeFunc.Parameters = append(codeFunc.Parameters, properties...) + } + + if x.Type.Results != nil { + fieldList := x.Type.Results.List + properties := BuildFieldToProperty(fieldList) + codeFunc.ReturnTypes = append(codeFunc.Parameters, properties...) + } + return codeFunc +} + func createMember() { } @@ -128,7 +134,7 @@ func BuildFieldToProperty(fieldList []*ast.Field) []trial.CodeProperty { return properties } -func BuildStructType(currentStruct trial.CodeDataStruct, x *ast.StructType, currentFile *trial.CodeFile) { +func AddStructType(currentStruct trial.CodeDataStruct, x *ast.StructType, currentFile *trial.CodeFile) { member := trial.CodeMember{ DataStructID: currentStruct.Name, Type: "struct", -- GitLab