1616 * See the License for the specific language governing permissions and
1717 * limitations under the License.
1818 */
19- package org .neo4j .driver .internal .messaging .v2 ;
19+
20+ package org .neo4j .driver .internal .messaging .common ;
2021
2122import java .io .IOException ;
2223import java .time .LocalDate ;
2627import java .time .ZoneId ;
2728import java .time .ZoneOffset ;
2829import java .time .ZonedDateTime ;
30+ import java .util .Map ;
2931
32+ import org .neo4j .driver .Value ;
3033import org .neo4j .driver .internal .InternalPoint2D ;
3134import org .neo4j .driver .internal .InternalPoint3D ;
32- import org .neo4j .driver .internal .messaging .v1 . ValuePackerV1 ;
35+ import org .neo4j .driver .internal .messaging .ValuePacker ;
3336import org .neo4j .driver .internal .packstream .PackOutput ;
34- import org .neo4j .driver .internal .types . TypeConstructor ;
37+ import org .neo4j .driver .internal .packstream . PackStream ;
3538import org .neo4j .driver .internal .value .InternalValue ;
3639import org .neo4j .driver .types .IsoDuration ;
3740import org .neo4j .driver .types .Point ;
3841
3942import static java .time .ZoneOffset .UTC ;
40- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .DATE ;
41- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .DATE_STRUCT_SIZE ;
42- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .DATE_TIME_STRUCT_SIZE ;
43- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .DATE_TIME_WITH_ZONE_ID ;
44- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .DATE_TIME_WITH_ZONE_OFFSET ;
45- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .DURATION ;
46- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .DURATION_TIME_STRUCT_SIZE ;
47- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .LOCAL_DATE_TIME ;
48- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .LOCAL_DATE_TIME_STRUCT_SIZE ;
49- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .LOCAL_TIME ;
50- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .LOCAL_TIME_STRUCT_SIZE ;
51- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .POINT_2D_STRUCT_SIZE ;
52- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .POINT_2D_STRUCT_TYPE ;
53- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .POINT_3D_STRUCT_SIZE ;
54- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .POINT_3D_STRUCT_TYPE ;
55- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .TIME ;
56- import static org .neo4j .driver .internal .messaging .v2 .MessageFormatV2 .TIME_STRUCT_SIZE ;
57-
58- public class ValuePackerV2 extends ValuePackerV1
43+
44+ public class CommonValuePacker implements ValuePacker
5945{
60- public ValuePackerV2 ( PackOutput output )
46+
47+ public static final byte DATE = 'D' ;
48+ public static final int DATE_STRUCT_SIZE = 1 ;
49+
50+ public static final byte TIME = 'T' ;
51+ public static final int TIME_STRUCT_SIZE = 2 ;
52+
53+ public static final byte LOCAL_TIME = 't' ;
54+ public static final int LOCAL_TIME_STRUCT_SIZE = 1 ;
55+
56+ public static final byte LOCAL_DATE_TIME = 'd' ;
57+ public static final int LOCAL_DATE_TIME_STRUCT_SIZE = 2 ;
58+
59+ public static final byte DATE_TIME_WITH_ZONE_OFFSET = 'F' ;
60+ public static final byte DATE_TIME_WITH_ZONE_ID = 'f' ;
61+ public static final int DATE_TIME_STRUCT_SIZE = 3 ;
62+
63+ public static final byte DURATION = 'E' ;
64+ public static final int DURATION_TIME_STRUCT_SIZE = 4 ;
65+
66+ public static final byte POINT_2D_STRUCT_TYPE = 'X' ;
67+ public static final int POINT_2D_STRUCT_SIZE = 3 ;
68+
69+ public static final byte POINT_3D_STRUCT_TYPE = 'Y' ;
70+ public static final int POINT_3D_STRUCT_SIZE = 4 ;
71+
72+ protected final PackStream .Packer packer ;
73+
74+ public CommonValuePacker ( PackOutput output )
75+ {
76+ this .packer = new PackStream .Packer ( output );
77+ }
78+
79+ @ Override
80+ public final void packStructHeader ( int size , byte signature ) throws IOException
81+ {
82+ packer .packStructHeader ( size , signature );
83+ }
84+
85+ @ Override
86+ public final void pack ( String string ) throws IOException
6187 {
62- super ( output );
88+ packer .pack ( string );
89+ }
90+
91+ @ Override
92+ public final void pack ( Value value ) throws IOException
93+ {
94+ if ( value instanceof InternalValue )
95+ {
96+ packInternalValue ( ((InternalValue ) value ) );
97+ }
98+ else
99+ {
100+ throw new IllegalArgumentException ( "Unable to pack: " + value );
101+ }
63102 }
64103
65104 @ Override
105+ public final void pack ( Map <String ,Value > map ) throws IOException
106+ {
107+ if ( map == null || map .size () == 0 )
108+ {
109+ packer .packMapHeader ( 0 );
110+ return ;
111+ }
112+ packer .packMapHeader ( map .size () );
113+ for ( Map .Entry <String ,Value > entry : map .entrySet () )
114+ {
115+ packer .pack ( entry .getKey () );
116+ pack ( entry .getValue () );
117+ }
118+ }
119+
66120 protected void packInternalValue ( InternalValue value ) throws IOException
67121 {
68- TypeConstructor typeConstructor = value .typeConstructor ();
69- switch ( typeConstructor )
122+ switch ( value .typeConstructor () )
70123 {
71124 case DATE :
72125 packDate ( value .asLocalDate () );
@@ -89,8 +142,49 @@ protected void packInternalValue( InternalValue value ) throws IOException
89142 case POINT :
90143 packPoint ( value .asPoint () );
91144 break ;
145+ case NULL :
146+ packer .packNull ();
147+ break ;
148+
149+ case BYTES :
150+ packer .pack ( value .asByteArray () );
151+ break ;
152+
153+ case STRING :
154+ packer .pack ( value .asString () );
155+ break ;
156+
157+ case BOOLEAN :
158+ packer .pack ( value .asBoolean () );
159+ break ;
160+
161+ case INTEGER :
162+ packer .pack ( value .asLong () );
163+ break ;
164+
165+ case FLOAT :
166+ packer .pack ( value .asDouble () );
167+ break ;
168+
169+ case MAP :
170+ packer .packMapHeader ( value .size () );
171+ for ( String s : value .keys () )
172+ {
173+ packer .pack ( s );
174+ pack ( value .get ( s ) );
175+ }
176+ break ;
177+
178+ case LIST :
179+ packer .packListHeader ( value .size () );
180+ for ( Value item : value .values () )
181+ {
182+ pack ( item );
183+ }
184+ break ;
185+
92186 default :
93- super . packInternalValue ( value );
187+ throw new IOException ( "Unknown type: " + value . type (). name () );
94188 }
95189 }
96190
0 commit comments