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

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

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

P
Phodal Huang 已提交
47
	property := &core_domain.CodeProperty{
48 49 50
		Modifiers:   nil,
		Name:        name,
		TypeType:    typeType,
51
		TypeValue:   typeName,
52 53
		ReturnTypes: results,
		Parameters:  params,
P
Phodal Huang 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
	}
	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 ""
	}
}

74
func BuildFunction(x *ast.FuncDecl, file *core_domain.CodeFile) *core_domain.CodeFunction {
P
Phodal Huang 已提交
75
	codeFunc := &core_domain.CodeFunction{
P
Phodal Huang 已提交
76 77 78 79 80 81 82 83
		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 已提交
84
		codeFunc.MultipleReturns = append(codeFunc.Parameters, BuildFieldToProperty(x.Type.Results.List)...)
P
Phodal Huang 已提交
85 86 87
	}

	for _, item := range x.Body.List {
P
Phodal Huang 已提交
88
		BuildMethodCall(codeFunc, item, file)
89
	}
P
Phodal Huang 已提交
90
	return codeFunc
91
}
P
Phodal Huang 已提交
92

P
Phodal Huang 已提交
93 94
func BuildFieldToProperty(fieldList []*ast.Field) []core_domain.CodeProperty {
	var properties []core_domain.CodeProperty
95 96 97 98 99 100
	for _, field := range fieldList {
		property := BuildPropertyField(getFieldName(field), field)
		properties = append(properties, *property)
	}
	return properties
}
P
Phodal Huang 已提交
101 102 103 104 105 106 107 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 133 134

func BuildMethodCall(codeFunc *core_domain.CodeFunction, item ast.Stmt, file *core_domain.CodeFile) {
	switch it := item.(type) {
	case *ast.ExprStmt:
		BuildMethodCallExprStmt(it, codeFunc)
	default:
		fmt.Fprintf(output, "methodCall %s\n", reflect.TypeOf(it))
	}
}

func BuildMethodCallExprStmt(it *ast.ExprStmt, codeFunc *core_domain.CodeFunction) {
	switch expr := it.X.(type) {
	case *ast.CallExpr:
		selector, selName := BuildExpr(expr.Fun.(ast.Expr))
		call := core_domain.CodeCall{
			Package:    "",
			Type:       "",
			NodeName:   selector,
			MethodName: selName,
		}

		for _, arg := range expr.Args {
			value, kind := BuildExpr(arg.(ast.Expr))
			property := &core_domain.CodeProperty{
				TypeValue: value,
				TypeType:  kind,
			}

			call.Parameters = append(call.Parameters, *property)
		}

		codeFunc.MethodCalls = append(codeFunc.MethodCalls, call)
	}
}