cocago_builder.go 5.1 KB
Newer Older
1 2 3
package cocago

import (
P
Phodal Huang 已提交
4
	"fmt"
5
	. "github.com/phodal/coca/pkg/domain/core_domain"
6
	"go/ast"
P
Phodal Huang 已提交
7
	"reflect"
P
Phodal Huang 已提交
8
	"strings"
9 10
)

11
func BuildPropertyField(name string, field *ast.Field) *CodeProperty {
P
Phodal Huang 已提交
12 13
	var typeName string
	var typeType string
14 15
	var params []CodeProperty
	var results []CodeProperty
P
Phodal Huang 已提交
16 17 18 19 20 21 22 23 24 25 26 27
	switch x := field.Type.(type) {
	case *ast.Ident:
		typeType = "Identify"
		typeName = x.String()
	case *ast.ArrayType:
		typeType = "ArrayType"
		switch typeX := x.Elt.(type) {
		case *ast.Ident:
			typeName = typeX.String()
		case *ast.SelectorExpr:
			typeName = getSelectorName(*typeX)
		default:
P
Phodal Huang 已提交
28
			fmt.Fprintf(output, "BuildPropertyField ArrayType %s\n", reflect.TypeOf(x.Elt))
P
Phodal Huang 已提交
29 30 31 32
		}
	case *ast.FuncType:
		typeType = "Function"
		typeName = "func"
33 34 35 36 37 38
		if x.Params != nil {
			params = BuildFieldToProperty(x.Params.List)
		}
		if x.Results != nil {
			results = BuildFieldToProperty(x.Results.List)
		}
P
Phodal Huang 已提交
39 40 41 42 43 44
	case *ast.StarExpr:
		typeName = getStarExprName(*x)
		typeType = "Star"
	case *ast.SelectorExpr:
		typeName = getSelectorName(*x)
	default:
P
Phodal Huang 已提交
45
		fmt.Fprintf(output, "BuildPropertyField %s\n", reflect.TypeOf(x))
46
	}
P
Phodal Huang 已提交
47

48
	property := &CodeProperty{
49 50 51
		Modifiers:   nil,
		Name:        name,
		TypeType:    typeType,
52
		TypeValue:   typeName,
53 54
		ReturnTypes: results,
		Parameters:  params,
P
Phodal Huang 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
	}
	return property
}

func getSelectorName(typeX ast.SelectorExpr) string {
	return typeX.X.(*ast.Ident).String() + "." + typeX.Sel.Name
}

func getStarExprName(starExpr ast.StarExpr) string {
	switch x := starExpr.X.(type) {
	case *ast.Ident:
		return x.Name
	case *ast.SelectorExpr:
		return getSelectorName(*x)
	default:
		fmt.Println("getStarExprName", reflect.TypeOf(x))
		return ""
	}
}

75 76
func BuildFunction(x *ast.FuncDecl, file *CodeFile) *CodeFunction {
	codeFunc := &CodeFunction{
P
Phodal Huang 已提交
77 78 79 80 81 82 83 84
		Name: x.Name.String(),
	}

	if x.Type.Params != nil {
		codeFunc.Parameters = append(codeFunc.Parameters, BuildFieldToProperty(x.Type.Params.List)...)
	}

	if x.Type.Results != nil {
P
Phodal Huang 已提交
85
		codeFunc.MultipleReturns = append(codeFunc.Parameters, BuildFieldToProperty(x.Type.Results.List)...)
P
Phodal Huang 已提交
86 87
	}

88 89
	fields := file.Fields
	var localVars []CodeProperty
P
Phodal Huang 已提交
90
	for _, item := range x.Body.List {
91
		localVars = BuildMethodCall(codeFunc, item, fields, localVars, file.Imports, file.PackageName)
92
	}
P
Phodal Huang 已提交
93
	return codeFunc
94
}
P
Phodal Huang 已提交
95

96 97
func BuildFieldToProperty(fieldList []*ast.Field) []CodeProperty {
	var properties []CodeProperty
98 99 100 101 102 103
	for _, field := range fieldList {
		property := BuildPropertyField(getFieldName(field), field)
		properties = append(properties, *property)
	}
	return properties
}
P
Phodal Huang 已提交
104

