Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CALL TRANSFORMATION, suppress initial components #821

Merged
merged 7 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
CLASS kernel_call_transformation DEFINITION PUBLIC.
* handling of ABAP statement CALL TRANSFORMATION
PUBLIC SECTION.
CLASS-METHODS call IMPORTING input TYPE any.
TYPES: BEGIN OF ty_options,
initial_components TYPE string,
END OF ty_options.

CONSTANTS: BEGIN OF gc_options,
suppress TYPE string VALUE 'suppress',
END OF gc_options.

CLASS-METHODS call
IMPORTING
name TYPE any
options TYPE any.
PRIVATE SECTION.
CLASS-DATA mi_doc TYPE REF TO if_ixml_document.
CLASS-DATA mi_writer TYPE REF TO if_sxml_writer.
CLASS-DATA mi_doc TYPE REF TO if_ixml_document.
CLASS-DATA mi_writer TYPE REF TO if_sxml_writer.
CLASS-DATA ms_options TYPE ty_options.

CLASS-METHODS parse_xml
IMPORTING
Expand All @@ -19,6 +31,9 @@ CLASS kernel_call_transformation DEFINITION PUBLIC.
iv_ref TYPE REF TO data
RETURNING
VALUE(rv_type) TYPE string.

CLASS-METHODS parse_options
IMPORTING options TYPE any.
ENDCLASS.

CLASS kernel_call_transformation IMPLEMENTATION.
Expand Down Expand Up @@ -46,6 +61,8 @@ CLASS kernel_call_transformation IMPLEMENTATION.
WRITE '@KERNEL lv_name.set(INPUT.name.toUpperCase());'.
ASSERT lv_name = 'ID'.

parse_options( options ).

* Handle input SOURCE
WRITE '@KERNEL if (INPUT.sourceXML?.constructor.name === "ABAPObject") this.mi_doc.set(INPUT.sourceXML);'.
WRITE '@KERNEL if (INPUT.sourceXML?.constructor.name === "String") lv_source.set(INPUT.sourceXML);'.
Expand Down Expand Up @@ -91,7 +108,9 @@ CLASS kernel_call_transformation IMPLEMENTATION.
WRITE '@KERNEL }'.
IF lv_result = abap_true.
lv_result = '<?xml version="1.0" encoding="utf-16"?><asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0"><asx:values>'.
CREATE OBJECT lo_data_to_xml.
CREATE OBJECT lo_data_to_xml
EXPORTING
is_options = ms_options.
WRITE '@KERNEL if (INPUT.source.constructor.name === "Object") {'.
WRITE '@KERNEL for (const name in INPUT.source) {'.
WRITE '@KERNEL lv_name.set(name);'.
Expand Down Expand Up @@ -164,6 +183,26 @@ CLASS kernel_call_transformation IMPLEMENTATION.

ENDMETHOD.

METHOD parse_options.
* https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapcall_transformation_options.htm

DATA lv_name TYPE string.
DATA lv_value TYPE string.

FIELD-SYMBOLS <lv_field> TYPE string.


WRITE '@KERNEL for (const name in INPUT.options || {}) {'.
WRITE '@KERNEL lv_name.set(name);'.
WRITE '@KERNEL lv_value.set(INPUT.options[name]);'.
ASSIGN COMPONENT lv_name OF STRUCTURE ms_options TO <lv_field>.
IF sy-subrc = 0.
<lv_field> = lv_value.
ENDIF.
WRITE '@KERNEL }'.

ENDMETHOD.

METHOD traverse_write_type.
DATA lo_type TYPE REF TO cl_abap_typedescr.
lo_type = cl_abap_typedescr=>describe_by_data( iv_ref->* ).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ CLASS lcl_heap DEFINITION.
VALUE(rv_xml) TYPE string.
PRIVATE SECTION.
DATA mv_counter TYPE i.
DATA mv_data TYPE string.
DATA mv_data TYPE string.
ENDCLASS.

CLASS lcl_data_to_xml DEFINITION.
PUBLIC SECTION.
METHODS constructor
IMPORTING
io_heap TYPE REF TO lcl_heap OPTIONAL.
is_options TYPE kernel_call_transformation=>ty_options OPTIONAL
io_heap TYPE REF TO lcl_heap OPTIONAL.

METHODS run
IMPORTING
Expand All @@ -30,7 +31,8 @@ CLASS lcl_data_to_xml DEFINITION.
RETURNING
VALUE(rv_xml) TYPE string.
PRIVATE SECTION.
DATA mo_heap TYPE REF TO lcl_heap.
DATA mo_heap TYPE REF TO lcl_heap.
DATA ms_options TYPE kernel_call_transformation=>ty_options.
ENDCLASS.

