Skip to content

Commit b365a0f

Browse files
committed
Add make and encoder functions for Point2&3D
1 parent 1999893 commit b365a0f

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

include/mgclient.h

+11
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,11 @@ MGCLIENT_EXPORT mg_duration *mg_duration_copy(const mg_duration *duration);
10101010
/// Destroy the given duration.
10111011
MGCLIENT_EXPORT void mg_duration_destroy(mg_duration *duration);
10121012

1013+
/// Creates mg_point_2d from srid, x_longitude and y_latitude.
1014+
/// \return a pointer to mg_point_2d or NULL if an error occured.
1015+
MGCLIENT_EXPORT mg_point_2d *mg_point_2d_make(uint16_t srid, double x_longitude,
1016+
double y_latitude);
1017+
10131018
/// Returns SRID of the 2D point.
10141019
MGCLIENT_EXPORT int64_t mg_point_2d_srid(const mg_point_2d *point_2d);
10151020

@@ -1027,6 +1032,12 @@ MGCLIENT_EXPORT mg_point_2d *mg_point_2d_copy(const mg_point_2d *point_2d);
10271032
/// Destroys the given 2D point.
10281033
MGCLIENT_EXPORT void mg_point_2d_destroy(mg_point_2d *point_2d);
10291034

1035+
/// Creates mg_point_3d from srid, x_longitude, y_latitude and z_height.
1036+
/// \return a pointer to mg_point_3d or NULL if an error occured.
1037+
MGCLIENT_EXPORT mg_point_3d *mg_point_3d_make(uint16_t srid, double x_longitude,
1038+
double y_latitude,
1039+
double z_height);
1040+
10301041
/// Returns SRID of the 3D point.
10311042
MGCLIENT_EXPORT int64_t mg_point_3d_srid(const mg_point_3d *point_3d);
10321043

src/mgsession-encoder.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ int mg_session_write_duration(mg_session *session, const mg_duration *dur) {
168168
return 0;
169169
}
170170

171+
int mg_session_write_point_2d(mg_session *session, const mg_point_2d *point) {
172+
MG_RETURN_IF_FAILED(
173+
mg_session_write_uint8(session, (uint8_t)(MG_MARKER_TINY_STRUCT3)));
174+
MG_RETURN_IF_FAILED(mg_session_write_uint8(session, MG_SIGNATURE_POINT_2D));
175+
MG_RETURN_IF_FAILED(mg_session_write_integer(session, point->srid));
176+
MG_RETURN_IF_FAILED(mg_session_write_float(session, point->x));
177+
MG_RETURN_IF_FAILED(mg_session_write_float(session, point->y));
178+
return 0;
179+
}
180+
171181
int mg_session_write_value(mg_session *session, const mg_value *value) {
172182
switch (value->type) {
173183
case MG_VALUE_TYPE_NULL:
@@ -219,8 +229,9 @@ int mg_session_write_value(mg_session *session, const mg_value *value) {
219229
case MG_VALUE_TYPE_DURATION:
220230
return mg_session_write_duration(session, value->duration_v);
221231
case MG_VALUE_TYPE_POINT_2D:
222-
mg_session_set_error(session, "tried to send value of type 'point_2d'");
223-
return MG_ERROR_INVALID_VALUE;
232+
return mg_session_write_point_2d(session, value->point_2d_v);
233+
// mg_session_set_error(session, "tried to send value of type
234+
// 'point_2d'"); // TODO(gitbuda): remove return MG_ERROR_INVALID_VALUE;
224235
case MG_VALUE_TYPE_POINT_3D:
225236
mg_session_set_error(session, "tried to send value of type 'point_3d'");
226237
return MG_ERROR_INVALID_VALUE;

src/mgvalue.c

+26
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,32 @@ mg_duration *mg_duration_make(int64_t months, int64_t days, int64_t seconds,
16821682
return dur;
16831683
}
16841684

1685+
mg_point_2d *mg_point_2d_make(uint16_t srid, double x_longitude,
1686+
double y_latitude) {
1687+
mg_point_2d *point = mg_point_2d_alloc(&mg_system_allocator);
1688+
if (!point) {
1689+
return NULL;
1690+
}
1691+
point->srid = srid;
1692+
point->x = x_longitude;
1693+
point->y = y_latitude;
1694+
return point;
1695+
}
1696+
1697+
mg_point_3d *mg_point_3d_make(uint16_t srid, double x_longitude,
1698+
double y_latitude, double z_height) {
1699+
mg_point_3d *point = mg_point_3d_alloc(&mg_system_allocator);
1700+
if (!point) {
1701+
return NULL;
1702+
}
1703+
point->srid = srid;
1704+
point->x = x_longitude;
1705+
point->y = y_latitude;
1706+
point->z = z_height;
1707+
;
1708+
return point;
1709+
}
1710+
16851711
int mg_string_equal(const mg_string *lhs, const mg_string *rhs) {
16861712
if (lhs->size != rhs->size) {
16871713
return 0;

0 commit comments

Comments
 (0)