105
func BuildMethodCall(codeFunc *CodeFunction, item ast.Stmt, fields []CodeField, localVars []CodeProperty, imports []CodeImport, packageName string) []CodeProperty {
P
Phodal Huang 已提交
106 107
	switch it := item.(type) {
	case *ast.ExprStmt:
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
		BuildMethodCallExprStmt(it, codeFunc, fields, imports, packageName, localVars)
	case *ast.DeferStmt:
		call := BuildCallFromExpr(it.Call, fields, imports, packageName, localVars)
		codeFunc.FunctionCalls = append(codeFunc.FunctionCalls, call)
	case *ast.AssignStmt:
		var vars []CodeProperty
		for _, lh := range it.Lhs {
			var left string
			switch lhx := lh.(type) {
			case *ast.Ident:
				left = lhx.Name
			}

			for _, expr := range it.Rhs {
				_, _, kind := BuildExpr(expr)
				property := CodeProperty{
					TypeValue: left,
					TypeType:  kind,
				}

				vars = append(vars, property)
			}
		}

		localVars = vars
P
Phodal Huang 已提交
133 134 135
	default:
		fmt.Fprintf(output, "methodCall %s\n", reflect.TypeOf(it))
	}
136 137

	return localVars
P
Phodal Huang 已提交
138 139
}

140
func BuildMethodCallExprStmt(it *ast.ExprStmt, codeFunc *CodeFunction, fields []CodeField, imports []CodeImport, currentPackage string, localVars []CodeProperty) {
P
Phodal Huang 已提交
141 142
	switch expr := it.X.(type) {
	case *ast.CallExpr:
143 144 145 146 147 148
		call := BuildCallFromExpr(expr, fields, imports, currentPackage, localVars)
		codeFunc.FunctionCalls = append(codeFunc.FunctionCalls, call)
	default:
		fmt.Fprintf(output, "BuildMethodCallExprStmt: %s\n", reflect.TypeOf(expr))
	}
}
149

150 151 152 153 154 155 156 157
func BuildCallFromExpr(expr *ast.CallExpr, fields []CodeField, imports []CodeImport, currentPackage string, localVars []CodeProperty) CodeCall {
	_, selector, selName := BuildExpr(expr.Fun.(ast.Expr))
	target := ParseTarget(selector, fields, localVars)
	packageName := getPackageName(target, imports)
	//fmt.Println(packageName)
	if packageName == "" {
		packageName = currentPackage
	}
P
Phodal Huang 已提交
158

159 160 161 162 163 164
	call := CodeCall{
		Package:    packageName,
		Type:       target,
		NodeName:   selector,
		MethodName: selName,
	}
P
Phodal Huang 已提交
165

166 167 168 169 170
	for _, arg := range expr.Args {
		_, value, kind := BuildExpr(arg.(ast.Expr))
		property := &CodeProperty{
			TypeValue: value,
			TypeType:  kind,
P
Phodal Huang 已提交
171 172
		}

173
		call.Parameters = append(call.Parameters, *property)
P
Phodal Huang 已提交
174
	}
175
	return call
P
Phodal Huang 已提交
176
}
177

178
func getPackageName(target string, imports []CodeImport) string {
P
Phodal Huang 已提交
179 180 181 182 183
	packageName := ""
	if strings.Contains(target, ".") {
		split := strings.Split(target, ".")
		for _, imp := range imports {
			if strings.HasSuffix(imp.Source, split[0]) {
184
				return split[0]
P
Phodal Huang 已提交
185 186 187 188 189 190
			}
		}
	}
	return packageName
}

191 192 193 194 195 196 197
func ParseTarget(selector string, fields []CodeField, localVars []CodeProperty) string {
	for _, localVar := range localVars {
		if selector == localVar.TypeValue {
			return localVar.TypeType
		}
	}

198 199 200 201 202 203 204
	for _, field := range fields {
		if field.TypeValue == selector {
			return field.TypeType
		}
	}
	return ""
}