Skip to content

Commit

Permalink
Tweak blocking ObjectIdSubstitutor to be a bit more like the async ve…
Browse files Browse the repository at this point in the history
…rsion

Just trying to make the transition it futures a bit smaller - this isn't
necessarily the best way to write a /simple/ blocking version, especially
given the object-id-mapping-function is memoised.
  • Loading branch information
rtyley committed Feb 17, 2014
1 parent 064d5da commit 3053e1d
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ trait ObjectIdSubstitutor {

// slow!
def replaceOldIds(message: String, reader: ObjectReader, mapper: Cleaner[ObjectId]): String = {
hexRegex.replaceAllIn(message, m => {
Some(AbbreviatedObjectId.fromString(m.matched)).flatMap(id=> reader.resolveExistingUniqueId(id).toOption).flatMap(mapper.substitution).map {
case (oldId, newId) =>
format(m.matched, reader.abbreviate(newId, m.matched.length).name)
}.getOrElse(m.matched)
})
val substitutionOpts = for {
m: String <- hexRegex.findAllIn(message).toSet
objectId <- reader.resolveExistingUniqueId(AbbreviatedObjectId.fromString(m)).toOption
} yield mapper.replacement(objectId).map(newId => m -> format(m, reader.abbreviate(newId, m.length).name))

val substitutions = substitutionOpts.flatten.toMap
if (substitutions.isEmpty) message else hexRegex.replaceSomeIn(message, m => substitutions.get(m.matched))
}
}

0 comments on commit 3053e1d

Please sign in to comment.