Search code examples
amazon-web-servicesgroovyjenkins-groovy

Convert Typed List in Groovy to String List


I'm trying to change a typed list into a string list. I'm using the AWS SDK to get a list of PackageVersionResponse objects and I want to create a String list of the version method:

def client = AWSCodeArtifactClientBuilder.standard()
  .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
  .withRegion(region)
  .build()
def lpm = ListPackageVersionsRequest().builder(domain)
      .repository(repo)
      .packageValue(package)
def pvs = client.ListPackageVersions(params)
// change pvs into String list of return value of version method

In node I'd do something like pvs.map(pv => pv.version()) and get a String array of versions, but I'm not sure what the equivalent is in Groovy / Java


Solution

  • Should just be

    def pvs = client.listPackageVersions(params).versions.collect { it.version }
    

    Or using the spread operator

    def pvs = client.listPackageVersions(params).versions*.version
    

    (from tracing the javadoc for the client method, the ListPackageVersionsResult object and the getVersion method inside the PackageVersionSummary class)