简体   繁体   English

想解析多个key=value字符串为Map然后序列化但是map到字符串序列化失败

[英]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"我正在转换键=值字符串 Groovy Map。然后在进行一些更改后,返回序列化 object。示例字符串 --> 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”使用此帮助程序脚本从主要 jenkins 作业调用时,变量正在获取值“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.在大量阅读和尝试不同的方法之后,对我有用的非常简单的事情是用相同的方法进行序列化,而不是从另一个方法定义和调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文件内容拆分为键值MAP - split file contents into key value MAP 将 map 字符串列表转换为 map 列表作为 Z5F202E7AB75F00AF194C61CC07AE6B0 中的 object 列表 - convert list of map String to List of map as object List in groovy Jenkinsfile - JsonSlurper 返回字符串而不是地图 - Jenkinsfile - JsonSlurper returning a string instead of a map 在 Jenkinsfile 中将 JSON 样式字符串转换为地图 Groovy - Convert a JSON style String to a map Groovy in Jenkinsfile 将groovy字符串转换为jenkins管道中的映射 - Convert groovy string to map in jenkins pipeline 在Jenkins管道中将字符串转换为Groovy中的映射 - Converting string to map in groovy within Jenkins pipeline Jenkins共享库:如何在groovy中创建一个函数,该函数接受字符串+映射并返回该映射值 - Jenkins shared library: How to create a function in groovy that takes a string + map and returns that mapped value 根据键名获取值时,地图返回null - Map returns null when getting value based on key name 从地图给定计算的 jenkins 键中获取常规值? - obtaining groovy value from map given computed key for jenkins? 如何配置 jenkins extendedChoice 参数以打印 map 中的值,但请参阅选择下拉列表中的键 - how to configure jenkins extendedChoice parameter to print value from map but see key in selection dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM