diff --git a/.abapgit.xml b/.abapgit.xml index 3f9057f..daaa39c 100644 --- a/.abapgit.xml +++ b/.abapgit.xml @@ -2,6 +2,7 @@ + Abap String Map E /src/ PREFIX @@ -9,12 +10,9 @@ /.gitignore /LICENSE /README.md - /package.json - /.travis.yml - /.gitlab-ci.yml /abaplint.json - /azure-pipelines.yml + ZCL_ABAP_STRING_MAP=>VERSION diff --git a/README.md b/README.md index bd5d27f..97be05f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + ![abaplint](https://github.com/sbcgua/abap-string-map/workflows/abaplint/badge.svg) ![abap package version](https://img.shields.io/endpoint?url=https://shield.abap.space/version-shield-json/github/sbcgua/abap-string-map/src/zcl_abap_string_map.clas.abap) @@ -83,6 +84,19 @@ lt_entries = value #( lo_map->from_entries( lt_entries ). ``` +- implements `to_entries` - the opposite to `from_entries`, saves data to a table with 2 char-like components + +```abap +types: + begin of ty_my_key_value, + a type string, + b type c length 10, + end of ty_my_key_value. + +data lt_entries type table of ty_my_key_value. +lo_map->to_entries( changing ct_entries = lt_entries ). +``` + - implements `from_string` - this parses string of pairs like `a = b, x = y` into map. Spaces are condensed. `to_string` renders in string representation (careful with case insensitive - keys will be upper-cased). ```abap @@ -126,5 +140,6 @@ lo_map->set( For more examples see [unit tests code](https://github.com/sbcgua/abap-string-map/blob/master/src/zcl_abap_string_map.clas.testclasses.abap) -### Blog posts -- https://blogs.sap.com/2020/08/04/bicycles.-1-string-map/ +## Blog posts + +- [Bicycles: String map](https://blogs.sap.com/2020/08/04/bicycles.-1-string-map) diff --git a/changelog.txt b/changelog.txt index 2a62c9c..c8a8cf6 100644 --- a/changelog.txt +++ b/changelog.txt @@ -8,6 +8,10 @@ Legend + : added - : removed +v1.0.4, 2024-06-24 +------------------ ++ to_entries - saves entries to a standard table with 2 char-like components (#11) + v1.0.3, 2021-07-25 ------------------ ! BREAKING: from_struc does not clears the state anymore, so all from* methods ADD values diff --git a/src/zcl_abap_string_map.clas.abap b/src/zcl_abap_string_map.clas.abap index 481f91d..674b3d1 100644 --- a/src/zcl_abap_string_map.clas.abap +++ b/src/zcl_abap_string_map.clas.abap @@ -98,6 +98,9 @@ class zcl_abap_string_map definition methods to_string returning value(rv_string) type string. + methods to_entries + changing + !ct_entries type standard table. methods strict importing @@ -393,6 +396,50 @@ CLASS ZCL_ABAP_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. + + 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. + + field-symbols like line of lo_stype->components. + 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. + + field-symbols like line of mt_entries. + field-symbols type any. + field-symbols type any. + field-symbols type any. + 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/zcl_abap_string_map.clas.testclasses.abap b/src/zcl_abap_string_map.clas.testclasses.abap index 93f2b40..a65e4fb 100644 --- a/src/zcl_abap_string_map.clas.testclasses.abap +++ b/src/zcl_abap_string_map.clas.testclasses.abap @@ -32,6 +32,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. @@ -837,4 +838,93 @@ 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 zcl_abap_string_map. + lo_cut = zcl_abap_string_map=>create( `x=1,y=2` ). + + 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. + + 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 ). + + data lx type ref to lcx_error. + data lt_bad1 type lty_bad1_t. + 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. + + data lt_bad2 type lty_bad2_t. + 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. + + data lt_bad3 type string_table. + 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. + endclass.