list matching in groovy switch statement -
is there way achieve in groovy?
def list1 = ['trs', 'file.xlsx', 'xxx'] def list2 = ['rls', 'file.xml', 'yyy'] def switchcheck(list) { switch (list) { case ['trs', /* matches pattern *.xlsx */ , /* value */]: println "trs message" break case ['rls', /* matches pattern *.xml */ , /* value */]: println "rls message" break default: println "no match" break } } switchcheck(list1) switchcheck(list2) i want check list data in switch statement of fields not matter, while others should match pattern (like ends *.xlsx or .xml)
i create demonstration example because monkey patch evil. can modify iscase @ runtime.
iscase method, when groovy calls in switch statement.(different java)
just call this:
... // don't need modify switchcheck method. def list2 = ['rls', 'file.xml', 'yyy'] list.metaclass.iscase = { object switchvalue -> if (!switchvalue in list && switchvalue.size() == 3) { false } else { switchvalue.first() == delegate.first() && (switchvalue[1]) in ~(createregexfromglob(delegate[1])) } } switchcheck(list1) the createregexfromglob copied here.
Comments
Post a Comment