Table of contents
- JSON object to map
- Switch Statement
- Use class in case
- deconstruct return
- example 1
- example 2
- GISTs
- Use Comparator
- Re-group and spread map into new map
- HTTP Builder get binary stream
- Use HTTP builder to add attachment to an email from an S3 bucket
JSON object to map
import com.fasterxml.jackson.databind.ObjectMapper
HashMap<String, Object> returnVal = new ObjectMapper().readValue(response.json.toString(), HashMap.class)
def slurper = new groovy.json.JsonSlurper()
Map<String, Object> result = slurper.parseText(ftpJSONBody) as Map<String, Object>
def paramMap = new grails.web.servlet.mvc.GrailsParameterMap(result, request)
def cmdObj = new IntegrationCommand(paramMap) as JSON
HashMap<String, Object> expected = new ObjectMapper().readValue(cmdObj.toString(), HashMap.class)
expected
Switch Statement
Use class in case
switch (ug) {
case GroupCompare:
comparedGroupIds = ((GroupCompare) ug)?.userGroupJoinGroupCompare*.userGroup.id
if (!comparedGroupIds || comparedGroupIds?.size() == 0) comparedGroupIds = null
comparedAssessmentOrderIds = ((GroupCompare) ug)?.groupAssessmentOrders*.assessmentOrderId
break
case ResultCompare:
comparedGroupIds = ((ResultCompare) ug)?.groupCompareJoinUserGroup?.userGroupId ? [((ResultCompare) ug)?.groupCompareJoinUserGroup?.userGroupId] : null
comparedAssessmentOrderIds = ((ResultCompare) ug)?.groupAssessmentOrders*.assessmentOrderId
break
case UserGroup:
DetachedCriteria<GroupCompare> groupCompareCriteria = findCompareGroupsByGroupCriteria(ug)
groupCompareCriteria.projections { property('id') }
comparedGroupIds = groupCompareCriteria.list()
break
default:
break
}
deconstruct return
example 1
def fetchGroupAndAssessmentIds
{ var ->
def comparedGroupIds, comparedAssessmentOrderIds
return [comparedGroupIds, comparedAssessmentOrderIds]
}
def (comparedGroupIds, comparedAssessmentOrderIds) = fetchGroupAndAssessmentIds(ug)
example 2
def fetchGroupAndAssessmentIds =
{ var ->
def comparedGroupIds, comparedAssessmentOrderIds
return [[returnRGList: returnRGList], [unmatchedGroups: unmatchedGroups], [duplicates: duplicates], [requestedAdminGroups: requestedAdminGroups], [originalValueMap: originalValueMap]]
}
def (roleGroupList, unmatchedGroups, duplicates, requestedAdminGroups, originalValueMap) = fetchGroupAndAssessmentIds(roleGroups)
GISTs
Use Comparator
comparator for 2 merged lists
Re-group and spread map into new map
create new map by conditions
HTTP Builder get binary stream
bin stream
Use HTTP builder to add attachment to an email from an S3 bucket
http builder