Skip to content

Avoid using arrays and work with List<String> #768

@bayrakta

Description

@bayrakta

As I could not find any mechanism to extend a list, I am trying to build a list of strings and work with the result in several spots to loop on it. The main idea is building a comma separated list as string and then split it by comma. However, the split method returns an array of String and I am not able to iterate on it. (The only method which works with an array is the join method, which can actually only work on a result of the split method and I don't see any meaningful use case for it.)

I would propose the following options to enhance the scripting capabilities :

  • Provide custom additional methods split and join, which work with List<String>, instead of using the ones from StringUtils.
  • Alternatively, new additional method(s) might be introduced to extend an existing list, such as extendList(List<String>, String) and appendList(List<String>, List<String>).

Sample block when the method extendList introduced :

- DEF MY_SITE_LIST=[]
- FOR siteNode IN CHILDREN OF /content:
    - DEF siteName=${siteNode.name}
    - DEF MY_SITE_LIST=${extendList(MY_SITE_LIST, siteName)}

- FOR siteName IN ${MY_SITE_LIST}:
    - DEF siteGroupName="sample-group-${siteName}"
    - ${siteGroupName}:
        - path: "/home/groups/myproject/${siteGroupName}"

P.S.: Once the improvements prepared, the last block needs to be tested. As far as I remember, the changes on MY_LIST is only in effect within the outermost FOR loop. Outside of the loop, it remains intact. This causes code duplicates, as overriding a global variable within a loop is not possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions