ABAP new syntax CL_ABAP_CORRESPONDING

Introduction

The standard tool classes provided by the system are used to implement dynamic assignment operations between structures and internal tables, which can realize simple assignment, default values, lookup and other functions, which basically cover the functions of move-corresponding and corresponding

Example

Simple assignment
Refer to the F1 keyword document, which can realize most of the functions of the old version of corresponding, including assignment of structure, inner table, and nested structure

data:
begin of struct1,
  a1 type string value 'a1',
  a2 type string value 'a2',
  a3 type string value 'a3',
  a4 type string value 'a4',
  a5 type string value 'a5',
end of struct1,
begin of struct2,
  b1 type string value 'b1',
  b2 type string value 'b2',
  b3 type string value 'b3',
  a4 type string value 'b4',
  a5 type string value 'b5',
end of struct2.

"首先创建字段匹配规则
data(mapper) =
      cl_abap_corresponding=>create(
      source            = struct1
      destination       = struct2
      mapping           = value cl_abap_corresponding=>mapping_table(
      ( level = 0 kind = cl_abap_corresponding=>mapping_component srcname = 'a1' dstname = 'b3' )
      ( level = 0 kind = cl_abap_corresponding=>mapping_component srcname = 'a3' dstname = 'b1' )
      ( level = 0 kind = cl_abap_corresponding=>mapping_except_all ) "同名的a4,a5被排除
      )
      ).

"然后执行匹配操作
mapper->execute(
exporting
  source      = struct1
changing
  destination = struct2 ).

cl_demo_output=>display( struct2 ).

Set default value

data:
  begin of struct1,
    a1 type string value 'a1',
    a2 type string value 'a2',
    a3 type string value '  ',
    a4 type string value 'a4',
  end of struct1,
  begin of struct2,
    b1 type string value 'b1',
    b2 type string value 'b2',
    b3 type string value 'b3',
    b4 type string value 'b4',
  end of struct2.

data(mapper) =
      cl_abap_corresponding=>create_with_value(
      source            = struct1
      destination       = struct2
      mapping           = value
      cl_abap_corresponding=>mapping_table_value(
          "正常匹配
          ( level = 0 kind = cl_abap_corresponding=>mapping_component  srcname = 'a1' dstname = 'b1' )
          "源结构无匹配字段,设置默认值xx
          ( level = 0 kind = cl_abap_corresponding=>mapping_value    dstname = 'b2' value = ref #( `xxx` ) )
          "源结构字段值为initial,故默认值设置起效,目标字段修改为yyy
          ( level = 0 kind = cl_abap_corresponding=>mapping_default_value srcname = 'a3' dstname = 'b3' value = ref #( `yyy` ) )
          "源结构字段有值,故默认值设置未起效,目标字段无修改,结果为a4
          ( level = 0 kind = cl_abap_corresponding=>mapping_default_value srcname = 'a4' dstname = 'b4' value = ref #( `zzz` ) )
        )
      ).

mapper->execute(
exporting
  source      = struct1
changing
  destination = struct2 ).

cl_demo_output=>display( struct2 ).

lookup表

types:
  begin of line1,
    value   type i,
    comment2 type string,
  end of line1,
BEGIN OF line2,
  VALUE   TYPE I,
  COMMENT2 TYPE string,
END OF line2,
  itab1 type standard table of line1 with empty key,
  itab2 type hashed table of line2 with unique key value.

data(itab1) = value itab1( for i = 1 until i >= 10 ( value = i ) ).
data(itab2) = value itab2(
      ( value = 2 comment2 = `...` )
      ( value = 3 comment2 = `...` )
      ( value = 5 comment2 = `...` )
      ( value = 8 comment2 = `...` ) ).

data(mapping_tab) = value cl_abap_corresponding=>mapping_table(
"主键查询
( level = 0 kind = cl_abap_corresponding=>using_key srcname = 'PRIMARY_KEY' )
"指定主键字段
( level = 0 kind = cl_abap_corresponding=>mapping_using_component srcname = 'VALUE' dstname = 'VALUE' )
"指定匹配规则
( level = 0 kind = cl_abap_corresponding=>mapping_component srcname = 'comment2' dstname = 'comment1' )
).

"将Itab2与itab1按value字段进行匹配,然后将itab2中的comment2赋值给itab1中的comment1
cl_abap_corresponding=>create_using(
destination       = itab1
using             = itab2
      mapping               = mapping_tab
)->execute_using( exporting using       = itab2
changing  destination = itab1 ).

cl_demo_output=>display( itab1 ).

Remarks

This talent is not very knowledgeable. If there are mistakes, please point out the trouble.
You can also add QQ 2212332116 to communicate.

Guess you like

Origin blog.csdn.net/u012232542/article/details/105501822