diff --git a/src/#mbtools#cl_string_map.clas.abap b/src/#mbtools#cl_string_map.clas.abap index fe66eb7..21c05ce 100644 --- a/src/#mbtools#cl_string_map.clas.abap +++ b/src/#mbtools#cl_string_map.clas.abap @@ -5,7 +5,7 @@ CLASS /mbtools/cl_string_map DEFINITION PUBLIC SECTION. - CONSTANTS version TYPE string VALUE 'v1.0.3'. + CONSTANTS version TYPE string VALUE 'v1.0.4'. CONSTANTS origin TYPE string VALUE 'https://github.com/sbcgua/abap-string-map'. CONSTANTS license TYPE string VALUE 'MIT'. @@ -49,6 +49,11 @@ CLASS /mbtools/cl_string_map DEFINITION !iv_val TYPE clike RETURNING VALUE(ro_map) TYPE REF TO /mbtools/cl_string_map. + METHODS setx + IMPORTING + !iv_str TYPE csequence + RETURNING + VALUE(ro_map) TYPE REF TO /mbtools/cl_string_map. METHODS size RETURNING VALUE(rv_size) TYPE i. @@ -98,6 +103,9 @@ CLASS /mbtools/cl_string_map DEFINITION METHODS to_string RETURNING VALUE(rv_string) TYPE string. + METHODS to_entries + CHANGING + !ct_entries TYPE STANDARD TABLE. METHODS strict IMPORTING @@ -382,6 +390,32 @@ CLASS /mbtools/cl_string_map IMPLEMENTATION. ENDMETHOD. + METHOD setx. + + DATA lv_key TYPE string. + DATA lv_val TYPE string. + + ro_map = me. + + IF iv_str IS INITIAL. + RETURN. + ENDIF. + + SPLIT iv_str AT ':' INTO lv_key lv_val. + CONDENSE lv_key. + CONDENSE lv_val. + + IF lv_key IS INITIAL. + RETURN. + ENDIF. + + set( + iv_key = lv_key + iv_val = lv_val ). + + ENDMETHOD. + + METHOD size. rv_size = lines( mt_entries ). @@ -395,6 +429,52 @@ CLASS /mbtools/cl_string_map IMPLEMENTATION. ENDMETHOD. + METHOD to_entries. + + DATA lo_ttype TYPE REF TO cl_abap_tabledescr. + DATA lo_dtype TYPE REF TO cl_abap_datadescr. + DATA lo_stype TYPE REF TO cl_abap_structdescr. + FIELD-SYMBOLS LIKE LINE OF lo_stype->components. + FIELD-SYMBOLS LIKE LINE OF mt_entries. + FIELD-SYMBOLS TYPE any. + FIELD-SYMBOLS TYPE any. + FIELD-SYMBOLS TYPE any. + + lo_ttype ?= cl_abap_typedescr=>describe_by_data( ct_entries ). + lo_dtype = lo_ttype->get_table_line_type( ). + + IF lo_dtype->kind <> cl_abap_typedescr=>kind_struct. + lcx_error=>raise( 'Unsupported table line type' ). + ENDIF. + + lo_stype ?= lo_dtype. + + IF lines( lo_stype->components ) <> 2. + lcx_error=>raise( 'Wrong number of fields in target table (must be 2)' ). + ENDIF. + + + LOOP AT lo_stype->components ASSIGNING . + IF NOT ( -type_kind = cl_abap_typedescr=>typekind_char OR -type_kind = cl_abap_typedescr=>typekind_string ). + lcx_error=>raise( 'Wrong type of fields in target table (must be char or string)' ). + ENDIF. + ENDLOOP. + + + LOOP AT mt_entries ASSIGNING . + APPEND INITIAL LINE TO ct_entries ASSIGNING . + ASSERT sy-subrc = 0. + ASSIGN COMPONENT 1 OF STRUCTURE TO . + ASSERT sy-subrc = 0. + ASSIGN COMPONENT 2 OF STRUCTURE TO . + ASSERT sy-subrc = 0. + = -k. + = -v. + ENDLOOP. + + ENDMETHOD. + + METHOD to_string. DATA lv_size TYPE i. diff --git a/src/#mbtools#cl_string_map.clas.testclasses.abap b/src/#mbtools#cl_string_map.clas.testclasses.abap index 027c27f..a293448 100644 --- a/src/#mbtools#cl_string_map.clas.testclasses.abap +++ b/src/#mbtools#cl_string_map.clas.testclasses.abap @@ -19,6 +19,7 @@ CLASS ltcl_string_map DEFINITION METHODS keys_values FOR TESTING. METHODS case_insensitive FOR TESTING. METHODS set_clike FOR TESTING. + METHODS setx FOR TESTING. METHODS strict FOR TESTING. METHODS freeze FOR TESTING. @@ -32,6 +33,7 @@ CLASS ltcl_string_map DEFINITION METHODS to_struc FOR TESTING. METHODS to_string FOR TESTING. + METHODS to_entries FOR TESTING. METHODS create_from FOR TESTING. METHODS case_insensitive_create FOR TESTING. @@ -838,4 +840,119 @@ CLASS ltcl_string_map IMPLEMENTATION. ENDMETHOD. + + METHOD to_entries. + + TYPES: + BEGIN OF lty_str, + a TYPE string, + b TYPE string, + END OF lty_str, + lty_str_t TYPE STANDARD TABLE OF lty_str, + BEGIN OF lty_char, + a TYPE c LENGTH 10, + b TYPE c LENGTH 10, + END OF lty_char, + lty_char_t TYPE STANDARD TABLE OF lty_char, + BEGIN OF lty_bad1, + a TYPE c LENGTH 10, + END OF lty_bad1, + lty_bad1_t TYPE STANDARD TABLE OF lty_bad1, + BEGIN OF lty_bad2, + a TYPE i, + b TYPE i, + END OF lty_bad2, + lty_bad2_t TYPE STANDARD TABLE OF lty_bad2. + + DATA lo_cut TYPE REF TO /mbtools/cl_string_map. + DATA lt_str_act TYPE lty_str_t. + DATA lt_str_exp TYPE lty_str_t. + DATA ls_str LIKE LINE OF lt_str_act. + DATA lt_char_act TYPE lty_char_t. + DATA lt_char_exp TYPE lty_char_t. + DATA ls_char LIKE LINE OF lt_char_act. + DATA lx TYPE REF TO lcx_error. + DATA lt_bad1 TYPE lty_bad1_t. + DATA lt_bad2 TYPE lty_bad2_t. + DATA lt_bad3 TYPE string_table. + lo_cut = /mbtools/cl_string_map=>create( `x=1,y=2` ). + + + ls_str-a = 'x'. + ls_str-b = '1'. + APPEND ls_str TO lt_str_exp. + ls_str-a = 'y'. + ls_str-b = '2'. + APPEND ls_str TO lt_str_exp. + lo_cut->to_entries( CHANGING ct_entries = lt_str_act ). + cl_abap_unit_assert=>assert_equals( + act = lt_str_act + exp = lt_str_exp ). + + ls_char-a = 'x'. + ls_char-b = '1'. + APPEND ls_char TO lt_char_exp. + ls_char-a = 'y'. + ls_char-b = '2'. + APPEND ls_char TO lt_char_exp. + lo_cut->to_entries( CHANGING ct_entries = lt_char_act ). + cl_abap_unit_assert=>assert_equals( + act = lt_char_act + exp = lt_char_exp ). + + + TRY. + lo_cut->to_entries( CHANGING ct_entries = lt_bad1 ). + cl_abap_unit_assert=>fail( ). + CATCH lcx_error INTO lx. + cl_abap_unit_assert=>assert_char_cp( + act = lx->get_text( ) + exp = '*number*' ). + ENDTRY. + + + TRY. + lo_cut->to_entries( CHANGING ct_entries = lt_bad2 ). + cl_abap_unit_assert=>fail( ). + CATCH lcx_error INTO lx. + cl_abap_unit_assert=>assert_char_cp( + act = lx->get_text( ) + exp = '*type*' ). + ENDTRY. + + + TRY. + lo_cut->to_entries( CHANGING ct_entries = lt_bad3 ). + cl_abap_unit_assert=>fail( ). + CATCH lcx_error INTO lx. + cl_abap_unit_assert=>assert_char_cp( + act = lx->get_text( ) + exp = '*table line*' ). + ENDTRY. + + ENDMETHOD. + + METHOD setx. + + DATA lo_cut TYPE REF TO /mbtools/cl_string_map. + + lo_cut = /mbtools/cl_string_map=>create( ). + + lo_cut->setx( 'a:1' ). + lo_cut->setx( |b : 2| ). + lo_cut->setx( ':c' ). + lo_cut->setx( '' ). + + cl_abap_unit_assert=>assert_equals( + exp = 2 + act = lo_cut->size( ) ). + cl_abap_unit_assert=>assert_equals( + exp = lo_cut->get( 'a' ) + act = '1' ). + cl_abap_unit_assert=>assert_equals( + exp = lo_cut->get( 'b' ) + act = '2' ). + + ENDMETHOD. + ENDCLASS.