Skip to content

DATACMNS-874 - Fix Scala Option unwrapping. #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2016 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.
Expand All @@ -15,7 +15,9 @@
*/
package org.springframework.data.repository.util;

import scala.Function0;
import scala.Option;
import scala.runtime.AbstractFunction0;

import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -48,6 +50,7 @@
* </ul>
*
* @author Oliver Gierke
* @author Mark Paluch
* @since 1.8
*/
public abstract class QueryExecutionConverters {
Expand Down Expand Up @@ -416,19 +419,27 @@ public Object convert(Object source) {
* A {@link Converter} to unwrap a Scala {@link Option} instance.
*
* @author Oliver Gierke
* @author Mark Paluch
* @author 1.13
*/
private static enum ScalOptionUnwrapper implements Converter<Object, Object> {

INSTANCE;

/*
private final Function0<Object> alternative = new AbstractFunction0<Object>() {
@Override
public Option<Object> apply() {
return null;
}
};

/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/
@Override
public Object convert(Object source) {
return source instanceof Option ? ((Option<?>) source).orNull(null) : source;
return source instanceof Option ? ((Option<?>) source).getOrElse(alternative) : source;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2016 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.
Expand Down Expand Up @@ -37,6 +37,7 @@
* Unit tests for {@link QueryExecutionConverters}.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
public class QueryExecutionConvertersUnitTests {

Expand Down Expand Up @@ -163,4 +164,12 @@ public void turnsNullIntoScalaOptionEmpty() {
public void unwrapsScalaOption() {
assertThat(QueryExecutionConverters.unwrap(Option.apply("foo")), is((Object) "foo"));
}

/**
* @see DATACMNS-874
*/
@Test
public void unwrapsEmptyScalaOption() {
assertThat(QueryExecutionConverters.unwrap(Option.empty()), is((Object) null));
}
}