CLASS lcl_heap IMPLEMENTATION.
Expand Down Expand Up @@ -110,6 +112,8 @@ CLASS lcl_data_to_xml IMPLEMENTATION.
ELSE.
mo_heap = io_heap.
ENDIF.

ms_options = is_options.
ENDMETHOD.

METHOD serialize_heap.
Expand All @@ -127,13 +131,20 @@ CLASS lcl_data_to_xml IMPLEMENTATION.
FIELD-SYMBOLS <table> TYPE ANY TABLE.
FIELD-SYMBOLS <field> TYPE any.


lo_type = cl_abap_typedescr=>describe_by_data( iv_ref->* ).

CASE lo_type->kind.
WHEN cl_abap_typedescr=>kind_struct.
lo_struc ?= lo_type.
lt_comps = lo_struc->get_components( ).
ASSIGN iv_ref->* TO <any>.

IF ms_options-initial_components = kernel_call_transformation=>gc_options-suppress AND <any> IS INITIAL.
rv_xml = rv_xml && |<{ iv_name }/>|.
RETURN.
ENDIF.

rv_xml = rv_xml && |<{ iv_name }>|.
LOOP AT lt_comps INTO ls_compo.
ASSIGN COMPONENT ls_compo-name OF STRUCTURE <any> TO <field>.
Expand All @@ -144,6 +155,10 @@ CLASS lcl_data_to_xml IMPLEMENTATION.
ENDLOOP.
rv_xml = rv_xml && |</{ iv_name }>|.
WHEN cl_abap_typedescr=>kind_elem.
IF ms_options-initial_components = kernel_call_transformation=>gc_options-suppress AND iv_ref->* IS INITIAL.
RETURN.
ENDIF.

IF lo_type->type_kind = cl_abap_typedescr=>typekind_string AND iv_ref->* IS INITIAL.
rv_xml = rv_xml && |<{ iv_name }/>|.
ELSE.
Expand All @@ -154,6 +169,12 @@ CLASS lcl_data_to_xml IMPLEMENTATION.
ENDIF.
WHEN cl_abap_typedescr=>kind_table.
ASSIGN iv_ref->* TO <table>.

IF ms_options-initial_components = kernel_call_transformation=>gc_options-suppress AND <table> IS INITIAL.
rv_xml = rv_xml && |<{ iv_name }/>|.
RETURN.
ENDIF.

rv_xml = rv_xml && |<{ iv_name }>|.
LOOP AT <table> ASSIGNING <any>.
GET REFERENCE OF <any> INTO lv_ref.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ CLASS ltcl_call_transformation DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATI
METHODS obj_static_attr FOR TESTING RAISING cx_static_check.
METHODS dynamic_source FOR TESTING RAISING cx_static_check.
METHODS dynamic_source_ixml FOR TESTING RAISING cx_static_check.
METHODS suppress1 FOR TESTING RAISING cx_static_check.
METHODS suppress2 FOR TESTING RAISING cx_static_check.
METHODS suppress3 FOR TESTING RAISING cx_static_check.
ENDCLASS.

CLASS ltcl_call_transformation IMPLEMENTATION.
Expand Down Expand Up @@ -881,4 +884,62 @@ CLASS ltcl_call_transformation IMPLEMENTATION.

ENDMETHOD.

METHOD suppress1.

DATA: BEGIN OF ls_foo,
field TYPE i,
END OF ls_foo.

DATA lv_xml TYPE string.

CALL TRANSFORMATION id
OPTIONS initial_components = 'suppress'
SOURCE foo = ls_foo
RESULT XML lv_xml.

cl_abap_unit_assert=>assert_char_cp(
act = lv_xml
exp = '*<asx:values><FOO/></asx:values>*' ).

ENDMETHOD.

METHOD suppress2.

DATA: BEGIN OF ls_foo,
field1 TYPE i,
field2 TYPE i,
END OF ls_foo.

DATA lv_xml TYPE string.

ls_foo-field2 = 2.

CALL TRANSFORMATION id
OPTIONS initial_components = 'suppress'
SOURCE foo = ls_foo
RESULT XML lv_xml.

cl_abap_unit_assert=>assert_char_cp(
act = lv_xml
exp = '*<FOO><FIELD2>2</FIELD2></FOO>*' ).

ENDMETHOD.

METHOD suppress3.

DATA lt_foo TYPE STANDARD TABLE OF i WITH DEFAULT KEY.
DATA lv_xml TYPE string.


CALL TRANSFORMATION id
OPTIONS initial_components = 'suppress'
SOURCE foo = lt_foo
RESULT XML lv_xml.

cl_abap_unit_assert=>assert_char_cp(
act = lv_xml
exp = '*<asx:values><FOO/></asx:values>*' ).

ENDMETHOD.

ENDCLASS.