예제 #1
0
// GetFunctionDescription returns the wrapped function description of the given function.
func (conn *Connection) GetFunctionDescription(goFuncName string) (goFuncDesc FunctionDescription, err error) {
	var errorInfo C.RFC_ERROR_INFO

	funcName, err := fillString(goFuncName)
	defer C.free(unsafe.Pointer(funcName))
	if err != nil {
		return
	}

	if !conn.alive {
		err = conn.Open()
		if err != nil {
			return
		}
	}

	funcDesc := C.RfcGetFunctionDesc(conn.handle, funcName, &errorInfo)
	if funcDesc == nil {
		return goFuncDesc, rfcError(errorInfo, "Could not get function description for \"%v\"", goFuncName)
	}

	return wrapFunctionDescription(funcDesc)
}
예제 #2
0
// Call calls the given function with the given parameters and wraps the results returned.
func (conn *Connection) Call(goFuncName string, params interface{}) (result map[string]interface{}, err error) {
	var errorInfo C.RFC_ERROR_INFO

	funcName, err := fillString(goFuncName)
	defer C.free(unsafe.Pointer(funcName))
	if err != nil {
		return
	}

	if !conn.alive {
		err = conn.Open()
		if err != nil {
			return
		}
	}

	funcDesc := C.RfcGetFunctionDesc(conn.handle, funcName, &errorInfo)
	if funcDesc == nil {
		return result, rfcError(errorInfo, "Could not get function description for \"%v\"", funcName)
	}

	funcCont := C.RfcCreateFunction(funcDesc, &errorInfo)
	if funcCont == nil {
		return result, rfcError(errorInfo, "Could not create function")
	}

	defer C.RfcDestroyFunction(funcCont, nil)

	paramsValue := reflect.ValueOf(params)
	if paramsValue.Type().Kind() == reflect.Map {
		keys := paramsValue.MapKeys()
		if len(keys) > 0 {
			if keys[0].Kind() == reflect.String {
				for _, nameValue := range keys {
					fieldName := nameValue.String()
					fieldValue := paramsValue.MapIndex(nameValue).Interface()

					err = fillFunctionParameter(funcDesc, funcCont, fieldName, fieldValue)
					if err != nil {
						return
					}
				}
			} else {
				return result, rfcError(errorInfo, "Could not fill parameters passed as map with non-string keys")
			}
		}
	} else if paramsValue.Type().Kind() == reflect.Struct {
		for i := 0; i < paramsValue.NumField(); i++ {
			fieldName := paramsValue.Type().Field(i).Name
			fieldValue := paramsValue.Field(i).Interface()

			err = fillFunctionParameter(funcDesc, funcCont, fieldName, fieldValue)
			if err != nil {
				return
			}
		}
	} else {
		return result, rfcError(errorInfo, "Parameters can only be passed as types map[string]interface{} or go-structures")
	}

	rc := C.RfcInvoke(conn.handle, funcCont, &errorInfo)
	if rc != C.RFC_OK {
		return result, rfcError(errorInfo, "Could not invoke function \"%v\"", goFuncName)
	}

	if conn.returnImportParams {
		return wrapResult(funcDesc, funcCont, (C.RFC_DIRECTION)(0), conn.rstrip)
	}
	return wrapResult(funcDesc, funcCont, C.RFC_IMPORT, conn.rstrip)
}