hi! Looking for Groovy help :slightly_smiling_face...
# nextflow-plugins
j
hi! Looking for Groovy help 🙂 In #C056RQB10LU we have a function with one optional argument, which is imported to pipelines and used as
validateParameters()
or
validateParameters('custom_nextflow_schema.json')
Copy code
@Function
    void validateParameters(
        String schemaFilename = 'nextflow_schema.json'
    ) { ... }
Now we need to add optional named arguments, so we used a Map for that
Copy code
@Function
    void validateParameters(
        Map options = null,
        String schemaFilename
    ) { ... }
But when using this function without any arguments (
validateParameters()
) we see this error
Copy code
ERROR ~ Could not find which method validateParameters() to invoke from this list:
  public void nextflow.validation.SchemaValidator#validateParameters(java.lang.String)
  public void nextflow.validation.SchemaValidator#validateParameters(java.util.Map, java.lang.String)
Is there a better way to code that?
n
Maybe add the
schemaFilename
as an option in the
options
map? since that parameter isn't mandatory for the execution of the function?
j
I haven’t considered that because this will break the current implementation for pipelines that are passing a schema path like
validateParameters('custom_nextflow_schema.json')
but you are right, maybe that’s our best option
n
Yes, indeed 😕
Let's maybe fix all functions to work with an options map for optional arguments and only allow required arguments as additional inputs while we're breaking stuff 😁
I'll be happy to help if I find some time (probably tomorrow)
s
yea usually for stuff like this, I use the "config object" method, which is basically like your map implementation, but instead of using a generic map, create an actual Class definition for the config for the function, then add your optional args as attributes to it, then pass it in to the function. Its cleaner because a Class instance will always have the exact attributes expected, but a map could have any or none of the expected attributes and is subject to type-safety risks too
you can either have the Config object for the method be a function input arg, or if you are adventurous you could have it as a global object in the function's scope. The latter method is similar to how libraries like Pythons
csv
work and are configured
example; config object; https://github.com/stevekm/dupefinder/blob/master/src/find.go#L10-L15 config object as required input to method; https://github.com/stevekm/dupefinder/blob/master/src/find.go#L126 same technique can apply well to a lot of languages
safer than using a plain old map
n
Wow, thank you for this! That seems like a really good idea for this!
j
Thanks for the info @steve! will definitely try this out 😄