Skip to content

Commit 3f2955e

Browse files
authored
Merge pull request #29 from sbcgua/improvements
Type improvements and description line autodetection
2 parents 29dbdb0 + a51b998 commit 3f2955e

12 files changed

+144
-71
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
.vscode
55
.ssh
66

7-
# Artifacts
8-
js-transpiled
7+
# Test related
8+
abaplint-deps
9+
transpiled
910
node_modules

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,14 @@ zcl_text2tab_parser=>create( i_pattern = ls_birthday i_begin_comment = '*' ).
357357

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

360+
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:
361+
362+
```text
363+
Name Date of birth <<< containes spaces
364+
NAME BIRTHDAY
365+
JOHN 01.01.1990
366+
```
367+
360368
## Error message redefinition
361369

362370
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:

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ v2.3.5, 2023-07-??
1212
------------------
1313
* code and type cleanups
1414
+ possibility to keep fields_only order during serialization #25
15+
+ auto detection of description line with spaces
1516

1617
v2.3.4, 2021-10-10
1718
------------------

src/zcl_text2tab_parser.clas.abap

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ class zcl_text2tab_parser definition
1010
ty_amount_format type c length 2.
1111
types:
1212
ty_date_format type c length 4.
13-
types:
14-
ty_begin_comment type c length 1.
1513
types:
1614
tt_field_map type standard table of i with default key.
1715

@@ -30,7 +28,7 @@ class zcl_text2tab_parser definition
3028
!i_ignore_nonflat type abap_bool default abap_false
3129
!i_amount_format type ty_amount_format optional
3230
!i_date_format type ty_date_format optional
33-
!i_begin_comment type ty_begin_comment optional
31+
!i_begin_comment type zif_text2tab=>ty_begin_comment optional
3432
!i_deep_provider type ref to zif_text2tab_deep_provider optional
3533
returning
3634
value(ro_parser) type ref to zcl_text2tab_parser
@@ -68,7 +66,7 @@ class zcl_text2tab_parser definition
6866
data mv_current_field type string .
6967
data mv_line_index type i .
7068
data mv_is_typeless type abap_bool .
71-
data mv_begin_comment type ty_begin_comment .
69+
data mv_begin_comment type zif_text2tab=>ty_begin_comment .
7270
data mt_ignore_exits type sorted table of abap_editmask with unique key table_line.
7371

7472
data mt_components type zcl_text2tab_utils=>tt_comp_descr .
@@ -612,7 +610,7 @@ CLASS ZCL_TEXT2TAB_PARSER IMPLEMENTATION.
612610
if l_unquoted co '0123456789'.
613611
e_field = l_unquoted.
614612
else.
615-
sy-subrc = 4.
613+
raise_error( i_msg = 'Field parsing failed' i_code = 'PF' ). "#EC NOTEXT
616614
endif.
617615

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

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

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

646644
endcase.
647645

648-
if sy-subrc is not initial.
649-
raise_error( i_msg = 'Field parsing failed' i_code = 'PF' ). "#EC NOTEXT
650-
endif.
651-
652646
endmethod.
653647

654648

@@ -826,17 +820,16 @@ CLASS ZCL_TEXT2TAB_PARSER IMPLEMENTATION.
826820

827821
method parse_time.
828822

829-
call function 'CONVERT_TIME_INPUT'
830-
exporting
831-
input = i_value
832-
importing
833-
output = r_time
834-
exceptions
835-
plausibility_check_failed = 2
836-
wrong_format_in_input = 4.
837-
if sy-subrc <> 0.
823+
try.
824+
cl_abap_timefm=>conv_time_ext_to_int(
825+
exporting
826+
time_ext = i_value
827+
is_24_allowed = abap_true
828+
importing
829+
time_int = r_time ).
830+
catch cx_abap_timefm_invalid.
838831
raise_error( i_msg = |{ i_value } is not a valid time| i_code = 'IT' ).
839-
endif.
832+
endtry.
840833

841834
endmethod.
842835

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

872-
lt_data = zcl_text2tab_utils=>break_to_lines( i_text = i_data i_begin_comment = mv_begin_comment ).
865+
lt_data = zcl_text2tab_utils=>break_to_lines(
866+
i_text = i_data
867+
i_begin_comment = mv_begin_comment ).
873868

