Skip to content

Commit

Permalink
Merge pull request #29 from sbcgua/improvements
Browse files Browse the repository at this point in the history
Type improvements and description line autodetection
  • Loading branch information
sbcgua authored Nov 7, 2023
2 parents 29dbdb0 + a51b998 commit 3f2955e
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 71 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.vscode
.ssh

# Artifacts
js-transpiled
# Test related
abaplint-deps
transpiled
node_modules
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,14 @@ zcl_text2tab_parser=>create( i_pattern = ls_birthday i_begin_comment = '*' ).

The char '*' must have the first position in the text line. Otherwise it isn't interpreted as a comment.

The comment (or rather the description) line can also be auto-detected with `i_begin_comment = zif_text2tab=>c_auto_detect_by_space`. It tries to find spaces in the first line of the text and, if found, ignores this first line. The idea is that the descriptions will probably have spaces, while abap field names will not:

```text
Name Date of birth <<< containes spaces
NAME BIRTHDAY
JOHN 01.01.1990
```

## Error message redefinition

The exception class - `zcx_text2tab_error` - exposes `structure`, `field`, `line` and `msg` attributes (and some others). They can be used to reformat the message text if needed. For example:
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ v2.3.5, 2023-07-??
------------------
* code and type cleanups
+ possibility to keep fields_only order during serialization #25
+ auto detection of description line with spaces

v2.3.4, 2021-10-10
------------------
Expand Down
39 changes: 17 additions & 22 deletions src/zcl_text2tab_parser.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class zcl_text2tab_parser definition
ty_amount_format type c length 2.
types:
ty_date_format type c length 4.
types:
ty_begin_comment type c length 1.
types:
tt_field_map type standard table of i with default key.

Expand All @@ -30,7 +28,7 @@ class zcl_text2tab_parser definition
!i_ignore_nonflat type abap_bool default abap_false
!i_amount_format type ty_amount_format optional
!i_date_format type ty_date_format optional
!i_begin_comment type ty_begin_comment optional
!i_begin_comment type zif_text2tab=>ty_begin_comment optional
!i_deep_provider type ref to zif_text2tab_deep_provider optional
returning
value(ro_parser) type ref to zcl_text2tab_parser
Expand Down Expand Up @@ -68,7 +66,7 @@ class zcl_text2tab_parser definition
data mv_current_field type string .
data mv_line_index type i .
data mv_is_typeless type abap_bool .
data mv_begin_comment type ty_begin_comment .
data mv_begin_comment type zif_text2tab=>ty_begin_comment .
data mt_ignore_exits type sorted table of abap_editmask with unique key table_line.

data mt_components type zcl_text2tab_utils=>tt_comp_descr .
Expand Down Expand Up @@ -612,7 +610,7 @@ CLASS ZCL_TEXT2TAB_PARSER IMPLEMENTATION.
if l_unquoted co '0123456789'.
e_field = l_unquoted.
else.
sy-subrc = 4.
raise_error( i_msg = 'Field parsing failed' i_code = 'PF' ). "#EC NOTEXT
endif.

when cl_abap_typedescr=>typekind_time. " Time
Expand All @@ -626,7 +624,7 @@ CLASS ZCL_TEXT2TAB_PARSER IMPLEMENTATION.
if l_unquoted co '0123456789'.
e_field = l_unquoted.
else.
sy-subrc = 4.
raise_error( i_msg = 'Field parsing failed' i_code = 'PF' ). "#EC NOTEXT
endif.

when cl_abap_typedescr=>typekind_hex. " Raw
Expand All @@ -637,18 +635,14 @@ CLASS ZCL_TEXT2TAB_PARSER IMPLEMENTATION.
try .
e_field = l_unquoted.
catch cx_sy_conversion_no_raw cx_sy_conversion_error.
sy-subrc = 4.
raise_error( i_msg = 'Field parsing failed' i_code = 'PF' ). "#EC NOTEXT
endtry.

when others.
raise_error( i_msg = 'Unsupported field type' i_code = 'UT' ). "#EC NOTEXT

endcase.

if sy-subrc is not initial.
raise_error( i_msg = 'Field parsing failed' i_code = 'PF' ). "#EC NOTEXT
endif.

endmethod.


Expand Down Expand Up @@ -826,17 +820,16 @@ CLASS ZCL_TEXT2TAB_PARSER IMPLEMENTATION.

method parse_time.

call function 'CONVERT_TIME_INPUT'
exporting
input = i_value
importing
output = r_time
exceptions
plausibility_check_failed = 2
wrong_format_in_input = 4.
if sy-subrc <> 0.
try.
cl_abap_timefm=>conv_time_ext_to_int(
exporting
time_ext = i_value
is_24_allowed = abap_true
importing
time_int = r_time ).
catch cx_abap_timefm_invalid.
raise_error( i_msg = |{ i_value } is not a valid time| i_code = 'IT' ).
endif.
endtry.

