Skip to content

to_entries feature #11

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

Merged
merged 6 commits into from
Jun 24, 2024
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
6 changes: 2 additions & 4 deletions .abapgit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<DATA>
<NAME>Abap String Map</NAME>
<MASTER_LANGUAGE>E</MASTER_LANGUAGE>
<STARTING_FOLDER>/src/</STARTING_FOLDER>
<FOLDER_LOGIC>PREFIX</FOLDER_LOGIC>
<IGNORE>
<item>/.gitignore</item>
<item>/LICENSE</item>
<item>/README.md</item>
<item>/package.json</item>
<item>/.travis.yml</item>
<item>/.gitlab-ci.yml</item>
<item>/abaplint.json</item>
<item>/azure-pipelines.yml</item>
</IGNORE>
<VERSION_CONSTANT>ZCL_ABAP_STRING_MAP=&gt;VERSION</VERSION_CONSTANT>
</DATA>
</asx:values>
</asx:abap>
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!-- markdownlint-disable MD041 -->
![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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions src/zcl_abap_string_map.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <c> like line of lo_stype->components.
loop at lo_stype->components assigning <c>.
if not ( <c>-type_kind = cl_abap_typedescr=>typekind_char or <c>-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 <entry> like line of mt_entries.
field-symbols <to> type any.
field-symbols <k> type any.
field-symbols <v> type any.
loop at mt_entries assigning <entry>.
append initial line to ct_entries assigning <to>.
assert sy-subrc = 0.
assign component 1 of structure <to> to <k>.
assert sy-subrc = 0.
assign component 2 of structure <to> to <v>.
assert sy-subrc = 0.
<k> = <entry>-k.
<v> = <entry>-v.
endloop.

endmethod.


method to_string.

data lv_size type i.
Expand Down
90 changes: 90 additions & 0 deletions src/zcl_abap_string_map.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Loading