Giter Site home page Giter Site logo

abap-string-map's Introduction

abaplint abap package version

Abap string map

String map primitive implementation for abap

  • install it using abapGit
  • errors, if happen, are raised via cx_no_check (with get_text)
  • implements get, set, has, size, is_empty, delete, clear
  • also convenience features to create map from string, table or structure (key/value)
data lo_map type ref to zcl_abap_string_map.
lo_map = zcl_abap_string_map=>create( ). " or create object ...

" Key and value can be a string or of C or N types (so `clike`)
lo_map->set(
  iv_key = 'hello'
  iv_val = 'world' ).
some_var = lo_map->get( 'hello' ). " => 'world'

lo_map->has( 'hello' ). " => abap_true
lo_map->is_empty( ).    " => abap_false
lo_map->size( ).        " => 1
lo_map->delete( 'hello' ).
lo_map->clear( ).
  • set allows chaining
lo_map->set(
  iv_key = 'A'
  iv_val = '1' 
)->set(
  iv_key = 'B'
  iv_val = '2' ).
  • implements keys, values
data lt_all_keys type string_table.
data lt_all_vals type string_table.

lt_all_keys = lo_map->keys( ).   " => ( 'hello' )
lt_all_vals = lo_map->values( ). " => ( 'world' )
  • implements to_struc, from_struc
data:
  begin of ls_struc,
    a type string,
    b type abap_bool,
    c type i,
  end of ls_struc.

lo_map->from_struc( ls_struc ). " Converts abap structure to string map
lo_map->to_struc( changing cs_container = ls_struc ). " Converts map to abap structure

" If you have more data entries in the map than fields in the target structure
" use strict( false ) - this skips entries which do not have a matching field
lo_map->strict( abap_false )->to_struc( changing cs_container = ls_struc ).
  • implements from_entries - this copies entries from a provided param. Importantly, the method accepts any table but the shape of the record must conform to zcl_abap_string_map=>ty_entry, namely it must have 2 string attributes for key and value respectively (see the code of from_entries for clarification)
types:
  begin of ty_my_key_value,
    key type string,
    value type string,
  end of ty_my_key_value.

data lt_entries type table of ty_my_key_value.
lt_entries = value #(
  ( key = 'hello' value 'world' )
  ( key = 'and' value 'another' )
).
lo_map->from_entries( lt_entries ).
  • implements from_string - this parses string of pairs like a = b, x = y into map. Spaces are condensed. to_string renders in string representation (careful with case insensitive - keys will be upper-cased).
lo_map->from_string( 'a = b, x = y' ).
lo_map->get( 'a' ). " => 'b'
lo_map->to_string( ). " => 'a=b,x=y'
  • implements from_map - copies another map object (respecting destination case sensitivity)
lo_map->from_map( lo_another_map ).
  • from_* methods allow chaining. They all add values to the map, existing values with the same name are overwritten.
lo_map->from_map( lo_another_map )->from_struc( ls_struc )->set( iv_key = 'x' iv_val = '1' ).
  • create also supports immediate initiation from another instance of string map, a structure (same as running from_struc after) or a table (same as running from_entries after)
lo_copy = zcl_abap_string_map=>create( lo_map ).
lo_copy = zcl_abap_string_map=>create( ls_struc ).   " see examples above
lo_copy = zcl_abap_string_map=>create( lt_entries ). " see examples above
lo_copy = zcl_abap_string_map=>create( 'a=b, x=y' ). " see examples above
  • you may set the map immutable (read only). Guards set, delete, clear, from_* methods.
lo_map->set(
  iv_key = 'A'
  iv_val = '1' )->freeze( ).

lo_map->set(
  iv_key = 'A'
  iv_val = '2' ). " raises cx_no_check

For more examples see unit tests code

Blog posts

abap-string-map's People

Contributors

mbtools avatar sbcgua avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.