endmethod.

Expand Down Expand Up @@ -869,7 +862,9 @@ CLASS ZCL_TEXT2TAB_PARSER IMPLEMENTATION.
raise_error( i_msg = 'Container type does not fit pattern' i_code = 'TE' ). "#EC NOTEXT
endif.

lt_data = zcl_text2tab_utils=>break_to_lines( i_text = i_data i_begin_comment = mv_begin_comment ).
lt_data = zcl_text2tab_utils=>break_to_lines(
i_text = i_data
i_begin_comment = mv_begin_comment ).

" Read and process header line
if i_has_head = abap_true.
Expand Down
22 changes: 11 additions & 11 deletions src/zcl_text2tab_parser.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ class ltcl_text2tab_parser_test definition for testing
e_dummy_tab_s type tt_dummy_str
e_dummy_header type string
e_dummy_string type string
e_map_corresp type int4_table
e_map type int4_table.
e_map_corresp type zcl_text2tab_parser=>tt_field_map
e_map type zcl_text2tab_parser=>tt_field_map.
methods get_dummy_data_with_time
exporting
e_exp_result type ty_dummy_with_time
Expand Down Expand Up @@ -434,7 +434,7 @@ class ltcl_text2tab_parser_test implementation.
end of wrong_struc.

data:
l_exp_code type char2,
l_exp_code type c length 2,
dummy_val type c length 40 ##NEEDED,
dummy_tab_act type tt_dummy ##NEEDED,
l_string type string,
Expand Down Expand Up @@ -846,9 +846,9 @@ class ltcl_text2tab_parser_test implementation.
data:
l_header type string,
l_header_bak type string,
l_exp_code type char2,
l_act_map type int4_table,
l_exp_map type int4_table,
l_exp_code type c length 2,
l_act_map type zcl_text2tab_parser=>tt_field_map,
l_exp_map type zcl_text2tab_parser=>tt_field_map,
l_ren_map type zcl_text2tab_utils=>th_field_name_map,
l_rename like line of l_ren_map,
lx type ref to zcx_text2tab_error.
Expand Down Expand Up @@ -1001,8 +1001,8 @@ class ltcl_text2tab_parser_test implementation.

data:
l_header type string,
l_act_map type int4_table,
l_exp_map type int4_table,
l_act_map type zcl_text2tab_parser=>tt_field_map,
l_exp_map type zcl_text2tab_parser=>tt_field_map,
l_ren_map type zcl_text2tab_utils=>th_field_name_map.

