예제 #1
0
파일: reflect.go 프로젝트: keysonZZZ/kmg
func reflectToTplConfig(req *GenerateRequest) *tplConfig {
	config := &tplConfig{
		OutPackageName: req.OutPackageName,
		ClassName:      req.OutClassName,
		innerClassMap:  map[string]*InnerClass{},
	}
	if req.ApiNameFilterCb == nil {
		req.ApiNameFilterCb = func(name string) bool {
			return true
		}
	}

	pkg := kmgGoParser.NewProgramFromDefault().GetPackage(req.ObjectPkgPath)
	//pkg := kmgGoParser.MustParsePackage(kmgConfig.DefaultEnv().GOPATHToString(), req.ObjectPkgPath)
	namedTyp := pkg.LookupNamedType(req.ObjectName)
	if namedTyp == nil {
		panic(fmt.Errorf("can not find this object. [%s]", req.ObjectName))
	}
	objTyp := kmgGoParser.Type(namedTyp)
	if req.ObjectIsPointer {
		objTyp = kmgGoParser.NewPointer(objTyp)
	}
	//获取 object 上面所有的方法
	methodList := pkg.GetNamedTypeMethodSet(namedTyp)
	for _, methodObj := range methodList {
		if !methodObj.IsExport() {
			continue
		}
		if !req.ApiNameFilterCb(methodObj.Name) {
			continue
		}
		api := Api{
			Name: methodObj.Name,
		}
		RequestInnerClass := &InnerClass{
			Name:     api.Name + "RpcRequest",
			IsPublic: false,
		}
		for _, pairObj := range methodObj.InParameter {
			if isPointerToKmgHttpContext(pairObj.Type) {
				// 此处忽略掉特殊传入参数 *kmgHttp.Context
				continue
			}
			pair := NameTypePair{
				Name: kmgStrings.FirstLetterToUpper(pairObj.Name),
			}
			pair.TypeStr = config.addType(pairObj.Type)
			api.InArgsList = append(api.InArgsList, pair)
			RequestInnerClass.FieldList = append(RequestInnerClass.FieldList, pair)
		}
		config.addInnerClass(RequestInnerClass)
		/*
		 * 在java中,如果输出参数的类型是error,会转到exception里面
		 * 在java中,如果输出参数去除error后,没有参数了,java函数不会返回参数
		 * 在java中,如果输出参数除去error后,还有1个,直接返回这个类型,此时此参数依然按照前面的说法进行向前兼容.
		 * 在java中,如果输出参数除去error后,还有2个及2个以上,返回一个class,这个class包含这些个参数.
		 */
		notErrorOutParameter := []NameTypePair{}
		for i, pairObj := range methodObj.OutParameter {
			builtintyp, ok := pairObj.Type.(kmgGoParser.BuiltinType)
			if !(ok && string(builtintyp) == "error") {
				name := kmgStrings.FirstLetterToUpper(pairObj.Name)
				if name == "" {
					name = "Out_" + strconv.Itoa(i)
				}
				notErrorOutParameter = append(notErrorOutParameter, NameTypePair{
					Name:    name,
					TypeStr: config.addType(pairObj.Type),
				})
			}
		}
		if len(notErrorOutParameter) == 0 {
			api.OutTypeString = "void"
		} else {
			responseInnerClass := &InnerClass{
				Name:      api.Name + "RpcResponse",
				FieldList: notErrorOutParameter,
				IsPublic:  true,
			}
			config.addInnerClass(responseInnerClass)

			if len(notErrorOutParameter) == 1 {
				api.OutTypeString = notErrorOutParameter[0].TypeStr
				api.OutTypeFieldName = notErrorOutParameter[0].Name
			} else {
				api.OutTypeString = responseInnerClass.Name
			}
		}
		config.ApiList = append(config.ApiList, api)
	}
	return config
}
예제 #2
0
파일: reflect.go 프로젝트: keysonZZZ/kmg
func reflectToTplConfig(req *GenerateRequest) *tplConfig {
	config := &tplConfig{
		ObjectName:     req.ObjectName,
		OutPackageName: path.Base(req.OutPackageImportPath),
		ImportPathMap: map[string]bool{
			"encoding/json": true,
			"errors":        true,
			"fmt":           true,
			"github.com/bronze1man/kmg/kmgCrypto":      true,
			"github.com/bronze1man/kmg/kmgLog":         true,
			"github.com/bronze1man/kmg/kmgNet/kmgHttp": true,
			"net/http": true,
			"bytes":    true,
		},
	}
	if req.ApiNameFilterCb == nil {
		req.ApiNameFilterCb = func(name string) bool {
			return true
		}
	}

	pkg := kmgGoParser.MustParsePackage(kmgConfig.DefaultEnv().GOPATHToString(), req.ObjectPkgPath)
	namedTyp := pkg.LookupNamedType(req.ObjectName)
	if namedTyp == nil {
		panic(fmt.Errorf("can not find this object. [%s]", req.ObjectName))
	}
	objTyp := kmgGoParser.Type(namedTyp)
	if req.ObjectIsPointer {
		objTyp = kmgGoParser.NewPointer(objTyp)
	}
	var importPathList []string
	config.ObjectTypeStr, importPathList = kmgGoParser.MustWriteGoTypes(req.OutPackageImportPath, objTyp)
	config.mergeImportPath(importPathList)

	//获取 object的 上面所有的方法
	methodList := pkg.GetNamedTypeMethodSet(namedTyp)
	for _, methodObj := range methodList {
		if !methodObj.IsExport() {
			continue
		}
		if !req.ApiNameFilterCb(methodObj.Name) {
			continue
		}
		api := Api{
			Name: methodObj.Name,
		}
		for _, pairObj := range methodObj.InParameter {
			pair := ArgumentNameTypePair{
				Name: kmgStrings.FirstLetterToUpper(pairObj.Name),
			}
			pair.ObjectTypeStr, importPathList = kmgGoParser.MustWriteGoTypes(req.OutPackageImportPath, pairObj.Type)
			config.mergeImportPath(importPathList)
			api.InArgsList = append(api.InArgsList, pair)
		}
		for i, pairObj := range methodObj.OutParameter {
			name := kmgStrings.FirstLetterToUpper(pairObj.Name)
			if name == "" {
				builtintyp, ok := pairObj.Type.(kmgGoParser.BuiltinType)
				if ok && string(builtintyp) == "error" { //TODO 不要特例
					name = "Err"
				} else {
					name = fmt.Sprintf("Out_%d", i)
				}
			}
			pair := ArgumentNameTypePair{
				Name: name,
			}
			pair.ObjectTypeStr, importPathList = kmgGoParser.MustWriteGoTypes(req.OutPackageImportPath, pairObj.Type)
			config.mergeImportPath(importPathList)
			api.OutArgsList = append(api.OutArgsList, pair)
		}
		config.ApiList = append(config.ApiList, api)
	}
	return config
}