Jenkins declarative pipeline

Hello,
I am using map to set additional arguments when calling a function for example:

def build_test_lambda(Map config) {
    dir (config.src_path) {
        script {
            def ROLE_CODE = config.role_code
            def BRANCH = env.GIT_BRANCH.replaceAll("/", "-").replaceAll(" ", "_")

            def package_name = "${config.lambda_name}-${BRANCH}-${currentBuild.number}.zip"
            def version = currentBuild.number
            if (env.GIT_BRANCH == 'master') {
                def packageJson = readJSON file: "${WORKSPACE}/package.json"
                sh "echo Lambda: '${config.lambda_name}'.  Version: '${packageJson.version}'"
                version = "'${packageJson.version}'"
                package_name = "${config.lambda_name}-${BRANCH}-${packageJson.version}.zip"
            }
}

When I call the function I have to provide all arguments as follows:

build_test_lambda(lambda_name: "apihttptestlambda", dockerFileName: "TestLambda/Dockerfile", role_code: "TEST_LAMBDA", src_path: "app/src", app_code: TEST_APP_CODE)

Is there a way to define a default values for let say config.src_path and if not specified in the function call statement to use the default values, otherwise if it’s specified to use the argument provided in the call function?
The idea is to be able to call the function only with one argument for example and to not throw an error:

build_test_lambda(dockerFileName: "TestLambda/Dockerfile")

I am little bit new to programming and especially Groovy :slight_smile:
So I hope some would be able to help me, thanks!

Hello @ValeriK :wave:

I don’t know much about Groovy, but I wouldn’t go with a Map for your parameters.
I would use regular parameters with default values.

def build_test_lambda(src_path = 'Default src_path_value', role_code = 'Default role_code_value, lambda_name = 'Default lambda_name_value ) {
 [...]
}

Hello @poddingue,
This is exactly how we are using it, right now.
However, the benefit of using map is that it’s not mandatory to pass arguments to the function in specific order.
Also you can give meaning full names for your arguments and to know what they are referring.
Using ordered arguments means that if you have 10-15 arguments and you want to modify the 13th you need to assign values to all 12 arguments before, even-though they will be assigned with default values for example.
So the code looks very complicated and hard to read and troubleshoot.

If there is a way to assign default values to the map passed as an argument to a specific function. This will help to modify only the desired arguments and they will be assigned with meaningful name which will increase readability and maintainability for the all shared functions across pipes.

Anyway, Thank you very much for looking into it :slight_smile: