Skip to content
This repository was archived by the owner on Dec 11, 2024. It is now read-only.

Include possibility to specify custom marshaller #201

Open
wants to merge 2 commits into
base: 1.0.0.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,55 +55,57 @@ class JSONDomainFactory {
if (object == null) {
return null
}
def marshaller
def marshaller = null
def objectClass = object.getClass()

// Check if we arrived from searchable domain class.
def parentObject = marshallingContext.peekDomainObject()
GrailsDomainClass domainClass = getDomainClass(parentObject)
def propertyMapping = elasticSearchContextHolder.getMappingContext(domainClass)?.getPropertyMapping(marshallingContext.lastParentPropertyName)
def customMarshaller = propertyMapping?.customMarshaller

if (customMarshaller && customMarshaller instanceof Class) {
marshaller = customMarshaller.newInstance()
}

// Resolve collections.
// Check for direct marshaller matching
if (object instanceof Collection) {
if (!marshaller && object instanceof Collection) {
marshaller = new CollectionMarshaller()
}

if (!marshaller && DEFAULT_MARSHALLERS[objectClass]) {
marshaller = DEFAULT_MARSHALLERS[objectClass].newInstance()
}

if (!marshaller) {

// Check if we arrived from searchable domain class.
def parentObject = marshallingContext.peekDomainObject()
if (parentObject && marshallingContext.lastParentPropertyName && DomainClassArtefactHandler.isDomainClass(parentObject.getClass())) {
GrailsDomainClass domainClass = getDomainClass(parentObject)
def propertyMapping = elasticSearchContextHolder.getMappingContext(domainClass)?.getPropertyMapping(marshallingContext.lastParentPropertyName)
def converter = propertyMapping?.converter
// Property has converter information. Lets see how we can apply it.
if (converter) {
// Property editor?
if (converter instanceof Class) {
if (PropertyEditor.isAssignableFrom(converter)) {
marshaller = new PropertyEditorMarshaller(propertyEditorClass: converter)
}
}
} else if (propertyMapping?.isDynamic()) {
marshaller = new DynamicValueMarshaller()
} else if (propertyMapping?.reference) {
def refClass = propertyMapping.getBestGuessReferenceType()
marshaller = new SearchableReferenceMarshaller(refClass: refClass)
} else if (propertyMapping?.component) {
if (propertyMapping?.isGeoPoint()) {
marshaller = new GeoPointMarshaller()
} else {
marshaller = new DeepDomainClassMarshaller()
if (!marshaller && parentObject && marshallingContext.lastParentPropertyName && DomainClassArtefactHandler.isDomainClass(parentObject.getClass())) {
def converter = propertyMapping?.converter
// Property has converter information. Lets see how we can apply it.
if (converter) {
// Property editor?
if (converter instanceof Class) {
if (PropertyEditor.isAssignableFrom(converter)) {
marshaller = new PropertyEditorMarshaller(propertyEditorClass: converter)
}
}
} else if (propertyMapping?.isDynamic()) {
marshaller = new DynamicValueMarshaller()
} else if (propertyMapping?.reference) {
def refClass = propertyMapping.getBestGuessReferenceType()
marshaller = new SearchableReferenceMarshaller(refClass: refClass)
} else if (propertyMapping?.component) {
if (propertyMapping?.isGeoPoint()) {
marshaller = new GeoPointMarshaller()
} else {
marshaller = new DeepDomainClassMarshaller()
}
}
}

if (!marshaller) {
// TODO : support user custom marshaller/converter (& marshaller registration)
// Check for domain classes
if (DomainClassArtefactHandler.isDomainClass(objectClass)) {
def propertyMapping = elasticSearchContextHolder.getMappingContext(getDomainClass(marshallingContext.peekDomainObject()))?.getPropertyMapping(marshallingContext.lastParentPropertyName)
propertyMapping = elasticSearchContextHolder.getMappingContext(getDomainClass(marshallingContext.peekDomainObject()))?.getPropertyMapping(marshallingContext.lastParentPropertyName)

if (propertyMapping?.isGeoPoint()) {
marshaller = new GeoPointMarshaller()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SearchableClassPropertyMapping {
private static final Set<String> SEARCHABLE_MAPPING_OPTIONS = ['boost', 'index', 'analyzer']
private static final Set<String> SEARCHABLE_SPECIAL_MAPPING_OPTIONS =
['component', 'converter', 'reference', 'excludeFromAll', 'maxDepth', 'multi_field', 'parent', 'geoPoint',
'alias', 'dynamic', 'attachment']
'alias', 'dynamic', 'attachment', 'customMarshaller'] as Set<String>

/** Grails attributes of this property */
private GrailsDomainClassProperty grailsProperty
Expand Down Expand Up @@ -79,6 +79,10 @@ class SearchableClassPropertyMapping {
specialMappingAttributes.converter
}

Object getCustomMarshaller() {
specialMappingAttributes.customMarshaller
}

Object getReference() {
specialMappingAttributes.reference
}
Expand Down