Skip to content

Commit 01df8c6

Browse files
Working on #9365
Refactoring codecs to use Groovy extension methods
1 parent f0d4735 commit 01df8c6

20 files changed

+135
-144
lines changed

grails-codecs/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dependencies {
2+
}

grails-plugin-codecs/src/main/groovy/org/grails/plugins/codecs/HexCodec.groovy renamed to grails-codecs/src/main/groovy/org/grails/plugins/codecs/HexCodecExtensionMethods.groovy

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
*/
1616
package org.grails.plugins.codecs
1717

18-
class HexCodec {
18+
import org.codehaus.groovy.runtime.NullObject
19+
20+
class HexCodecExtensionMethods {
1921

2022
static HEXDIGITS = '0123456789abcdef'
2123

2224
// Expects an array/list of numbers
23-
static encode = { theTarget ->
24-
if (theTarget == null) {
25+
static encodeAsHex(theTarget) {
26+
if (theTarget == null || theTarget instanceof NullObject) {
2527
return null
2628
}
2729

@@ -30,13 +32,13 @@ class HexCodec {
3032
theTarget = theTarget.getBytes("UTF-8")
3133
}
3234
theTarget.each() {
33-
result << HexCodec.HEXDIGITS[(it & 0xF0) >> 4]
34-
result << HexCodec.HEXDIGITS[it & 0x0F]
35+
result << HexCodecExtensionMethods.HEXDIGITS[(it & 0xF0) >> 4]
36+
result << HexCodecExtensionMethods.HEXDIGITS[it & 0x0F]
3537
}
3638
return result.toString()
3739
}
3840

39-
static decode = { theTarget ->
41+
static decodeHex(theTarget) {
4042
if (!theTarget) return null
4143

4244
def output = []

grails-plugin-codecs/src/main/groovy/org/grails/plugins/codecs/MD5Codec.groovy renamed to grails-codecs/src/main/groovy/org/grails/plugins/codecs/MD5BytesCodecExtensionMethods.groovy

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@
1515
*/
1616
package org.grails.plugins.codecs
1717

18-
class MD5Codec {
18+
import org.codehaus.groovy.runtime.NullObject
19+
20+
class MD5BytesCodecExtensionMethods {
1921
// Returns the byte[] of the digest, taken from UTF-8 of the string representation
2022
// or the raw data coerced to bytes
21-
static encode = { theTarget ->
22-
HexCodec.encode(MD5BytesCodec.encode(theTarget))
23+
static encodeAsMD5Bytes(theTarget) {
24+
if(theTarget == null || theTarget instanceof NullObject) {
25+
return null
26+
}
27+
DigestUtils.digest("MD5", theTarget)
2328
}
2429

25-
static decode = { theTarget ->
30+
static decodeMD5Bytes(theTarget) {
2631
throw new UnsupportedOperationException("Cannot decode MD5 hashes")
2732
}
2833
}

grails-plugin-codecs/src/main/groovy/org/grails/plugins/codecs/MD5BytesCodec.groovy renamed to grails-codecs/src/main/groovy/org/grails/plugins/codecs/MD5CodecExtensionMethods.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
*/
1616
package org.grails.plugins.codecs
1717

18-
class MD5BytesCodec {
18+
class MD5CodecExtensionMethods {
1919
// Returns the byte[] of the digest, taken from UTF-8 of the string representation
2020
// or the raw data coerced to bytes
21-
static encode = { theTarget ->
22-
DigestUtils.digest("MD5", theTarget)
21+
static encodeAsMD5(theTarget) {
22+
theTarget.encodeAsMD5Bytes()?.encodeAsHex()
2323
}
2424

25-
static decode = { theTarget ->
25+
static decodeMD5(theTarget) {
2626
throw new UnsupportedOperationException("Cannot decode MD5 hashes")
2727
}
2828
}

grails-plugin-codecs/src/main/groovy/org/grails/plugins/codecs/SHA1BytesCodec.groovy renamed to grails-codecs/src/main/groovy/org/grails/plugins/codecs/SHA1BytesCodecExtensionMethods.groovy

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
*/
1616
package org.grails.plugins.codecs
1717

18-
class SHA1BytesCodec {
18+
import org.codehaus.groovy.runtime.NullObject
19+
20+
class SHA1BytesCodecExtensionMethods {
1921
// Returns the byte[] of the digest
20-
static encode = { theTarget ->
22+
static encodeAsSHA1Bytes(theTarget) {
23+
if(theTarget == null || theTarget instanceof NullObject) {
24+
return null
25+
}
2126
DigestUtils.digest("SHA-1", theTarget)
2227
}
2328

24-
static decode = { theTarget ->
29+
static decodeSHA1Bytes(theTarget) {
2530
throw new UnsupportedOperationException("Cannot decode SHA-1 hashes")
2631
}
2732
}

grails-plugin-codecs/src/main/groovy/org/grails/plugins/codecs/SHA1Codec.groovy renamed to grails-codecs/src/main/groovy/org/grails/plugins/codecs/SHA1CodecExtensionMethods.groovy

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
*/
1616
package org.grails.plugins.codecs
1717

18-
class SHA1Codec{
18+
import org.codehaus.groovy.runtime.NullObject
19+
20+
class SHA1CodecExtensionMethods {
1921
// Returns the byte[] of the digest
20-
static encode = { theTarget ->
21-
HexCodec.encode(SHA1BytesCodec.encode(theTarget))
22+
static encodeAsSHA1(theTarget) {
23+
if(theTarget == null || theTarget instanceof NullObject) {
24+
return null
25+
}
26+
theTarget.encodeAsSHA1Bytes().encodeAsHex()
2227
}
2328

24-
static decode = { theTarget ->
29+
static decodeSHA1(theTarget) {
2530
throw new UnsupportedOperationException("Cannot decode SHA-1 hashes")
2631
}
2732
}

grails-plugin-codecs/src/main/groovy/org/grails/plugins/codecs/SHA256BytesCodec.groovy renamed to grails-codecs/src/main/groovy/org/grails/plugins/codecs/SHA256BytesCodecExtensionMethods.groovy

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
*/
1616
package org.grails.plugins.codecs
1717

18-
class SHA256BytesCodec {
18+
import org.codehaus.groovy.runtime.NullObject
19+
20+
class SHA256BytesCodecExtensionMethods {
1921
// Returns the byte[] of the digest
20-
static encode = { theTarget ->
22+
static encodeAsSHA256Bytes(theTarget) {
23+
if(theTarget == null || theTarget instanceof NullObject) {
24+
return null
25+
}
2126
DigestUtils.digest("SHA-256", theTarget)
2227
}
2328

24-
static decode = { theTarget ->
29+
static decodeSHA256Bytes(theTarget) {
2530
throw new UnsupportedOperationException("Cannot decode SHA-256 hashes")
2631
}
2732
}

grails-plugin-codecs/src/main/groovy/org/grails/plugins/codecs/SHA256Codec.groovy renamed to grails-codecs/src/main/groovy/org/grails/plugins/codecs/SHA256CodecExtensionMethods.groovy

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
*/
1616
package org.grails.plugins.codecs
1717

18-
class SHA256Codec extends DigestUtils {
18+
import org.codehaus.groovy.runtime.NullObject
19+
20+
class SHA256CodecExtensionMethods extends DigestUtils {
1921
// Returns the byte[] of the digest
20-
static encode = { theTarget ->
21-
HexCodec.encode(SHA256BytesCodec.encode(theTarget))
22+
static encodeAsSHA256(theTarget) {
23+
if(theTarget == null || theTarget instanceof NullObject) {
24+
return null
25+
}
26+
theTarget.encodeAsSHA256Bytes()?.encodeAsHex()
2227
}
2328

24-
static decode = { theTarget ->
29+
static decodeSHA256(theTarget) {
2530
throw new UnsupportedOperationException("Cannot decode SHA-256 hashes")
2631
}
2732
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
moduleName=grails-codecs
2+
moduleVersion=3.0
3+
extensionClasses=org.grails.plugins.codecs.HexCodecExtensionMethods,\
4+
org.grails.plugins.codecs.MD5BytesCodecExtensionMethods,\
5+
org.grails.plugins.codecs.MD5CodecExtensionMethods,\
6+
org.grails.plugins.codecs.SHA1BytesCodecExtensionMethods,\
7+
org.grails.plugins.codecs.SHA1CodecExtensionMethods,\
8+
org.grails.plugins.codecs.SHA256BytesCodecExtensionMethods,\
9+
org.grails.plugins.codecs.SHA256CodecExtensionMethods

0 commit comments

Comments
 (0)