Skip to content

Commit a42dd24

Browse files
authored
Add support for temporal types in the communication layer of mgclient (#33)
1 parent a3d7a41 commit a42dd24

File tree

6 files changed

+129
-17
lines changed

6 files changed

+129
-17
lines changed

include/mgclient.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,10 @@ MGCLIENT_EXPORT mg_path *mg_path_copy(const mg_path *path);
869869
/// Destroys the given path.
870870
MGCLIENT_EXPORT void mg_path_destroy(mg_path *path);
871871

872+
/// Creates mg_date from days.
873+
/// \return a pointer to mg_date or NULL if an error occurred.
874+
MGCLIENT_EXPORT mg_date *mg_date_make(int64_t days);
875+
872876
/// Returns days since the Unix epoch.
873877
MGCLIENT_EXPORT int64_t mg_date_days(const mg_date *date);
874878

@@ -898,6 +902,10 @@ MGCLIENT_EXPORT void mg_time_destroy(mg_time *time);
898902
MGCLIENT_EXPORT int64_t
899903
mg_local_time_nanoseconds(const mg_local_time *local_time);
900904

905+
/// Creates mg_local_time from nanoseconds.
906+
/// \return a pointer to mg_local_time or NULL if an error occurred.
907+
MGCLIENT_EXPORT mg_local_time *mg_local_time_make(int64_t nanoseconds);
908+
901909
/// Creates a copy of the given local time.
902910
///
903911
/// \return A pointer to the copy or NULL if an error occured.
@@ -947,11 +955,17 @@ MGCLIENT_EXPORT mg_date_time_zone_id *mg_date_time_zone_id_copy(
947955
MGCLIENT_EXPORT void mg_date_time_zone_id_destroy(
948956
mg_date_time_zone_id *date_time_zone_id);
949957

950-
/// Returns seconds since Unix epoch.
958+
/// Creates mg_local_date_time from seconds and nanoseconds.
959+
/// \return a pointer to mg_local_date_time or NULL if an error occurred.
960+
MGCLIENT_EXPORT mg_local_date_time *mg_local_date_time_make(
961+
int64_t seconds, int64_t nanoseconds);
962+
//
963+
/// Returns seconds since Unix epoch. This includes the hours, minutes, seconds
964+
/// fields of the local_time.
951965
MGCLIENT_EXPORT int64_t
952966
mg_local_date_time_seconds(const mg_local_date_time *local_date_time);
953967

954-
/// Returns nanoseconds since midnight.
968+
/// Returns subseconds of the local_time field as nanoseconds.
955969
MGCLIENT_EXPORT int64_t
956970
mg_local_date_time_nanoseconds(const mg_local_date_time *local_date_time);
957971

@@ -965,6 +979,12 @@ MGCLIENT_EXPORT mg_local_date_time *mg_local_date_time_copy(
965979
MGCLIENT_EXPORT void mg_local_date_time_destroy(
966980
mg_local_date_time *local_date_time);
967981

982+
/// Creates mg_duration from months, days, seconds and nanoseconds.
983+
/// \return a pointer to mg_duration or NULL if an error occurred.
984+
MGCLIENT_EXPORT mg_duration *mg_duration_make(int64_t months, int64_t days,
985+
int64_t seconds,
986+
int64_t nanoseconds);
987+
968988
/// Returns the months part of the temporal amount.
969989
MGCLIENT_EXPORT int64_t mg_duration_months(const mg_duration *duration);
970990

src/mgconstants.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ static const uint8_t MG_MARKERS_MAP[] = {MG_MARKER_TINY_MAP, MG_MARKER_MAP_8,
7474
MG_MARKER_MAP_16, MG_MARKER_MAP_32};
7575

7676
#define MG_MARKER_TINY_STRUCT 0xB0
77+
#define MG_MARKER_TINY_STRUCT1 0xB1
78+
#define MG_MARKER_TINY_STRUCT2 0xB2
79+
#define MG_MARKER_TINY_STRUCT3 0xB3
80+
#define MG_MARKER_TINY_STRUCT4 0xB4
81+
#define MG_MARKER_TINY_STRUCT5 0xB5
7782
#define MG_MARKER_STRUCT_8 0xDC
7883
#define MG_MARKER_STRUCT_16 0xDD
7984

src/mgsession-decoder.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "mgclient.h"
16-
#include "mgsession.h"
17-
1815
#include <string.h>
1916

17+
#include "mgclient.h"
2018
#include "mgcommon.h"
2119
#include "mgconstants.h"
2220
#include "mgmessage.h"
21+
#include "mgsession.h"
2322
#include "mgvalue.h"
2423

2524
int mg_session_read_uint8(mg_session *session, uint8_t *val) {

src/mgsession-encoder.c

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "mgsession.h"
16-
1715
#include <stdint.h>
1816
#include <stdlib.h>
1917
#include <string.h>
2018

2119
#include "mgcommon.h"
2220
#include "mgconstants.h"
21+
#include "mgsession.h"
2322
#include "mgvalue.h"
2423

2524
int mg_session_write_uint8(mg_session *session, uint8_t val) {
@@ -131,6 +130,44 @@ int mg_session_write_map(mg_session *session, const mg_map *map) {
131130
return 0;
132131
}
133132

133+
int mg_session_write_date(mg_session *session, const mg_date *date) {
134+
MG_RETURN_IF_FAILED(
135+
mg_session_write_uint8(session, (uint8_t)(MG_MARKER_TINY_STRUCT1)));
136+
MG_RETURN_IF_FAILED(mg_session_write_uint8(session, MG_SIGNATURE_DATE));
137+
MG_RETURN_IF_FAILED(mg_session_write_integer(session, date->days));
138+
return 0;
139+
}
140+
141+
int mg_session_write_local_time(mg_session *session, const mg_local_time *lt) {
142+
MG_RETURN_IF_FAILED(
143+
mg_session_write_uint8(session, (uint8_t)(MG_MARKER_TINY_STRUCT1)));
144+
MG_RETURN_IF_FAILED(mg_session_write_uint8(session, MG_SIGNATURE_LOCAL_TIME));
145+
MG_RETURN_IF_FAILED(mg_session_write_integer(session, lt->nanoseconds));
146+
return 0;
147+
}
148+
149+
int mg_session_write_local_date_time(mg_session *session,
150+
const mg_local_date_time *ldt) {
151+
MG_RETURN_IF_FAILED(
152+
mg_session_write_uint8(session, (uint8_t)(MG_MARKER_TINY_STRUCT2)));
153+
MG_RETURN_IF_FAILED(
154+
mg_session_write_uint8(session, MG_SIGNATURE_LOCAL_DATE_TIME));
155+
MG_RETURN_IF_FAILED(mg_session_write_integer(session, ldt->seconds));
156+
MG_RETURN_IF_FAILED(mg_session_write_integer(session, ldt->nanoseconds));
157+
return 0;
158+
}
159+
160+
int mg_session_write_duration(mg_session *session, const mg_duration *dur) {
161+
MG_RETURN_IF_FAILED(
162+
mg_session_write_uint8(session, (uint8_t)(MG_MARKER_TINY_STRUCT4)));
163+
MG_RETURN_IF_FAILED(mg_session_write_uint8(session, MG_SIGNATURE_DURATION));
164+
MG_RETURN_IF_FAILED(mg_session_write_integer(session, dur->months));
165+
MG_RETURN_IF_FAILED(mg_session_write_integer(session, dur->days));
166+
MG_RETURN_IF_FAILED(mg_session_write_integer(session, dur->seconds));
167+
MG_RETURN_IF_FAILED(mg_session_write_integer(session, dur->nanoseconds));
168+
return 0;
169+
}
170+
134171
int mg_session_write_value(mg_session *session, const mg_value *value) {
135172
switch (value->type) {
136173
case MG_VALUE_TYPE_NULL:
@@ -163,14 +200,12 @@ int mg_session_write_value(mg_session *session, const mg_value *value) {
163200
mg_session_set_error(session, "tried to send value of type 'path'");
164201
return MG_ERROR_INVALID_VALUE;
165202
case MG_VALUE_TYPE_DATE:
166-
mg_session_set_error(session, "tried to send value of type 'date'");
167-
return MG_ERROR_INVALID_VALUE;
203+
return mg_session_write_date(session, value->date_v);
168204
case MG_VALUE_TYPE_TIME:
169205
mg_session_set_error(session, "tried to send value of type 'time'");
170206
return MG_ERROR_INVALID_VALUE;
171207
case MG_VALUE_TYPE_LOCAL_TIME:
172-
mg_session_set_error(session, "tried to send value of type 'local_time'");
173-
return MG_ERROR_INVALID_VALUE;
208+
return mg_session_write_local_time(session, value->local_time_v);
174209
case MG_VALUE_TYPE_DATE_TIME:
175210
mg_session_set_error(session, "tried to send value of type 'date_time'");
176211
return MG_ERROR_INVALID_VALUE;
@@ -179,12 +214,10 @@ int mg_session_write_value(mg_session *session, const mg_value *value) {
179214
"tried to send value of type 'date_time_zone_id'");
180215
return MG_ERROR_INVALID_VALUE;
181216
case MG_VALUE_TYPE_LOCAL_DATE_TIME:
182-
mg_session_set_error(session,
183-
"tried to send value of type 'local_date_time'");
184-
return MG_ERROR_INVALID_VALUE;
217+
return mg_session_write_local_date_time(session,
218+
value->local_date_time_v);
185219
case MG_VALUE_TYPE_DURATION:
186-
mg_session_set_error(session, "tried to send value of type 'duration'");
187-
return MG_ERROR_INVALID_VALUE;
220+
return mg_session_write_duration(session, value->duration_v);
188221
case MG_VALUE_TYPE_POINT_2D:
189222
mg_session_set_error(session, "tried to send value of type 'point_2d'");
190223
return MG_ERROR_INVALID_VALUE;

src/mgvalue.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include "mgvalue.h"
16+
1517
#include <stdlib.h>
1618
#include <string.h>
1719

1820
#include "mgallocator.h"
1921
#include "mgclient.h"
2022
#include "mgconstants.h"
21-
#include "mgvalue.h"
2223

2324
mg_string *mg_string_alloc(uint32_t size, mg_allocator *allocator) {
2425
char *block = mg_allocator_malloc(allocator, sizeof(mg_string) + size);
@@ -1639,6 +1640,48 @@ mg_path *mg_path_make(uint32_t node_count, mg_node **nodes,
16391640
return path;
16401641
}
16411642

1643+
mg_date *mg_date_make(int64_t days) {
1644+
mg_date *date = mg_date_alloc(&mg_system_allocator);
1645+
if (!date) {
1646+
return NULL;
1647+
}
1648+
date->days = days;
1649+
return date;
1650+
}
1651+
1652+
mg_local_time *mg_local_time_make(int64_t nanoseconds) {
1653+
mg_local_time *lt = mg_local_time_alloc(&mg_system_allocator);
1654+
if (!lt) {
1655+
return NULL;
1656+
}
1657+
lt->nanoseconds = nanoseconds;
1658+
return lt;
1659+
}
1660+
1661+
mg_local_date_time *mg_local_date_time_make(int64_t seconds,
1662+
int64_t nanoseconds) {
1663+
mg_local_date_time *ldt = mg_local_date_time_alloc(&mg_system_allocator);
1664+
if (!ldt) {
1665+
return NULL;
1666+
}
1667+
ldt->seconds = seconds;
1668+
ldt->nanoseconds = nanoseconds;
1669+
return ldt;
1670+
}
1671+
1672+
mg_duration *mg_duration_make(int64_t months, int64_t days, int64_t seconds,
1673+
int64_t nanoseconds) {
1674+
mg_duration *dur = mg_duration_alloc(&mg_system_allocator);
1675+
if (!dur) {
1676+
return NULL;
1677+
}
1678+
dur->months = months;
1679+
dur->days = days;
1680+
dur->seconds = seconds;
1681+
dur->nanoseconds = nanoseconds;
1682+
return dur;
1683+
}
1684+
16421685
int mg_string_equal(const mg_string *lhs, const mg_string *rhs) {
16431686
if (lhs->size != rhs->size) {
16441687
return 0;

tests/encoder.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,16 @@ INSTANTIATE_TEST_CASE_P(String, ValueTest,
289289
INSTANTIATE_TEST_CASE_P(List, ValueTest,
290290
::testing::ValuesIn(ListTestCases()), );
291291

292+
INSTANTIATE_TEST_CASE_P(Date, ValueTest,
293+
::testing::ValuesIn(DateTestCases()), );
294+
295+
INSTANTIATE_TEST_CASE_P(LocalTime, ValueTest,
296+
::testing::ValuesIn(LocalTimeTestCases()), );
297+
298+
INSTANTIATE_TEST_CASE_P(LocalDateTime, ValueTest,
299+
::testing::ValuesIn(LocalDateTimeTestCases()), );
300+
301+
INSTANTIATE_TEST_CASE_P(Duration, ValueTest,
302+
::testing::ValuesIn((DurationTestCases())), );
303+
292304
INSTANTIATE_TEST_CASE_P(Map, ValueTest, ::testing::ValuesIn(MapTestCases()), );

0 commit comments

Comments
 (0)