Skip to content

Commit

Permalink
Add make and encoder functions for Point2&3D (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitbuda authored Aug 24, 2024
1 parent 60d05ee commit 316e0cc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
15 changes: 14 additions & 1 deletion examples/advanced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ std::string MgValueToString(const mg::ConstValue &value) {
value_str = std::to_string(value.ValueBool());
} else if (value.type() == mg::Value::Type::Double) {
value_str = std::to_string(value.ValueDouble());
} else if (value.type() == mg::Value::Type::Point2d) {
auto point2d = value.ValuePoint2d();
value_str += "Point2D({ srid:" + std::to_string(point2d.srid()) +
", x:" + std::to_string(point2d.x()) +
", y:" + std::to_string(point2d.y()) + " })";
} else if (value.type() == mg::Value::Type::Point3d) {
auto point3d = value.ValuePoint3d();
value_str += "Point3D({ srid:" + std::to_string(point3d.srid()) +
", x:" + std::to_string(point3d.x()) +
", y:" + std::to_string(point3d.y()) +
", z:" + std::to_string(point3d.z()) + " })";
} else if (value.type() == mg::Value::Type::List) {
value_str += "[";
for (auto item : value.ValueList()) {
Expand Down Expand Up @@ -63,7 +74,9 @@ int main(int argc, char *argv[]) {

if (!client->Execute(
"CREATE (:Person:Entrepreneur {id: 0, age: 40, name: 'John', "
"isStudent: false, score: 5.0});")) {
"isStudent: false, score: 5.0, "
"position2D: point({x: 1, y: 2, srid: 4326}), "
"position3D: point({x: 8, y: 9, z: 10, srid: 9757}) });")) {
std::cerr << "Failed to add data." << std::endl;
return 1;
}
Expand Down
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
27 changes: 23 additions & 4 deletions src/mgsession-encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,27 @@ 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_point_3d(mg_session *session, const mg_point_3d *point) {
MG_RETURN_IF_FAILED(
mg_session_write_uint8(session, (uint8_t)(MG_MARKER_TINY_STRUCT4)));
MG_RETURN_IF_FAILED(mg_session_write_uint8(session, MG_SIGNATURE_POINT_3D));
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));
MG_RETURN_IF_FAILED(mg_session_write_float(session, point->z));
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,11 +240,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);
case MG_VALUE_TYPE_POINT_3D:
mg_session_set_error(session, "tried to send value of type 'point_3d'");
return MG_ERROR_INVALID_VALUE;
return mg_session_write_point_3d(session, value->point_3d_v);
case MG_VALUE_TYPE_UNKNOWN:
mg_session_set_error(session, "tried to send value of unknown type");
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 @@ -1681,6 +1681,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 316e0cc

Please sign in to comment.