Skip to content

Commit ee2355f

Browse files
authored
Merge pull request #11176 from erichelgeson/eric/uuidConverter
Add a UUID Value Converter
2 parents 0e4dd22 + 4294ce0 commit ee2355f

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.grails.databinding.converters
17+
18+
import grails.databinding.converters.ValueConverter;
19+
import groovy.transform.CompileStatic
20+
21+
@CompileStatic
22+
class UUIDConverter implements ValueConverter {
23+
24+
@Override
25+
boolean canConvert(value) {
26+
value instanceof String
27+
}
28+
29+
@Override
30+
def convert(value) {
31+
if(value) {
32+
try {
33+
return UUID.fromString(value as String)
34+
} catch(IllegalArgumentException ignore) {
35+
return null
36+
}
37+
}
38+
return null
39+
}
40+
41+
@Override
42+
Class<?> getTargetType() {
43+
UUID
44+
}
45+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.grails.databinding.converters
2+
3+
4+
import grails.databinding.SimpleMapDataBindingSource
5+
6+
import grails.databinding.SimpleDataBinder
7+
8+
import spock.lang.Specification
9+
10+
class UUIDConversionSpec extends Specification {
11+
12+
void 'Binding String to a UUID'() {
13+
given:
14+
def binder = new SimpleDataBinder()
15+
binder.registerConverter new UUIDConverter()
16+
def testClass = new UUIDTestClass()
17+
18+
and:
19+
def givenUUID = '534f7cee-bf88-45f3-96f2-9cae0828cd16'
20+
21+
when:
22+
binder.bind testClass, [uuid: givenUUID] as SimpleMapDataBindingSource
23+
24+
then:
25+
testClass.uuid instanceof UUID
26+
testClass.uuid.toString() == givenUUID
27+
}
28+
29+
void 'Binding badly formatted string to a UUID'() {
30+
given:
31+
def binder = new SimpleDataBinder()
32+
binder.registerConverter new UUIDConverter()
33+
def testClass = new UUIDTestClass()
34+
35+
and:
36+
def givenUUID = '123-not-a-uuid-3291'
37+
38+
when:
39+
binder.bind testClass, [uuid: givenUUID] as SimpleMapDataBindingSource
40+
41+
then:
42+
notThrown(IllegalArgumentException)
43+
testClass.uuid == null
44+
}
45+
46+
void 'Binding null to UUID'() {
47+
given:
48+
def binder = new SimpleDataBinder()
49+
binder.registerConverter new UUIDConverter()
50+
def testClass = new UUIDTestClass()
51+
52+
and:
53+
def givenUUID = null
54+
55+
when:
56+
binder.bind testClass, [uuid: givenUUID] as SimpleMapDataBindingSource
57+
58+
then:
59+
notThrown(IllegalArgumentException)
60+
testClass.uuid == null
61+
}
62+
}
63+
64+
class UUIDTestClass {
65+
UUID uuid
66+
}

grails-plugin-databinding/src/main/groovy/org/grails/plugins/databinding/DataBindingGrailsPlugin.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import grails.plugins.Plugin
2020
import grails.util.GrailsUtil
2121
import grails.web.databinding.DataBindingUtils
2222
import grails.web.databinding.GrailsWebDataBinder
23+
import org.grails.databinding.converters.UUIDConverter
2324
import org.grails.web.databinding.bindingsource.DataBindingSourceRegistry
2425
import org.grails.web.databinding.bindingsource.DefaultDataBindingSourceRegistry
2526
import org.grails.web.databinding.bindingsource.HalJsonDataBindingSourceCreator
@@ -69,6 +70,7 @@ class DataBindingGrailsPlugin extends Plugin {
6970
}
7071

7172
timeZoneConverter(TimeZoneConverter)
73+
uuidConverter(UUIDConverter)
7274

7375
defaultDateConverter(DateConversionHelper) {
7476
formatStrings = dateFormats

0 commit comments

Comments
 (0)