From 4294ce0c23e5947907150f13f84fd46cf8b71cac Mon Sep 17 00:00:00 2001 From: Eric Helgeson Date: Thu, 29 Nov 2018 10:30:13 -0600 Subject: [PATCH] Add a UUID Value Converter --- .../converters/UUIDConverter.groovy | 45 +++++++++++++ .../converters/UUIDConversionSpec.groovy | 66 +++++++++++++++++++ .../DataBindingGrailsPlugin.groovy | 2 + 3 files changed, 113 insertions(+) create mode 100644 grails-databinding/src/main/groovy/org/grails/databinding/converters/UUIDConverter.groovy create mode 100644 grails-databinding/src/test/groovy/org/grails/databinding/converters/UUIDConversionSpec.groovy diff --git a/grails-databinding/src/main/groovy/org/grails/databinding/converters/UUIDConverter.groovy b/grails-databinding/src/main/groovy/org/grails/databinding/converters/UUIDConverter.groovy new file mode 100644 index 00000000000..c40920d14f8 --- /dev/null +++ b/grails-databinding/src/main/groovy/org/grails/databinding/converters/UUIDConverter.groovy @@ -0,0 +1,45 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.grails.databinding.converters + +import grails.databinding.converters.ValueConverter; +import groovy.transform.CompileStatic + +@CompileStatic +class UUIDConverter implements ValueConverter { + + @Override + boolean canConvert(value) { + value instanceof String + } + + @Override + def convert(value) { + if(value) { + try { + return UUID.fromString(value as String) + } catch(IllegalArgumentException ignore) { + return null + } + } + return null + } + + @Override + Class getTargetType() { + UUID + } +} diff --git a/grails-databinding/src/test/groovy/org/grails/databinding/converters/UUIDConversionSpec.groovy b/grails-databinding/src/test/groovy/org/grails/databinding/converters/UUIDConversionSpec.groovy new file mode 100644 index 00000000000..bb0c3d2be71 --- /dev/null +++ b/grails-databinding/src/test/groovy/org/grails/databinding/converters/UUIDConversionSpec.groovy @@ -0,0 +1,66 @@ +package org.grails.databinding.converters + + +import grails.databinding.SimpleMapDataBindingSource + +import grails.databinding.SimpleDataBinder + +import spock.lang.Specification + +class UUIDConversionSpec extends Specification { + + void 'Binding String to a UUID'() { + given: + def binder = new SimpleDataBinder() + binder.registerConverter new UUIDConverter() + def testClass = new UUIDTestClass() + + and: + def givenUUID = '534f7cee-bf88-45f3-96f2-9cae0828cd16' + + when: + binder.bind testClass, [uuid: givenUUID] as SimpleMapDataBindingSource + + then: + testClass.uuid instanceof UUID + testClass.uuid.toString() == givenUUID + } + + void 'Binding badly formatted string to a UUID'() { + given: + def binder = new SimpleDataBinder() + binder.registerConverter new UUIDConverter() + def testClass = new UUIDTestClass() + + and: + def givenUUID = '123-not-a-uuid-3291' + + when: + binder.bind testClass, [uuid: givenUUID] as SimpleMapDataBindingSource + + then: + notThrown(IllegalArgumentException) + testClass.uuid == null + } + + void 'Binding null to UUID'() { + given: + def binder = new SimpleDataBinder() + binder.registerConverter new UUIDConverter() + def testClass = new UUIDTestClass() + + and: + def givenUUID = null + + when: + binder.bind testClass, [uuid: givenUUID] as SimpleMapDataBindingSource + + then: + notThrown(IllegalArgumentException) + testClass.uuid == null + } +} + +class UUIDTestClass { + UUID uuid +} diff --git a/grails-plugin-databinding/src/main/groovy/org/grails/plugins/databinding/DataBindingGrailsPlugin.groovy b/grails-plugin-databinding/src/main/groovy/org/grails/plugins/databinding/DataBindingGrailsPlugin.groovy index 6e8d54871ba..faf5b040afa 100644 --- a/grails-plugin-databinding/src/main/groovy/org/grails/plugins/databinding/DataBindingGrailsPlugin.groovy +++ b/grails-plugin-databinding/src/main/groovy/org/grails/plugins/databinding/DataBindingGrailsPlugin.groovy @@ -20,6 +20,7 @@ import grails.plugins.Plugin import grails.util.GrailsUtil import grails.web.databinding.DataBindingUtils import grails.web.databinding.GrailsWebDataBinder +import org.grails.databinding.converters.UUIDConverter import org.grails.web.databinding.bindingsource.DataBindingSourceRegistry import org.grails.web.databinding.bindingsource.DefaultDataBindingSourceRegistry import org.grails.web.databinding.bindingsource.HalJsonDataBindingSourceCreator @@ -69,6 +70,7 @@ class DataBindingGrailsPlugin extends Plugin { } timeZoneConverter(TimeZoneConverter) + uuidConverter(UUIDConverter) defaultDateConverter(DateConversionHelper) { formatStrings = dateFormats