Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add snake_case to params normalization #3884

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,15 @@ class ScriptBinding extends WorkflowBinding {

@Override
Object get(Object key) {
if( !target.containsKey(key) ) {
String normalizedKey = normalizeToCamelCase((String) key)
if( !target.containsKey(normalizedKey) ) {
final msg = "Access to undefined parameter `$key` -- Initialise it to a default value eg. `params.$key = some_value`"
if( NF.isStrictMode() )
throw new AbortOperationException(msg)
log.warn1(msg, firstOnly: true)
return null
}
return target.get(key)
return target.get(normalizedKey)
}

/**
Expand Down Expand Up @@ -283,17 +284,15 @@ class ScriptBinding extends WorkflowBinding {
// keep track of the real name
realNames << name

// normalize the name
def name2 = name.contains('-') ? hyphenToCamelCase(name) : camelCaseToHyphen(name)
// internally normalize the name to camelCase
name = normalizeToCamelCase(name)

final readOnly = name in readOnlyNames || name2 in readOnlyNames
final readOnly = name in readOnlyNames

def result = null
if( !readOnly ) {
readOnlyNames << name
readOnlyNames << name2
result = target.put(name, value)
target.put(name2, value)
}

return result
Expand Down Expand Up @@ -360,54 +359,18 @@ class ScriptBinding extends WorkflowBinding {
* @return
*/
@PackageScope
static String hyphenToCamelCase( String str ) {
static String normalizeToCamelCase( String str ) {

if( !str ) { return str }

def result = new StringBuilder()
str.split('-').eachWithIndex{ String entry, int i ->
str.split('[-_]').eachWithIndex{ String entry, int i ->
result << (i>0 ? StringUtils.capitalize(entry) : entry )
}

return result.toString()
}

/**
* Converts a camel-case string to a string where words are separated by hyphen character
*
* @param str The string to be converted
* @return A string where camel-case words are converted to words separated by hyphen character
*/
@PackageScope
static String camelCaseToHyphen( String str ) {

def lower = 'a'..'z'
def upper = 'A'..'Z'

def result = new StringBuilder()
if( !str ) {
return str
}

result << str[0]
for( int i=1; i<str.size(); i++ ) {
if( str[i] in upper && str[i-1] in lower ) {
result << '-'
if( i+1<str.size() && str[i+1] in lower ) {
result << str[i].toLowerCase()
}
else {
result << str[i]
}
}
else {
result << str[i]
}
}

return result.toString()
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,30 @@ class ScriptBindingTest extends Specification {

}

def 'should convert hyphen separated string to camel case' () {
def 'should normalize strings to camel case' () {

expect:
ScriptBinding.ParamsMap.hyphenToCamelCase('a') == 'a'
ScriptBinding.ParamsMap.hyphenToCamelCase('A') == 'A'
ScriptBinding.ParamsMap.hyphenToCamelCase('a-b-c-') == 'aBC'
ScriptBinding.ParamsMap.hyphenToCamelCase('aa-bb-cc') == 'aaBbCc'
ScriptBinding.ParamsMap.hyphenToCamelCase('alpha-beta-delta') == 'alphaBetaDelta'
ScriptBinding.ParamsMap.hyphenToCamelCase('Alpha-Beta-delta') == 'AlphaBetaDelta'

}

def 'should convert camel case string to hyphen separated' () {

expect:
ScriptBinding.ParamsMap.camelCaseToHyphen('alphaBetaDelta') == 'alpha-beta-delta'
ScriptBinding.ParamsMap.camelCaseToHyphen('AlphaBetaDelta') == 'Alpha-beta-delta'
ScriptBinding.ParamsMap.camelCaseToHyphen('Field1') == 'Field1'
ScriptBinding.ParamsMap.camelCaseToHyphen('FieldUno') == 'Field-uno'
ScriptBinding.ParamsMap.camelCaseToHyphen('FieldUNO') == 'Field-UNO'
ScriptBinding.ParamsMap.camelCaseToHyphen('FieldA') == 'Field-A'
ScriptBinding.ParamsMap.camelCaseToHyphen('FieldAB') == 'Field-AB'
ScriptBinding.ParamsMap.camelCaseToHyphen('FieldAb') == 'Field-ab'
ScriptBinding.ParamsMap.normalizeToCamelCase('a') == 'a'
ScriptBinding.ParamsMap.normalizeToCamelCase('A') == 'A'
ScriptBinding.ParamsMap.normalizeToCamelCase('a-b-c-') == 'aBC'
ScriptBinding.ParamsMap.normalizeToCamelCase('a_b_c-') == 'aBC'
ScriptBinding.ParamsMap.normalizeToCamelCase('aa-bb-cc') == 'aaBbCc'
ScriptBinding.ParamsMap.normalizeToCamelCase('aa_bb_cc') == 'aaBbCc'
ScriptBinding.ParamsMap.normalizeToCamelCase('alpha_beta_delta') == 'alphaBetaDelta'
ScriptBinding.ParamsMap.normalizeToCamelCase('alpha_beta-delta') == 'alphaBetaDelta'
ScriptBinding.ParamsMap.normalizeToCamelCase('alpha-beta_delta') == 'alphaBetaDelta'
ScriptBinding.ParamsMap.normalizeToCamelCase('alpha-beta-delta') == 'alphaBetaDelta'
ScriptBinding.ParamsMap.normalizeToCamelCase('Alpha_beta_delta') == 'AlphaBetaDelta'
ScriptBinding.ParamsMap.normalizeToCamelCase('Alpha_beta-delta') == 'AlphaBetaDelta'
ScriptBinding.ParamsMap.normalizeToCamelCase('Alpha-beta_delta') == 'AlphaBetaDelta'
ScriptBinding.ParamsMap.normalizeToCamelCase('Alpha-Beta-delta') == 'AlphaBetaDelta'
ScriptBinding.ParamsMap.normalizeToCamelCase('Field1') == 'Field1'
ScriptBinding.ParamsMap.normalizeToCamelCase('Field_uno') == 'FieldUno'
ScriptBinding.ParamsMap.normalizeToCamelCase('Field_UNO') == 'FieldUNO'
ScriptBinding.ParamsMap.normalizeToCamelCase('Field_A') == 'FieldA'
ScriptBinding.ParamsMap.normalizeToCamelCase('field-ab') == 'fieldAb'
ScriptBinding.ParamsMap.normalizeToCamelCase('Field_AB') == 'FieldAB'
ScriptBinding.ParamsMap.normalizeToCamelCase('Field_ab') == 'FieldAb'

}

Expand Down Expand Up @@ -133,7 +134,7 @@ class ScriptBindingTest extends Specification {

then:
map['field1'] == 1
map['field-1'] == null
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test case changes because camel case is a bit ambiguous with numbers- field-1 normalizes to field1 but field1 wasn't converting via camelCaseToHyphen into field-1.

map['field-1'] == 1
map['field2'] == 2
map['Field2'] == 3

Expand Down Expand Up @@ -184,7 +185,7 @@ class ScriptBindingTest extends Specification {
when:
def params = new ScriptBinding.ParamsMap('foo-bar':1)
then:
params.size() == 2
params.size() == 1
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ParamsMap.size now more accurately reflects the number of params it contains.

params.fooBar == 1
params.'foo-bar' == 1
params.all() == '--foo-bar 1'
Expand Down