Skip to content

Instantly share code, notes, and snippets.

@turboBasic
Last active May 25, 2022 12:18
Show Gist options
  • Save turboBasic/1c5d8331cea686a408f75496daf7ebdd to your computer and use it in GitHub Desktop.
Save turboBasic/1c5d8331cea686a408f75496daf7ebdd to your computer and use it in GitHub Desktop.
Use List as vararg item in Jenkins pipeline (spread operator is disabled) #groovy #jenkins
def foo(Object... args) {
def normalizedArgs = []
if (args != null) {
if (args.size() == 1) {
normalizedArgs << args[0]
} else {
normalizedArgs += args.toList()
}
}
print('foo(varargs): ' + normalizedArgs.size() + ': ')
println normalizedArgs.join(', ')
}
def foo(List args) {
print 'foo(List): '
foo(args as Object[])
}
foo 1 // foo(varargs): 1: 1
foo([[1]]) // foo(List): foo(varargs): 1: [1]
foo 1, 2, 3 // foo(varargs): 3: 1, 2, 3
foo() // foo(varargs): 0:
foo null // foo(List): foo(varargs): 0:
foo null, null // foo(varargs): 2: null, null
foo([null]) // foo(List): foo(varargs): 1: null
foo([1, 2, 3]) // foo(List): foo(varargs): 3: 1, 2, 3
foo([1, 2, 3], 2) // foo(varargs): 2: [1, 2, 3], 2
foo(*[1, 2, 3]) // foo(varargs): 3: 1, 2, 3 (not allowed in Jenkins pipeline)
foo(*[1, 2, 3], 4) // foo(varargs): 4: 1, 2, 3, 4 (not allowed in Jenkins pipeline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment