All files / src/repo/utils zcl_abapgit_repo_labels.clas.abap

95.96% Statements 309/322
71.87% Branches 23/32
100% Functions 0/0
95.96% Lines 309/322

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 3231x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 7x 7x 7x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 28x 28x 28x 28x 28x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 4x 4x 1x 1x 1x 1x 1x 1x 1x 2x   20x 20x 3x   17x 17x 17x     17x 17x 17x 10x 10x 2x   10x 15x 7x 6x 14x 2x 2x 14x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x   1x 1x 1x 1x  
CLASS zcl_abapgit_repo_labels DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .
 
  PUBLIC SECTION.
 
    TYPES:
      BEGIN OF ty_label_color,
        label TYPE string,
        color TYPE string,
      END OF ty_label_color,
      ty_label_colors TYPE STANDARD TABLE OF ty_label_color WITH KEY label.
 
    TYPES:
      BEGIN OF ty_color,
        cls    TYPE string,
        fg     TYPE string,
        bg     TYPE string,
        border TYPE string,
      END OF ty_color.
 
    CONSTANTS c_allowed_chars TYPE string VALUE `-_. a-zA-Z0-9` ##NO_TEXT.
 
    " it is easier to allow chars, though potentially other chars can be added later if needed
    CLASS-METHODS class_constructor.
    CLASS-METHODS validate
      IMPORTING
        !iv_labels TYPE string
      RAISING
        zcx_abapgit_exception.
    CLASS-METHODS split
      IMPORTING
        !iv_labels       TYPE string
      RETURNING
        VALUE(rt_labels) TYPE string_table.
    CLASS-METHODS normalize
      IMPORTING
        !iv_labels       TYPE string
      RETURNING
        VALUE(rv_labels) TYPE string.
 
    CLASS-METHODS validate_colors
      IMPORTING
        !iv_config TYPE string
      RAISING
        zcx_abapgit_exception.
    CLASS-METHODS split_colors
      IMPORTING
        !iv_config             TYPE string
      RETURNING
        VALUE(rt_label_colors) TYPE ty_label_colors.
    CLASS-METHODS split_colors_into_map
      IMPORTING
        !iv_config    TYPE string
      RETURNING
        VALUE(ro_map) TYPE REF TO zcl_abapgit_string_map.
    CLASS-METHODS normalize_colors
      IMPORTING
        !iv_config       TYPE string
      RETURNING
        VALUE(rv_config) TYPE string.
 
    CLASS-METHODS parse_color
      IMPORTING
        iv_color         TYPE string
      RETURNING
        VALUE(rs_parsed) TYPE ty_color.
 
  PROTECTED SECTION.
  PRIVATE SECTION.
    CLASS-DATA gv_regex TYPE string.
 
    CLASS-METHODS validate_one_label_color
      IMPORTING
        !is_lc    TYPE ty_label_color
        !iv_index TYPE i DEFAULT 0
      RAISING
        zcx_abapgit_exception.
 
    CLASS-METHODS validate_rgb_color
      IMPORTING
        !iv_color TYPE string
        !iv_index TYPE i DEFAULT 0
      RAISING
        zcx_abapgit_exception.
 
ENDCLASS.
 
 
 
CLASS zcl_abapgit_repo_labels IMPLEMENTATION.
 
 
  METHOD class_constructor.
    gv_regex = |^[{ c_allowed_chars }]*$|. " Must start with -
  ENDMETHOD.
 
 
  METHOD normalize.
 
    DATA lt_labels TYPE string_table.
    DATA lt_normalized TYPE string_table.
    FIELD-SYMBOLS <lv_lab> LIKE LINE OF lt_labels.
 
    lt_labels = split( iv_labels ).
 
    LOOP AT lt_labels ASSIGNING <lv_lab>.
      FIND REGEX gv_regex IN <lv_lab>.
      IF sy-subrc = 0.
        APPEND <lv_lab> TO lt_normalized.
      ENDIF.
    ENDLOOP.
 
    SORT lt_normalized.
    DELETE ADJACENT DUPLICATES FROM lt_normalized.
 
    rv_labels = concat_lines_of(
      table = lt_normalized
      sep = `, ` ).
 
  ENDMETHOD.
 
 
  METHOD normalize_colors.
 
    DATA lt_colors TYPE ty_label_colors.
    DATA lt_normalized TYPE ty_label_colors.
    DATA lt_pairs TYPE string_table.
    DATA lv_pair TYPE string.
    FIELD-SYMBOLS <ls_c> LIKE LINE OF lt_colors.
 
    lt_colors = split_colors( iv_config ).
 
    LOOP AT lt_colors ASSIGNING <ls_c>.
      TRY.
          validate_one_label_color( <ls_c> ).
          APPEND <ls_c> TO lt_normalized.
        CATCH zcx_abapgit_exception.
      ENDTRY.
    ENDLOOP.
 
    SORT lt_normalized BY label.
    DELETE ADJACENT DUPLICATES FROM lt_normalized COMPARING label.
 
    LOOP AT lt_normalized ASSIGNING <ls_c>.
      lv_pair = <ls_c>-label && `:` && <ls_c>-color.
      APPEND lv_pair TO lt_pairs.
    ENDLOOP.
 
    rv_config = concat_lines_of(
      table = lt_pairs
      sep = `, ` ).
 
  ENDMETHOD.
 
 
  METHOD parse_color.
 
    DATA lv_tmp TYPE string.
 
    IF iv_color IS INITIAL.
      RETURN.
    ENDIF.
 
    IF iv_color+0(1) = '#'.
      lv_tmp  = iv_color+1.
      SPLIT lv_tmp AT '/' INTO rs_parsed-fg rs_parsed-bg rs_parsed-border.
    ELSE.
      rs_parsed-cls = iv_color.
    ENDIF.
 
  ENDMETHOD.
 
 
  METHOD split.
 
    FIELD-SYMBOLS <lv_lab> LIKE LINE OF rt_labels.
 
    SPLIT iv_labels AT ',' INTO TABLE rt_labels.
    LOOP AT rt_labels ASSIGNING <lv_lab>.
      CONDENSE <lv_lab>.
    ENDLOOP.
    DELETE rt_labels WHERE table_line IS INITIAL.
 
  ENDMETHOD.
 
 
  METHOD split_colors.
 
    DATA lt_pairs TYPE string_table.
    DATA lv_clean_config LIKE iv_config.
    DATA ls_c LIKE LINE OF rt_label_colors.
    FIELD-SYMBOLS <lv_pair> LIKE LINE OF lt_pairs.
 
    lv_clean_config = replace(
      val = iv_config
      sub = cl_abap_char_utilities=>newline
      with = ` ` ). " text area ends with LF
 
    SPLIT lv_clean_config AT ',' INTO TABLE lt_pairs.
    LOOP AT lt_pairs ASSIGNING <lv_pair>.
      CONDENSE <lv_pair>.
      IF <lv_pair> IS NOT INITIAL.
        SPLIT <lv_pair> AT ':' INTO ls_c-label ls_c-color.
        CONDENSE ls_c-label.
        CONDENSE ls_c-color.
        APPEND ls_c TO rt_label_colors.
      ENDIF.
    ENDLOOP.
 
  ENDMETHOD.
 
 
  METHOD split_colors_into_map.
 
    DATA lt_colors TYPE ty_label_colors.
    FIELD-SYMBOLS <ls_c> LIKE LINE OF lt_colors.
 
    lt_colors = split_colors( iv_config ).
 
    ro_map = zcl_abapgit_string_map=>create( ).
    LOOP AT lt_colors ASSIGNING <ls_c>.
      TRY.
          ro_map->set(
            iv_key = <ls_c>-label
            iv_val = <ls_c>-color ).
        CATCH zcx_abapgit_exception.
      ENDTRY.
    ENDLOOP.
 
  ENDMETHOD.
 
 
  METHOD validate.
 
    DATA lt_labels TYPE string_table.
    FIELD-SYMBOLS <lv_lab> LIKE LINE OF lt_labels.
 
    lt_labels = split( iv_labels ).
 
    LOOP AT lt_labels ASSIGNING <lv_lab>.
      FIND REGEX gv_regex IN <lv_lab>.
      IF sy-subrc <> 0.
        zcx_abapgit_exception=>raise( |Disallowed chars in label #{ sy-tabix }| ).
      ENDIF.
      " TODO: maybe also limit length ?
    ENDLOOP.
 
  ENDMETHOD.
 
 
  METHOD validate_colors.
 
    DATA lt_colors TYPE ty_label_colors.
    FIELD-SYMBOLS <ls_c> LIKE LINE OF lt_colors.
 
    lt_colors = split_colors( iv_config ).
 
    LOOP AT lt_colors ASSIGNING <ls_c>.
      validate_one_label_color(
        is_lc    = <ls_c>
        iv_index = sy-tabix ).
    ENDLOOP.
 
  ENDMETHOD.
 
 
  METHOD validate_one_label_color.
 
    DATA ls_parsed_color TYPE ty_color.
 
    IF is_lc-label IS INITIAL.
      zcx_abapgit_exception=>raise( |Label is empty in pair #{ iv_index }| ).
    ENDIF.
 
    IF is_lc-color IS INITIAL.
      zcx_abapgit_exception=>raise( |Color is empty in pair #{ iv_index }| ).
    ENDIF.
 
    FIND REGEX gv_regex IN is_lc-label.
    IF sy-subrc <> 0.
      zcx_abapgit_exception=>raise( |Disallowed chars in label in pair #{ iv_index }| ).
    ENDIF.
 
    ls_parsed_color = parse_color( is_lc-color ).
    IF ls_parsed_color-cls IS NOT INITIAL.
      FIND REGEX '^[-_A-Za-z]+$' IN ls_parsed_color-cls.
      IF sy-subrc <> 0.
        zcx_abapgit_exception=>raise( |Disallowed chars in color in pair #{ iv_index }| ).
      ENDIF.
    ENDIF.
    IF ls_parsed_color-fg IS NOT INITIAL.
      validate_rgb_color( ls_parsed_color-fg ).
    ENDIF.
    IF ls_parsed_color-bg IS NOT INITIAL.
      validate_rgb_color( ls_parsed_color-bg ).
    ENDIF.
    IF ls_parsed_color-border IS NOT INITIAL.
      validate_rgb_color( ls_parsed_color-border ).
    ENDIF.
 
  ENDMETHOD.
 
 
  METHOD validate_rgb_color.
 
    DATA lv_len TYPE i.
 
    IF iv_color IS NOT INITIAL.
      FIND REGEX '^[0-9A-Fa-f]+$' IN iv_color.
      IF sy-subrc <> 0.
        zcx_abapgit_exception=>raise( |Disallowed chars in color in pair #{ iv_index }| ).
      ENDIF.
      lv_len = strlen( iv_color ).
      IF NOT ( lv_len = 3 OR lv_len = 6 ).
        zcx_abapgit_exception=>raise( |Incorrect color in pair #{ iv_index }| ).
      ENDIF.
    ENDIF.
 
  ENDMETHOD.
ENDCLASS.