Skip to content

Added value conversion for char[] and short[] #1018

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

Merged
merged 1 commit into from
Oct 26, 2021
Merged
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
12 changes: 12 additions & 0 deletions driver/src/main/java/org/neo4j/driver/Values.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@ public static Value value( Object value )
if ( value instanceof Iterator<?> ) { return value( (Iterator<Object>) value ); }
if ( value instanceof Stream<?> ) { return value( (Stream<Object>) value ); }

if ( value instanceof char[] ) { return value( (char[]) value ); }
if ( value instanceof byte[] ) { return value( (byte[]) value ); }
if ( value instanceof boolean[] ) { return value( (boolean[]) value ); }
if ( value instanceof String[] ) { return value( (String[]) value ); }
if ( value instanceof long[] ) { return value( (long[]) value ); }
if ( value instanceof int[] ) { return value( (int[]) value ); }
if ( value instanceof short[] ) { return value( (short[]) value ); }
if ( value instanceof double[] ) { return value( (double[]) value ); }
if ( value instanceof float[] ) { return value( (float[]) value ); }
if ( value instanceof Value[] ) { return value( (Value[]) value ); }
Expand Down Expand Up @@ -197,6 +199,16 @@ public static Value value( long... input )
return new ListValue( values );
}

public static Value value( short... input )
{
Value[] values = new Value[input.length];
for ( int i = 0; i < input.length; i++ )
{
values[i] = value( input[i] );
}
return new ListValue( values );
}

public static Value value( int... input )
{
Value[] values = new Value[input.length];
Expand Down
31 changes: 31 additions & 0 deletions driver/src/test/java/org/neo4j/driver/internal/ValuesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ class ValuesTest
@Test
void shouldConvertPrimitiveArrays()
{
assertThat( value( new short[]{1, 2, 3} ),
equalTo( new ListValue( values( 1, 2, 3 ) ) ) );

assertThat( value( new int[]{1, 2, 3} ),
equalTo( new ListValue( values( 1, 2, 3 ) ) ) );

Expand All @@ -112,6 +115,34 @@ void shouldConvertPrimitiveArrays()
equalTo( new ListValue( values( "a", "b", "c" ) ) ) );
}

@Test
void shouldConvertPrimitiveArraysFromObject()
{
assertThat( value( (Object) new short[]{1, 2, 3} ),
equalTo( new ListValue( values( 1, 2, 3 ) ) ) );

assertThat( value( (Object) new int[]{1, 2, 3} ),
equalTo( new ListValue( values( 1, 2, 3 ) ) ) );

assertThat( value( (Object) new long[]{1, 2, 3} ),
equalTo( new ListValue( values( 1, 2, 3 ) ) ) );

assertThat( value( (Object) new float[]{1.1f, 2.2f, 3.3f} ),
equalTo( new ListValue( values( 1.1f, 2.2f, 3.3f ) ) ) );

assertThat( value( (Object) new double[]{1.1, 2.2, 3.3} ),
equalTo( new ListValue( values( 1.1, 2.2, 3.3 ) ) ) );

assertThat( value( (Object) new boolean[]{true, false, true} ),
equalTo( new ListValue( values( true, false, true ) ) ) );

assertThat( value( (Object) new char[]{'a', 'b', 'c'} ),
equalTo( new ListValue( values( 'a', 'b', 'c' ) ) ) );

assertThat( value( (Object) new String[]{"a", "b", "c"} ),
equalTo( new ListValue( values( "a", "b", "c" ) ) ) );
}

@Test
void shouldComplainAboutStrangeTypes()
{
Expand Down