get_dummy_data(
Expand All @@ -1029,8 +1029,8 @@ class ltcl_text2tab_parser_test implementation.
data:
l_dataline type string,
l_header_bak type string,
l_exp_code type char2,
lt_map type int4_table,
l_exp_code type c length 2,
lt_map type zcl_text2tab_parser=>tt_field_map,
lx type ref to zcx_text2tab_error.

get_dummy_data( importing e_dummy_header = l_header_bak
Expand Down Expand Up @@ -1072,7 +1072,7 @@ class ltcl_text2tab_parser_test implementation.
dummy_tab_act type tt_dummy,
l_string type string,
lt_data type table of string,
lt_map type int4_table,
lt_map type zcl_text2tab_parser=>tt_field_map,
lx type ref to zcx_text2tab_error.

get_dummy_data( importing e_dummy_tab = dummy_tab_exp
Expand Down
6 changes: 3 additions & 3 deletions src/zcl_text2tab_serializer.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class zcl_text2tab_serializer definition
types:
ts_fields_list type sorted table of abap_compname with unique key table_line.

constants c_crlf like cl_abap_char_utilities=>cr_lf value cl_abap_char_utilities=>cr_lf. "#EC NOTEXT
constants c_lf like cl_abap_char_utilities=>newline value cl_abap_char_utilities=>newline. "#EC NOTEXT
constants c_tab like cl_abap_char_utilities=>horizontal_tab value cl_abap_char_utilities=>horizontal_tab. "#EC NOTEXT
constants c_crlf like cl_abap_char_utilities=>cr_lf value cl_abap_char_utilities=>cr_lf.
constants c_lf like cl_abap_char_utilities=>newline value cl_abap_char_utilities=>newline.
constants c_tab like cl_abap_char_utilities=>horizontal_tab value cl_abap_char_utilities=>horizontal_tab.

constants:
begin of c_header,
Expand Down
14 changes: 11 additions & 3 deletions src/zcl_text2tab_serializer.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ltcl_text2tab_serializer_test definition final
traw type x length 1,
tstring type string,
talpha type veri_alpha,
tdecimal type dmbtr,
tdecimal type p length 13 decimals 2, " dmbtr
tnumber type n length 4,
tinteger type i,
tfloat type f,
Expand Down Expand Up @@ -50,6 +50,7 @@ class ltcl_text2tab_serializer_test definition final

methods serialize_date for testing raising zcx_text2tab_error.
methods serialize_field for testing raising zcx_text2tab_error.
methods serialize_field_cunit for testing raising zcx_text2tab_error.
methods negatives for testing.
methods create for testing.

Expand Down Expand Up @@ -500,8 +501,6 @@ class ltcl_text2tab_serializer_test implementation.
data l_act type string.
data ls_dummy type ty_dummy.
data ld_type type ref to cl_abap_structdescr.
data lv_meins type meins.
data ls_comp like line of mt_field_components.

ld_type ?= cl_abap_typedescr=>describe_by_data( ls_dummy ).
mt_field_components = zcl_text2tab_utils=>describe_struct( ld_type ).
Expand All @@ -526,6 +525,15 @@ class ltcl_text2tab_serializer_test implementation.
test_field( f = 'TDATE' v = '00000000' exp = '' ).
test_field( f = 'TDATE' v = '' exp = '' ).

endmethod.

method serialize_field_cunit.

data lx type ref to zcx_text2tab_error.
data l_act type string.
data lv_meins type meins.
data ls_comp like line of mt_field_components.

" Unit of measurement (CUNIT CONV)
lv_meins = 'KG'.
ls_comp-type_kind = cl_abap_typedescr=>typekind_char.
Expand Down
39 changes: 24 additions & 15 deletions src/zcl_text2tab_utils.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ class ZCL_TEXT2TAB_UTILS definition

public section.

constants c_tab like cl_abap_char_utilities=>horizontal_tab value cl_abap_char_utilities=>horizontal_tab. "#EC NOTEXT
constants c_crlf like cl_abap_char_utilities=>cr_lf value cl_abap_char_utilities=>cr_lf. "#EC NOTEXT
constants c_lf like cl_abap_char_utilities=>newline value cl_abap_char_utilities=>newline. "#EC NOTEXT

types:
begin of ty_comp_descr.
include type abap_compdescr.
Expand Down Expand Up @@ -81,7 +77,7 @@ class ZCL_TEXT2TAB_UTILS definition
class-methods break_to_lines
importing
!i_text type string
!i_begin_comment type c
!i_begin_comment type zif_text2tab=>ty_begin_comment
returning
value(rt_tab) type string_table .
class-methods get_safe_struc_descr
Expand All @@ -105,6 +101,7 @@ class ZCL_TEXT2TAB_UTILS definition
value(rr_dref) type ref to data
raising
zcx_text2tab_error .

protected section.
private section.
types:
Expand Down Expand Up @@ -134,26 +131,38 @@ CLASS ZCL_TEXT2TAB_UTILS IMPLEMENTATION.


method break_to_lines.
data:
l_found type i,
l_break type string value c_crlf.
field-symbols: <line> type string.

constants lc_crlf like cl_abap_char_utilities=>cr_lf value cl_abap_char_utilities=>cr_lf.
constants lc_lf like cl_abap_char_utilities=>newline value cl_abap_char_utilities=>newline.

data l_found type i.
data l_break type string value lc_crlf.
field-symbols <l> type string.

" Detect line break
l_found = find( val = i_text sub = c_crlf ).
l_found = find( val = i_text sub = lc_crlf ).
if l_found < 0.
l_found = find( val = i_text sub = c_lf ).
l_found = find( val = i_text sub = lc_lf ).
if l_found >= 0.
l_break = c_lf.
l_break = lc_lf.
endif.
endif.

split i_text at l_break into table rt_tab.

if i_begin_comment <> space.
loop at rt_tab assigning <line>.
if i_begin_comment = zif_text2tab=>c_auto_detect_by_space.
read table rt_tab index 1 assigning <l>.
if sy-subrc = 0 and find( val = <l> sub = ` ` ) > 0.
" Idea is that a text may contain (optional!) description row and then field name row
" Field names will not contain spaces, because it is not allowed in abap
" And the desciptions probably will.
" So not a 100% reliable feature but can be useful for the majority of real cases
delete rt_tab index 1.
endif.
elseif i_begin_comment <> space.
loop at rt_tab assigning <l>.
try.
if <line>+0(1) = i_begin_comment.
if <l>+0(1) = i_begin_comment.
delete rt_tab index sy-tabix.
endif.
catch cx_sy_range_out_of_bounds.
Expand Down
Loading

0 comments on commit 3f2955e

Please sign in to comment.