874869
" Read and process header line
875870
if i_has_head = abap_true.

src/zcl_text2tab_parser.clas.testclasses.abap

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ class ltcl_text2tab_parser_test definition for testing
119119
e_dummy_tab_s type tt_dummy_str
120120
e_dummy_header type string
121121
e_dummy_string type string
122-
e_map_corresp type int4_table
123-
e_map type int4_table.
122+
e_map_corresp type zcl_text2tab_parser=>tt_field_map
123+
e_map type zcl_text2tab_parser=>tt_field_map.
124124
methods get_dummy_data_with_time
125125
exporting
126126
e_exp_result type ty_dummy_with_time
@@ -434,7 +434,7 @@ class ltcl_text2tab_parser_test implementation.
434434
end of wrong_struc.
435435

436436
data:
437-
l_exp_code type char2,
437+
l_exp_code type c length 2,
438438
dummy_val type c length 40 ##NEEDED,
439439
dummy_tab_act type tt_dummy ##NEEDED,
440440
l_string type string,
@@ -846,9 +846,9 @@ class ltcl_text2tab_parser_test implementation.
846846
data:
847847
l_header type string,
848848
l_header_bak type string,
849-
l_exp_code type char2,
850-
l_act_map type int4_table,
851-
l_exp_map type int4_table,
849+
l_exp_code type c length 2,
850+
l_act_map type zcl_text2tab_parser=>tt_field_map,
851+
l_exp_map type zcl_text2tab_parser=>tt_field_map,
852852
l_ren_map type zcl_text2tab_utils=>th_field_name_map,
853853
l_rename like line of l_ren_map,
854854
lx type ref to zcx_text2tab_error.
@@ -1001,8 +1001,8 @@ class ltcl_text2tab_parser_test implementation.
10011001

10021002
data:
10031003
l_header type string,
1004-
l_act_map type int4_table,
1005-
l_exp_map type int4_table,
1004+
l_act_map type zcl_text2tab_parser=>tt_field_map,
1005+
l_exp_map type zcl_text2tab_parser=>tt_field_map,
10061006
l_ren_map type zcl_text2tab_utils=>th_field_name_map.
10071007

