Skip to content

Commit 286e399

Browse files
committed
structured attributes
1 parent 878c65e commit 286e399

7 files changed

+124
-3
lines changed

zcl_mustache_data.clas.abap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ class ZCL_MUSTACHE_DATA definition
55

66
public section.
77

8+
class-methods CREATE_FOR
9+
importing
10+
!IV_NAME type STRING
11+
!IV_VAL type ANY
12+
returning
13+
value(RO_DATA) type ref to ZCL_MUSTACHE_DATA .
814
class-methods GET_FOR
915
importing
1016
!IV_NAME type STRING
@@ -62,6 +68,12 @@ CLASS ZCL_MUSTACHE_DATA IMPLEMENTATION.
6268
endmethod.
6369

6470

71+
method create_for.
72+
create object ro_data.
73+
ro_data->add( iv_name = iv_name iv_val = iv_val ).
74+
endmethod.
75+
76+
6577
method get.
6678
rt_data = mt_data.
6779
endmethod.

zcl_mustache_parser.clas.abap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ CLASS ZCL_MUSTACHE_PARSER IMPLEMENTATION.
185185

186186
rv_token-content = lv_param.
187187

188+
if ( rv_token-type = zif_mustache=>c_token_type-etag
189+
or rv_token-type = zif_mustache=>c_token_type-utag
190+
or rv_token-type = zif_mustache=>c_token_type-section )
191+
and find( val = rv_token-content sub = '/' ) >= 1. " / at the beginning mean namespace
192+
split rv_token-content at '/' into table rv_token-path.
193+
endif.
194+
188195
endmethod. " parse_tag.
189196

190197

zcl_mustache_parser.clas.testclasses.abap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ class ltcl_mustache_parser implementation.
159159

160160
cl_abap_unit_assert=>assert_equals( exp = lt_exp act = lt_act ).
161161

162+
" Path
163+
append initial line to lt_exp assigning <token>.
164+
<token>-type = zif_mustache=>c_token_type-etag.
165+
<token>-content = 'A/B'.
166+
append 'A' to <token>-path.
167+
append 'B' to <token>-path.
168+
cl_abap_unit_assert=>assert_equals( exp = <token> act = zcl_mustache_parser=>parse_tag( 'A/B' ) ).
169+
162170
endmethod. "parse_tag
163171

164172
method parse_tag_negative.

zcl_mustache_render.clas.abap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ CLASS ZCL_MUSTACHE_RENDER IMPLEMENTATION.
132132
describe field <field> type lv_type.
133133

134134
if lv_type ca c_data_type-elem. " Element data type
135-
rv_val = <field>.
135+
rv_val = |{ <field> }|.
136136
elseif lv_type ca c_data_type-oref. " Object or interface instance
137137
rv_val = render_oref( iv_tag_name = iv_name io_obj = <field> ).
138138
else.
@@ -156,6 +156,7 @@ CLASS ZCL_MUSTACHE_RENDER IMPLEMENTATION.
156156

157157
field-symbols: <field> type any,
158158
<struc> type any,
159+
<fname> type string,
159160
<rec> type zif_mustache=>ty_struc,
160161
<table> type any table.
161162

@@ -395,8 +396,7 @@ CLASS ZCL_MUSTACHE_RENDER IMPLEMENTATION.
395396
describe field i_data type lv_type.
396397

397398
if lv_type ca c_data_type-table
398-
and cl_abap_typedescr=>describe_by_data( i_data )->absolute_name
399-
= c_ty_struc_tt_absolute_name.
399+
and cl_abap_typedescr=>describe_by_data( i_data )->absolute_name = c_ty_struc_tt_absolute_name.
400400
lv_unitab = abap_true.
401401
endif.
402402

zcl_mustache_render.clas.testclasses.abap

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
define _add_mu_token.
2+
append initial line to &1 assigning <token>.
3+
<token>-type = &2.
4+
<token>-cond = &3.
5+
<token>-level = &4.
6+
<token>-content = &5.
7+
end-of-definition.
8+
19
class ltcl_mustache_render definition final
210
for testing
311
risk level
@@ -7,6 +15,7 @@ class ltcl_mustache_render definition final
715

816
methods find_value for testing.
917
methods render_section for testing.
18+
methods render_path for testing.
1019

1120
endclass.
1221

@@ -66,6 +75,21 @@ class ltcl_mustache_render implementation.
6675
cl_abap_unit_assert=>fail( lx->msg ).
6776
endtry.
6877

78+
"4----------------
79+
data ls_struc type zcl_mustache_test=>ty_dummy.
80+
ls_struc-attr-age = 10.
81+
clear lt_data_stack.
82+
get reference of ls_struc into lr.
83+
append lr to lt_data_stack.
84+
85+
try .
86+
lv_act = zcl_mustache_render=>find_value( it_data_stack = lt_data_stack iv_name = 'attr-age' ).
87+
cl_abap_unit_assert=>assert_equals( exp = '10' act = lv_act ).
88+
catch zcx_mustache_error into lx.
89+
cl_abap_unit_assert=>fail( lx->msg ).
90+
endtry.
91+
92+
6993
endmethod. " find_value.
7094

7195
method render_section.
@@ -129,4 +153,68 @@ class ltcl_mustache_render implementation.
129153

130154
endmethod. "render_section
131155

156+
method render_path.
157+
158+
data ls_test type zcl_mustache_test=>ty_test_case.
159+
data ls_statics type zcl_mustache_render=>ty_context.
160+
data ls_data type zcl_mustache_test=>ty_dummy.
161+
data lt_act type string_table.
162+
data lv_act type string.
163+
data lx type ref to zcx_mustache_error.
164+
field-symbols <token> like line of ls_test-tokens.
165+
166+
ls_data-name = 'Vasya'.
167+
ls_data-attr-male = abap_true.
168+
ls_data-attr-age = 30.
169+
170+
ls_test-template = '{{name}}: age {{attr-age}}'.
171+
_add_mu_token ls_statics-tokens zif_mustache=>c_token_type-etag '' 1 'name'.
172+
_add_mu_token ls_statics-tokens zif_mustache=>c_token_type-static '' 1 `: age `.
173+
_add_mu_token ls_statics-tokens zif_mustache=>c_token_type-etag '' 1 'attr-age'.
174+
ls_test-output = 'Vasya: age 30'.
175+
176+
try .
177+
zcl_mustache_render=>render_section(
178+
exporting
179+
is_statics = ls_statics
180+
i_data = ls_data
181+
changing
182+
ct_lines = lt_act ).
183+
lv_act = zcl_mustache_utils=>join_strings( it_tab = lt_act iv_sep = '' ).
184+
185+
cl_abap_unit_assert=>assert_equals(
186+
exp = ls_test-output
187+
act = lv_act ).
188+
catch zcx_mustache_error into lx.
189+
cl_abap_unit_assert=>fail( lx->msg ).
190+
endtry.
191+
192+
clear: ls_statics-tokens, lt_act.
193+
ls_test-template = '{{name}}: {{#attr-male}}Male{{/attr-male}}{{#attr-female}}Female{{/attr-female}}'.
194+
_add_mu_token ls_statics-tokens zif_mustache=>c_token_type-etag '' 1 'name'.
195+
_add_mu_token ls_statics-tokens zif_mustache=>c_token_type-static '' 1 `: `.
196+
_add_mu_token ls_statics-tokens zif_mustache=>c_token_type-section '=' 1 'attr-male'.
197+
_add_mu_token ls_statics-tokens zif_mustache=>c_token_type-static '' 2 `Male`.
198+
_add_mu_token ls_statics-tokens zif_mustache=>c_token_type-section '=' 1 'attr-female'.
199+
_add_mu_token ls_statics-tokens zif_mustache=>c_token_type-static '' 2 `Female`.
200+
ls_test-output = 'Vasya: Male'.
201+
202+
try .
203+
zcl_mustache_render=>render_section(
204+
exporting
205+
is_statics = ls_statics
206+
i_data = ls_data
207+
changing
208+
ct_lines = lt_act ).
209+
lv_act = zcl_mustache_utils=>join_strings( it_tab = lt_act iv_sep = '' ).
210+
211+
cl_abap_unit_assert=>assert_equals(
212+
exp = ls_test-output
213+
act = lv_act ).
214+
catch zcx_mustache_error into lx.
215+
cl_abap_unit_assert=>fail( lx->msg ).
216+
endtry.
217+
218+
endmethod.
219+
132220
endclass.

zcl_mustache_test.clas.abap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class zcl_mustache_test definition
1616
html type string,
1717
tab type string_table,
1818
obj type ref to zcl_mustache_utils,
19+
begin of attr,
20+
age type i,
21+
male type abap_bool,
22+
female type abap_bool,
23+
end of attr,
1924
end of ty_dummy,
2025

2126
begin of ty_size,

zif_mustache.intf.abap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface zif_mustache
1414
cond type char2,
1515
level type i,
1616
content type string,
17+
path type string_table,
1718
end of ty_token,
1819
ty_token_tt type standard table of ty_token with key type cond level.
1920

0 commit comments

Comments
 (0)