Last active
February 26, 2023 15:12
-
-
Save Gong-Bao-Chicken/743793b68cb52000a247ad02cc33b8d1 to your computer and use it in GitHub Desktop.
Convert nested ABAP structure into Flat structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DATA: lo_structure_descriptor type ref to cl_abap_structdescr, | |
lo_structure_descriptor_tmp type ref to cl_abap_structdescr, | |
lo_table_descriptor type ref to cl_abap_tabledescr, | |
lt_structure_components_nested type cl_abap_structdescr=>component_table, | |
lt_structure_components_flat type cl_abap_structdescr=>component_table, | |
lv_index type sy-tabix. | |
lo_table_descriptor ?= cl_abap_structdescr=>describe_by_data( lt_result_table_nested ). | |
lo_structure_descriptor ?= lo_table_descriptor->get_table_line_type( ). | |
lt_structure_components = lo_structure_descriptor->get_components( ). | |
loop at lt_structure_components_nested ASSIGNING FIELD-SYMBOL(<fs_component>). | |
lv_index = sy-tabix. | |
IF <fs_component>-type->kind EQ 'E'. | |
* Add only elemantary components to component table | |
APPEND <fs_component> to lt_structure_components_flat. | |
ENDIF. | |
IF <fs_component>-type->kind EQ 'S'. | |
* Expand nested structures before adding their elements to component table | |
lo_structure_descriptor_tmp ?= <fs_component>-type. | |
data(nested_components) = lo_structure_descriptor_tmp->get_components( ). | |
loop at nested_components ASSIGNING FIELD-SYMBOL(<fs_nested_component>). | |
if line_exists( lt_structure_components_nested[ name = <fs_nested_component>-name ] ). | |
* Workaround for duplicate file-names | |
<fs_nested_component>-name = <fs_comp>-name && `_` && <fs_new_comp>-name. | |
ENDIF. | |
* Insert after current index (preserves sort order) | |
INSERT <fs_nested_component> INTO lt_structure_components_nested INDEX ( lv_index + sy-tabix ). | |
ENDLOOP. | |
ENDIF. | |
ENDLOOP. | |
* Prepare the output table | |
DATA: lo_output_structdescr type ref to cl_abap_structdescr, | |
lo_output_tabledescr type ref to cl_abap_tabledescr, | |
lo_output_struc type ref to data, | |
lo_output_ttype type ref to data. | |
field-symbols: <fs_table> type standard table, | |
<fs_line> type any. | |
* create structure and table type descriptor | |
lo_output_structdescr = cl_abap_structdescr=>create( lt_structure_components_flat ). | |
lo_output_tabledescr = cl_abap_tabledescr=>create( | |
p_line_type = cl_abap_structdescr=>create( lt_structure_components_flat ) | |
p_table_kind = cl_abap_tabledescr=>tablekind_std | |
p_unique = abap_false | |
). | |
* handle runtime data type by means of generic variables | |
create data: lo_output_struc type handle lo_output_structdescr, | |
lo_output_ttype type handle lo_output_tabledescr. | |
assign: lo_output_struc->* to <fs_line>, | |
lo_output_ttype->* to <fs_table>. | |
* Move data to flat strucutre from nested structure | |
loop at lt_result_table_nested ASSIGNING FIELD-SYMBOL(<fs_result>). | |
MOVE-CORRESPONDING <fs_result> to <fs_line>. | |
MOVE-CORRESPONDING <fs_result>-nested-structure to <fs_line>. | |
* Note: Incomplete solution, as renamed (duplicate components occuring in multiple nested structures) can't be moved this way. | |
APPEND <fs_line> to <fs_table>. | |
ENDLOOP. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment