Skip to content

Commit

Permalink
Add make and encoder functions for Point2&3D
Browse files Browse the repository at this point in the history
  • Loading branch information
gitbuda committed Aug 16, 2024
1 parent 1999893 commit b365a0f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
11 changes: 11 additions & 0 deletions include/mgclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,11 @@ MGCLIENT_EXPORT mg_duration *mg_duration_copy(const mg_duration *duration);
/// Destroy the given duration.
MGCLIENT_EXPORT void mg_duration_destroy(mg_duration *duration);

/// Creates mg_point_2d from srid, x_longitude and y_latitude.
/// \return a pointer to mg_point_2d or NULL if an error occured.
MGCLIENT_EXPORT mg_point_2d *mg_point_2d_make(uint16_t srid, double x_longitude,
double y_latitude);

/// Returns SRID of the 2D point.
MGCLIENT_EXPORT int64_t mg_point_2d_srid(const mg_point_2d *point_2d);

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

/// Creates mg_point_3d from srid, x_longitude, y_latitude and z_height.
/// \return a pointer to mg_point_3d or NULL if an error occured.
MGCLIENT_EXPORT mg_point_3d *mg_point_3d_make(uint16_t srid, double x_longitude,
double y_latitude,
double z_height);

/// Returns SRID of the 3D point.
MGCLIENT_EXPORT int64_t mg_point_3d_srid(const mg_point_3d *point_3d);

Expand Down
15 changes: 13 additions & 2 deletions src/mgsession-encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ int mg_session_write_duration(mg_session *session, const mg_duration *dur) {
return 0;
}

int mg_session_write_point_2d(mg_session *session, const mg_point_2d *point) {
MG_RETURN_IF_FAILED(
mg_session_write_uint8(session, (uint8_t)(MG_MARKER_TINY_STRUCT3)));
MG_RETURN_IF_FAILED(mg_session_write_uint8(session, MG_SIGNATURE_POINT_2D));
MG_RETURN_IF_FAILED(mg_session_write_integer(session, point->srid));
MG_RETURN_IF_FAILED(mg_session_write_float(session, point->x));
MG_RETURN_IF_FAILED(mg_session_write_float(session, point->y));
return 0;
}

int mg_session_write_value(mg_session *session, const mg_value *value) {
switch (value->type) {
case MG_VALUE_TYPE_NULL:
Expand Down Expand Up @@ -219,8 +229,9 @@ int mg_session_write_value(mg_session *session, const mg_value *value) {
case MG_VALUE_TYPE_DURATION:
return mg_session_write_duration(session, value->duration_v);
case MG_VALUE_TYPE_POINT_2D:
mg_session_set_error(session, "tried to send value of type 'point_2d'");
return MG_ERROR_INVALID_VALUE;
return mg_session_write_point_2d(session, value->point_2d_v);
// mg_session_set_error(session, "tried to send value of type
// 'point_2d'"); // TODO(gitbuda): remove return MG_ERROR_INVALID_VALUE;
case MG_VALUE_TYPE_POINT_3D:
mg_session_set_error(session, "tried to send value of type 'point_3d'");
return MG_ERROR_INVALID_VALUE;
Expand Down
26 changes: 26 additions & 0 deletions src/mgvalue.c
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,32 @@ mg_duration *mg_duration_make(int64_t months, int64_t days, int64_t seconds,
return dur;
}

mg_point_2d *mg_point_2d_make(uint16_t srid, double x_longitude,
double y_latitude) {
mg_point_2d *point = mg_point_2d_alloc(&mg_system_allocator);
if (!point) {
return NULL;
}
point->srid = srid;
point->x = x_longitude;
point->y = y_latitude;
return point;
}

mg_point_3d *mg_point_3d_make(uint16_t srid, double x_longitude,
double y_latitude, double z_height) {
mg_point_3d *point = mg_point_3d_alloc(&mg_system_allocator);
if (!point) {
return NULL;
}
point->srid = srid;
point->x = x_longitude;
point->y = y_latitude;
point->z = z_height;
;
return point;
}

int mg_string_equal(const mg_string *lhs, const mg_string *rhs) {
if (lhs->size != rhs->size) {
return 0;
Expand Down

0 comments on commit b365a0f

Please sign in to comment.