Skip to content

Commit

Permalink
Added serialize config
Browse files Browse the repository at this point in the history
  • Loading branch information
dhalupa committed Mar 5, 2012
1 parent 0091a8f commit fc5a81b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
@@ -0,0 +1,12 @@
package org.grails.plugins.marshallers

public class ActionConfigBuilder{
def config=[:]
def methodMissing(String name,args){
if(args.size()==1 && args[0] instanceof Closure){
config[name]=args[0]
}else{
throw new IllegalStateException('Illegal syntax encountered while building serializers config: '+ name)
}
}
}
Expand Up @@ -82,8 +82,14 @@ class GenericDomainClassXMLMarshaller implements ObjectMarshaller<XML>,NameAware

for (GrailsDomainClassProperty property : properties) {
if(!isIn(mc,'ignore',property.getName()) && !isIn(mc,'attribute',property.getName())){
LOG.debug("Trying to write field as xml element: $property.name on $value")
writeElement(xml, property, beanWrapper,mc);
def serializers=mc?.serializer
if(serializers && serializers[property.name]){
Object val = beanWrapper.getPropertyValue(property.getName());
serializers[property.name].call(val,xml)
}else{
LOG.debug("Trying to write field as xml element: $property.name on $value")
writeElement(xml, property, beanWrapper,mc);
}
}
}
}
Expand Down Expand Up @@ -166,12 +172,10 @@ class GenericDomainClassXMLMarshaller implements ObjectMarshaller<XML>,NameAware
@SuppressWarnings("unused") GrailsDomainClass referencedDomainClass) throws ConverterException {
Object idValue;
if(proxyHandler instanceof EntityProxyHandler) {

idValue = ((EntityProxyHandler) proxyHandler).getProxyIdentifier(refObj);
if(idValue == null) {
idValue = new BeanWrapperImpl(refObj).getPropertyValue(idProperty.getName());
}

}
else {
idValue = new BeanWrapperImpl(refObj).getPropertyValue(idProperty.getName());
Expand Down
@@ -1,30 +1,44 @@
package org.grails.plugins.marshallers.config

import java.util.Map;
import org.grails.plugins.marshallers.ActionConfigBuilder



class MarshallingConfigBuilder {
def static singleValueAttrs=['elementName']

def static rules=[
singleAttr:[
members:['elementName'],
rule:{args-> args[0] }
],
actions:[
members:['serializer'],
rule:{args->
ActionConfigBuilder builder=new ActionConfigBuilder();
def closure = args[0]
closure.delegate = builder
closure.resolveStrategy = Closure.DELEGATE_FIRST;
closure.call();
builder.config;
}
]
]

def config=[:];

def methodMissing(String name,args){
if(args.size()==1 && args[0] instanceof Closure){
def entry=rules.find{key,value->value.members.contains(name)};
if(entry!=null){
config[name]=entry.value.rule.call(args)
}else if(args.size()==1 && args[0] instanceof Closure){
MarshallingConfigBuilder builder=new MarshallingConfigBuilder();
def closure = args[0]
closure.delegate = builder
closure.resolveStrategy = Closure.DELEGATE_FIRST;
closure.call();
config[name]=builder.config;
}else{
boolean singleValue=singleValueAttrs.find {it==name}!=null;
if(singleValue){
if(args.size()==1){
config[name]=args[0];
}else{
throw new IllegalStateException('Illegal syntax for '+ name)
}
}else{
config[name]=args;
}
config[name]=args
}
}
}
Expand Up @@ -20,6 +20,9 @@ class MarshallingConfigBuilderTests extends GrailsUnitTestCase {
named {
attributes 'some', 'some1'
ignore 'ig', 'big'
serializer {
taxonomies {val,xml-> println val }
}
}
}
json {
Expand All @@ -43,6 +46,7 @@ class MarshallingConfigBuilderTests extends GrailsUnitTestCase {
println c.config;
assertEquals(c.getConfig('xml','default').elementName,'elName')
assertNull(c.getConfig('xml','named').elementName)
assertTrue(c.getConfig('xml','named').serializer.taxonomies instanceof Closure)
assertEquals(c.getConfig('xml','default').attributes.size(),2)
assertEquals(c.getConfig('some','default').size(),0)
assertEquals(c.getConfig('xml', 'some').size(),0)
Expand Down

0 comments on commit fc5a81b

Please sign in to comment.