Skip to content

Commit

Permalink
Bolt support for char and char[]
Browse files Browse the repository at this point in the history
We simply support `char` and `char[]` by treating them as String
  • Loading branch information
pontusmelke authored and Zhen committed Jul 5, 2016
1 parent 8866fd9 commit 3a8e8fc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
Expand Up @@ -90,6 +90,11 @@ else if ( obj instanceof String )
{ {
pack( (String) obj ); pack( (String) obj );
} }
else if (obj instanceof Character )
{
Character character = (Character) obj;
pack( character.toString() );
}
else if ( obj instanceof Map ) else if ( obj instanceof Map )
{ {
Map<Object, Object> map = (Map<Object, Object>) obj; Map<Object, Object> map = (Map<Object, Object>) obj;
Expand All @@ -115,6 +120,11 @@ else if ( obj instanceof byte[] )
// Pending decision // Pending decision
throw new UnsupportedOperationException( "Binary values cannot be packed." ); throw new UnsupportedOperationException( "Binary values cannot be packed." );
} }
else if ( obj instanceof char[] )
{
// Treat it as a String
pack( new String( (char[]) obj ) );
}
else if ( obj instanceof short[] ) else if ( obj instanceof short[] )
{ {
short[] array = (short[]) obj; short[] array = (short[]) obj;
Expand Down
Expand Up @@ -216,6 +216,11 @@ public void pack( double value ) throws IOException
out.writeByte( FLOAT_64 ).writeDouble( value ); out.writeByte( FLOAT_64 ).writeDouble( value );
} }


public void pack( char character ) throws IOException
{
pack( new String( new char[]{character} ) );
}

public void pack( String value ) throws IOException public void pack( String value ) throws IOException
{ {
if ( value == null ) { packNull(); } if ( value == null ) { packNull(); }
Expand Down
Expand Up @@ -180,4 +180,32 @@ public void shouldNotBeAbleToUnpackPaths() throws IOException
unpacked( packed( path ) ); unpacked( packed( path ) );
} }
} }

@Test
public void shouldTreatSingleCharAsSingleCharacterString() throws IOException
{
// Given
PackedOutputArray output = new PackedOutputArray();
Neo4jPack.Packer packer = new Neo4jPack.Packer( output );
packer.pack( 'C' );
Object unpacked = unpacked( output.bytes() );

// Then
assertThat( unpacked, instanceOf( String.class ) );
assertThat( unpacked, equalTo( "C" ) );
}

@Test
public void shouldTreatCharArrayAsString() throws IOException
{
// Given
PackedOutputArray output = new PackedOutputArray();
Neo4jPack.Packer packer = new Neo4jPack.Packer( output );
packer.pack( new char[]{'W', 'H', 'Y'} );
Object unpacked = unpacked( output.bytes() );

// Then
assertThat( unpacked, instanceOf( String.class ) );
assertThat( unpacked, equalTo( "WHY" ) );
}
} }

0 comments on commit 3a8e8fc

Please sign in to comment.