简体   繁体   中英

Want to parse multiple key=value string to Map and then serialize it but map to string serialization fails

I am converting the key=value string Groovy Map. Then after some changes, returning back the serialize object. Example String --> dlpxDcTags = "OWNER=test,PROJECT=test2,COSTCENTER=1234,TEAM=devops"

parseDcenterTags(dlpxDcTags){
  Map tags = [:]
  tags += dlpxDcTags.replaceAll('\\[|\\]', '').split(',').collectEntries { entry ->
    def pair = entry.split('=')
    [(pair.first().trim()): pair.last().trim()]
    return tags
  }
}

def createDcenterTags(dlpxDcTags=null) {
  // Values passed from the environment of the user takes precedence
  tags = parseDcenterTags(dlpxDcTags)
  if (tags) {
    if (!(tags.get('PROJECT'))) {
        tags['PROJECT'] = env.JOB_NAME
      }

    if (!tags.get('OWNER')) {
      // not supplied via dlpxDcTags
      tags['OWNER'] = env.BUILD_USER_EMAIL
      }
    }
  else {
    tags['PROJECT'] = env.JOB_NAME
    tags['OWNER'] = env.BUILD_USER_EMAIL
  }
  return serializeDcenterTags(tags)
 }

def serializeDcenterTags(tags){
  dlpxDcTags = {
    tags.collect { /$tags.key="$tags.value"/ } join ","
  }
  return dlpxDcTags
}

Facing issue:

  • The variable when called from main jenkins job using this helper script is getting value “org.jenkinsci.plugins.workflow.cps.CpsClosure2@64ed2e4b”

After lot of reading and trying different ways, the very simple thing which worked for me was doing the serialization in the same method instead of defining and calling it from another method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM