在编写 基于 Func 生成 PO 的 Data Type 规则时找到一个关于数据类型的转换文章 Generalize the call to conversion exit functions以下是针对相关复现
FUNCTION zfm_generalize_conversion.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" REFERENCE(I_VARIABLE)
*" REFERENCE(I_CONVER_TYPE) TYPE C DEFAULT 'I'
*" EXPORTING
*" REFERENCE(O_VARIABLE)
*" EXCEPTIONS
*" CONVER_TYPE_FAIL
*"----------------------------------------------------------------------
" note:
" - 要求 i_variable 或 o_variable 参考数据元素或表字段创建( 可取到 转换规则 )
" - 优先使用 i_variable 的转换规则
" - I_CONVER_TYPE 指定转换方式 I 外值转内值 O 内值转外值
" - 例外 CONVER_FAIL 为转换异常
DATA: lo_data TYPE REF TO data.
DATA: lv_rule TYPE funcname.
" 获取字段属性
DESCRIBE FIELD i_variable OUTPUT-LENGTH DATA(l_len)
EDIT MASK DATA(l_mask).
IF l_mask IS INITIAL.
DESCRIBE FIELD o_variable OUTPUT-LENGTH l_len
EDIT MASK l_mask.
IF l_mask IS INITIAL.
o_variable = i_variable.
RETURN.
ENDIF.
ENDIF.
REPLACE FIRST OCCURRENCE OF '==' IN l_mask WITH ''.
CONDENSE l_mask NO-GAPS.
" 为 APLHA 类的转换设置传出长度
CREATE DATA lo_data TYPE c LENGTH l_len.
IF lo_data IS BOUND.
ASSIGN lo_data->* TO FIELD-SYMBOL(<fs_data>).
IF <fs_data> IS ASSIGNED.
" pass
ELSE.
" 异常处理
o_variable = i_variable.
ENDIF.
ELSE.
" 异常处理
o_variable = i_variable.
ENDIF.
" 转换规则
CASE i_conver_type.
WHEN 'I'.
lv_rule = |CONVERSION_EXIT_{ l_mask }_INPUT|.
WHEN 'O'.
lv_rule = |CONVERSION_EXIT_{ l_mask }_OUTPUT|.
WHEN OTHERS.
RAISE conver_type_fail.
ENDCASE.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = i_variable
IMPORTING
output = <fs_data>
EXCEPTIONS
OTHERS = 1.
IF sy-subrc = 0.
o_variable = <fs_data>.
ELSE.
o_variable = i_variable.
ENDIF.
ENDFUNCTION.