10081008
get_dummy_data(
@@ -1029,8 +1029,8 @@ class ltcl_text2tab_parser_test implementation.
10291029
data:
10301030
l_dataline type string,
10311031
l_header_bak type string,
1032-
l_exp_code type char2,
1033-
lt_map type int4_table,
1032+
l_exp_code type c length 2,
1033+
lt_map type zcl_text2tab_parser=>tt_field_map,
10341034
lx type ref to zcx_text2tab_error.
10351035

10361036
get_dummy_data( importing e_dummy_header = l_header_bak
@@ -1072,7 +1072,7 @@ class ltcl_text2tab_parser_test implementation.
10721072
dummy_tab_act type tt_dummy,
10731073
l_string type string,
10741074
lt_data type table of string,
1075-
lt_map type int4_table,
1075+
lt_map type zcl_text2tab_parser=>tt_field_map,
10761076
lx type ref to zcx_text2tab_error.
10771077

10781078
get_dummy_data( importing e_dummy_tab = dummy_tab_exp

src/zcl_text2tab_serializer.clas.abap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class zcl_text2tab_serializer definition
1717
types:
1818
ts_fields_list type sorted table of abap_compname with unique key table_line.
1919

20-
constants c_crlf like cl_abap_char_utilities=>cr_lf value cl_abap_char_utilities=>cr_lf. "#EC NOTEXT
21-
constants c_lf like cl_abap_char_utilities=>newline value cl_abap_char_utilities=>newline. "#EC NOTEXT
22-
constants c_tab like cl_abap_char_utilities=>horizontal_tab value cl_abap_char_utilities=>horizontal_tab. "#EC NOTEXT
20+
constants c_crlf like cl_abap_char_utilities=>cr_lf value cl_abap_char_utilities=>cr_lf.
21+
constants c_lf like cl_abap_char_utilities=>newline value cl_abap_char_utilities=>newline.
22+
constants c_tab like cl_abap_char_utilities=>horizontal_tab value cl_abap_char_utilities=>horizontal_tab.
2323

2424
constants:
2525
begin of c_header,

src/zcl_text2tab_serializer.clas.testclasses.abap

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ltcl_text2tab_serializer_test definition final
1313
traw type x length 1,
1414
tstring type string,
1515
talpha type veri_alpha,
16-
tdecimal type dmbtr,
16+
tdecimal type p length 13 decimals 2, " dmbtr
1717
tnumber type n length 4,
1818
tinteger type i,
1919
tfloat type f,
@@ -50,6 +50,7 @@ class ltcl_text2tab_serializer_test definition final
5050

5151
methods serialize_date for testing raising zcx_text2tab_error.
5252
methods serialize_field for testing raising zcx_text2tab_error.
53+
methods serialize_field_cunit for testing raising zcx_text2tab_error.
5354
methods negatives for testing.
5455
methods create for testing.
5556

@@ -500,8 +501,6 @@ class ltcl_text2tab_serializer_test implementation.
500501
data l_act type string.
501502
data ls_dummy type ty_dummy.
502503
data ld_type type ref to cl_abap_structdescr.
503-
data lv_meins type meins.
504-
data ls_comp like line of mt_field_components.
505504

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

528+
endmethod.
529+
530+
method serialize_field_cunit.
531+
532+
data lx type ref to zcx_text2tab_error.
533+
data l_act type string.
534+
data lv_meins type meins.
535+
data ls_comp like line of mt_field_components.
536+
529537
" Unit of measurement (CUNIT CONV)
530538
lv_meins = 'KG'.
531539
ls_comp-type_kind = cl_abap_typedescr=>typekind_char.

src/zcl_text2tab_utils.clas.abap

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ class ZCL_TEXT2TAB_UTILS definition
55

66
public section.
77

8-
constants c_tab like cl_abap_char_utilities=>horizontal_tab value cl_abap_char_utilities=>horizontal_tab. "#EC NOTEXT
9-
constants c_crlf like cl_abap_char_utilities=>cr_lf value cl_abap_char_utilities=>cr_lf. "#EC NOTEXT
10-
constants c_lf like cl_abap_char_utilities=>newline value cl_abap_char_utilities=>newline. "#EC NOTEXT
11-
128
types:
139
begin of ty_comp_descr.
1410
include type abap_compdescr.
@@ -81,7 +77,7 @@ class ZCL_TEXT2TAB_UTILS definition
8177
class-methods break_to_lines
8278
importing
8379
!i_text type string
84-
!i_begin_comment type c
80+
!i_begin_comment type zif_text2tab=>ty_begin_comment
8581
returning
8682
value(rt_tab) type string_table .
8783
class-methods get_safe_struc_descr
@@ -105,6 +101,7 @@ class ZCL_TEXT2TAB_UTILS definition
105101
value(rr_dref) type ref to data
106102
raising
107103
zcx_text2tab_error .
104+
108105
protected section.
109106
private section.
110107
types:
@@ -134,26 +131,38 @@ CLASS ZCL_TEXT2TAB_UTILS IMPLEMENTATION.
134131

135132

136133
method break_to_lines.
137-
data:
138-
l_found type i,
139-
l_break type string value c_crlf.
140-
field-symbols: <line> type string.
134+
135+
constants lc_crlf like cl_abap_char_utilities=>cr_lf value cl_abap_char_utilities=>cr_lf.
136+
constants lc_lf like cl_abap_char_utilities=>newline value cl_abap_char_utilities=>newline.
137+
138+
data l_found type i.
139+
data l_break type string value lc_crlf.
140+
field-symbols <l> type string.
141141

142142
" Detect line break
143-
l_found = find( val = i_text sub = c_crlf ).
143+
l_found = find( val = i_text sub = lc_crlf ).
144144
if l_found < 0.
145-
l_found = find( val = i_text sub = c_lf ).
145+
l_found = find( val = i_text sub = lc_lf ).
146146
if l_found >= 0.
147-
l_break = c_lf.
147+
l_break = lc_lf.
148148
endif.
149149
endif.
150150

151151
split i_text at l_break into table rt_tab.
152152

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

0 commit comments

Comments
 (0)