Beispiel #1
0
func wrapStructure(typeDesc C.RFC_TYPE_DESC_HANDLE, container C.RFC_STRUCTURE_HANDLE, strip bool) (result map[string]interface{}, err error) {
	var errorInfo C.RFC_ERROR_INFO
	var i, fieldCount C.uint
	var fieldDesc C.RFC_FIELD_DESC

	rc := C.RfcGetFieldCount(typeDesc, &fieldCount, &errorInfo)
	if rc != C.RFC_OK {
		return result, rfcError(errorInfo, "Failed getting field count")
	}
	result = make(map[string]interface{})
	for i = 0; i < fieldCount; i++ {
		rc = C.RfcGetFieldDescByIndex(typeDesc, i, &fieldDesc, &errorInfo)
		if rc != C.RFC_OK {
			return result, rfcError(errorInfo, "Failed getting field description by index(%v)", i)
		}
		var fieldName string
		fieldName, err = wrapString((*C.SAP_UC)(&fieldDesc.name[0]), strip)
		if err != nil {
			return
		}
		result[fieldName], err = wrapVariable(fieldDesc._type, C.RFC_FUNCTION_HANDLE(container), (*C.SAP_UC)(&fieldDesc.name[0]), fieldDesc.nucLength, fieldDesc.typeDescHandle, strip)
		if err != nil {
			return
		}
	}
	return
}
Beispiel #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
}