Example #1
0
func wrapFunctionDescription(funcDesc C.RFC_FUNCTION_DESC_HANDLE) (goFuncDesc FunctionDescription, err error) {
	var rc C.RFC_RC
	var errorInfo C.RFC_ERROR_INFO
	var funcName C.RFC_ABAP_NAME
	var i, paramCount C.uint
	var paramDesc C.RFC_PARAMETER_DESC

	rc = C.RfcGetFunctionName(funcDesc, &funcName[0], &errorInfo)
	if rc != C.RFC_OK {
		return goFuncDesc, rfcError(errorInfo, "Failed getting function name")
	}

	goFuncName, err := wrapString((*C.SAP_UC)(&funcName[0]), false)
	if err != nil {
		return
	}
	goFuncDesc = FunctionDescription{Name: goFuncName}

	rc = C.RfcGetParameterCount(funcDesc, &paramCount, &errorInfo)
	if rc != C.RFC_OK {
		return goFuncDesc, rfcError(errorInfo, "Failed getting function(%v) parameter count", goFuncName)
	}

	for i = 0; i < paramCount; i++ {
		rc = C.RfcGetParameterDescByIndex(funcDesc, i, &paramDesc, &errorInfo)
		if rc != C.RFC_OK {
			return goFuncDesc, rfcError(errorInfo, "Failed getting function(%v) parameter description by index(%v)", goFuncName, i)
		}

		optional := true
		if paramDesc.optional == 0 {
			optional = false
		}

		var paramName string
		var paramType string
		var paramDir string
		var paramDefaultVal string
		var paramText string
		paramName, err = wrapString((*C.SAP_UC)(&paramDesc.name[0]), false)
		paramType, err = wrapString((*C.SAP_UC)(C.RfcGetTypeAsString(paramDesc._type)), false)
		paramDir, err = wrapString((*C.SAP_UC)(C.RfcGetDirectionAsString(paramDesc.direction)), false)
		paramDefaultVal, err = wrapString((*C.SAP_UC)(&paramDesc.defaultValue[0]), false)
		paramText, err = wrapString((*C.SAP_UC)(&paramDesc.parameterText[0]), false)
		if err != nil {
			return
		}

		goParamDesc := ParameterDescription{
			Name:          paramName,
			ParameterType: paramType,
			Direction:     paramDir,
			NucLength:     uint(paramDesc.nucLength),
			UcLength:      uint(paramDesc.ucLength),
			Decimals:      uint(paramDesc.decimals),
			DefaultValue:  paramDefaultVal,
			ParameterText: paramText,
			Optional:      optional,
		}

		if paramDesc.typeDescHandle != nil {
			goParamDesc.TypeDesc, err = wrapTypeDescription(paramDesc.typeDescHandle)
			if err != nil {
				return
			}
		}

		goFuncDesc.Parameters = append(goFuncDesc.Parameters, goParamDesc)
	}

	return
}
Example #2
0
func wrapTypeDescription(typeDesc C.RFC_TYPE_DESC_HANDLE) (goTypeDesc TypeDescription, err error) {
	var rc C.RFC_RC
	var errorInfo C.RFC_ERROR_INFO
	var fieldDesc C.RFC_FIELD_DESC
	var nucLength, ucLength C.uint
	var i, fieldCount C.uint

	typeName := (*C.SAP_UC)(C.malloc((C.size_t)(40 + 1)))
	*typeName = 0
	defer C.free(unsafe.Pointer(typeName))

	rc = C.RfcGetTypeName(typeDesc, (*C.RFC_CHAR)(typeName), &errorInfo)
	if rc != C.RFC_OK {
		return goTypeDesc, rfcError(errorInfo, "Failed getting type name")
	}

	name, err := wrapString(typeName, false)
	if err != nil {
		return
	}

	rc = C.RfcGetTypeLength(typeDesc, &nucLength, &ucLength, &errorInfo)
	if rc != C.RFC_OK {
		return goTypeDesc, rfcError(errorInfo, "Failed getting type(%v) length", name)
	}

	goTypeDesc = TypeDescription{Name: name, NucLength: uint(nucLength), UcLength: uint(ucLength)}

	rc = C.RfcGetFieldCount(typeDesc, &fieldCount, &errorInfo)
	if rc != C.RFC_OK {
		return goTypeDesc, rfcError(errorInfo, "Failed getting field count")
	}

	for i = 0; i < fieldCount; i++ {
		rc = C.RfcGetFieldDescByIndex(typeDesc, i, &fieldDesc, &errorInfo)
		if rc != C.RFC_OK {
			return goTypeDesc, rfcError(errorInfo, "Failed getting field by index(%v)", i)
		}

		var fieldName string
		var fieldType string
		fieldName, err = wrapString((*C.SAP_UC)(&fieldDesc.name[0]), false)
		fieldType, err = wrapString((*C.SAP_UC)(C.RfcGetTypeAsString(fieldDesc._type)), false)
		if err != nil {
			return
		}

		goFieldDesc := FieldDescription{
			Name:      fieldName,
			FieldType: fieldType,
			NucLength: uint(fieldDesc.nucLength),
			NucOffset: uint(fieldDesc.nucOffset),
			UcLength:  uint(fieldDesc.ucLength),
			UcOffset:  uint(fieldDesc.ucOffset),
			Decimals:  uint(fieldDesc.decimals),
		}

		if fieldDesc.typeDescHandle != nil {
			goFieldDesc.TypeDesc, err = wrapTypeDescription(fieldDesc.typeDescHandle)
			if err != nil {
				return
			}
		}

		goTypeDesc.Fields = append(goTypeDesc.Fields, goFieldDesc)
	}

	return
}