Skip to content

Commit

Permalink
Supported all msgpack object types.
Browse files Browse the repository at this point in the history
Fixed cmake warning.
  • Loading branch information
redboltz committed Aug 17, 2024
1 parent 985184b commit 6f24d24
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 43 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ else()
ENDIF ()
endif()

IF ((CMAKE_VERSION VERSION_GREATER 3.27) OR
(CMAKE_VERSION VERSION_EQUAL 3.27))
CMAKE_POLICY(SET CMP0145 OLD)
ENDIF ()

OPTION (MSGPACK_BUILD_TESTS "Build msgpack tests." OFF)
OPTION (MSGPACK_GEN_COVERAGE "Enable running gcov to get a test coverage report." OFF)

Expand Down
32 changes: 31 additions & 1 deletion include/msgpack/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,37 @@ typedef struct msgpack_object_kv {
} msgpack_object_kv;

MSGPACK_DLLEXPORT
bool msgpack_object_init(msgpack_object* d, int type, void* data, size_t size);
void msgpack_object_init_nil(msgpack_object* d);

MSGPACK_DLLEXPORT
void msgpack_object_init_boolean(msgpack_object* d, bool v);

MSGPACK_DLLEXPORT
void msgpack_object_init_unsigned_integer(msgpack_object* d, uint64_t v);

MSGPACK_DLLEXPORT
void msgpack_object_init_signed_integer(msgpack_object* d, int64_t v);

MSGPACK_DLLEXPORT
void msgpack_object_init_float32(msgpack_object* d, float v);

MSGPACK_DLLEXPORT
void msgpack_object_init_float64(msgpack_object* d, double v);

MSGPACK_DLLEXPORT
void msgpack_object_init_str(msgpack_object* d, const char* data, uint32_t size);

MSGPACK_DLLEXPORT
void msgpack_object_init_bin(msgpack_object* d, const char* data, uint32_t size);

MSGPACK_DLLEXPORT
void msgpack_object_init_ext(msgpack_object* d, int8_t type, const char* data, uint32_t size);

MSGPACK_DLLEXPORT
void msgpack_object_init_array(msgpack_object* d, msgpack_object* data, uint32_t size);

MSGPACK_DLLEXPORT
void msgpack_object_init_map(msgpack_object* d, msgpack_object_kv* data, uint32_t size);

#if !defined(_KERNEL_MODE)
MSGPACK_DLLEXPORT
Expand Down
97 changes: 63 additions & 34 deletions src/objectc.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,41 +125,70 @@ int msgpack_pack_object(msgpack_packer* pk, msgpack_object d)
}
}

bool msgpack_object_init(msgpack_object* d, int type, void* data, size_t size)
{
switch (type) {
case MSGPACK_OBJECT_STR:
{
d->via.str.ptr = (char *)data;
d->via.str.size = size;
break;
}
case MSGPACK_OBJECT_BIN:
{
d->via.bin.ptr = (char *)data;
d->via.bin.size = size;
break;
}
case MSGPACK_OBJECT_ARRAY:
{
d->via.array.ptr = (msgpack_object *)data;
d->via.array.size = size;
break;
}
case MSGPACK_OBJECT_MAP:
{
d->via.map.ptr = (msgpack_object_kv *)data;
d->via.map.size = size;
break;
}
default:
{
// Other types are not supported and need to be initialized manually.
return false;
}
void msgpack_object_init_nil(msgpack_object* d) {
d->type = MSGPACK_OBJECT_NIL;
}

void msgpack_object_init_boolean(msgpack_object* d, bool v) {
d->type = MSGPACK_OBJECT_BOOLEAN;
d->via.boolean = v;
}

void msgpack_object_init_unsigned_integer(msgpack_object* d, uint64_t v) {
d->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
d->via.u64 = v;
}

void msgpack_object_init_signed_integer(msgpack_object* d, int64_t v) {
if (v < 0) {
d->type = MSGPACK_OBJECT_NEGATIVE_INTEGER;
d->via.i64 = v;
}
else {
d->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
d->via.u64 = v;
}
d->type = type;
return true;
}

void msgpack_object_init_float32(msgpack_object* d, float v) {
d->type = MSGPACK_OBJECT_FLOAT32;
d->via.f64 = v;
}

void msgpack_object_init_float64(msgpack_object* d, double v) {
d->type = MSGPACK_OBJECT_FLOAT64;
d->via.f64 = v;
}

void msgpack_object_init_str(msgpack_object* d, const char* data, uint32_t size) {
d->type = MSGPACK_OBJECT_STR;
d->via.str.ptr = data;
d->via.str.size = size;
}

void msgpack_object_init_bin(msgpack_object* d, const char* data, uint32_t size) {
d->type = MSGPACK_OBJECT_BIN;
d->via.bin.ptr = data;
d->via.bin.size = size;
}

void msgpack_object_init_ext(msgpack_object* d, int8_t type, const char* data, uint32_t size) {
d->type = MSGPACK_OBJECT_EXT;
d->via.ext.type = type;
d->via.ext.ptr = data;
d->via.ext.size = size;
}

void msgpack_object_init_array(msgpack_object* d, msgpack_object* data, uint32_t size) {
d->type = MSGPACK_OBJECT_ARRAY;
d->via.array.ptr = data;
d->via.array.size = size;
}

void msgpack_object_init_map(msgpack_object* d, msgpack_object_kv* data, uint32_t size) {
d->type = MSGPACK_OBJECT_MAP;
d->via.map.ptr = data;
d->via.map.size = size;
}

#if !defined(_KERNEL_MODE)
Expand Down
76 changes: 68 additions & 8 deletions test/msgpack_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1612,31 +1612,91 @@ TEST(MSGPACKC, object_bin_print_buffer_overflow) {
EXPECT_STREQ("\"test\"", buffer);
}

TEST(MSGPACKC, init_msgpack_obj_nil) {
msgpack_object obj;
msgpack_object_init_nil(&obj);
EXPECT_EQ(MSGPACK_OBJECT_NIL, obj.type);
}

TEST(MSGPACKC, init_msgpack_obj_boolean) {
msgpack_object obj;
msgpack_object_init_boolean(&obj, true);
EXPECT_EQ(MSGPACK_OBJECT_BOOLEAN, obj.type);
EXPECT_EQ(true, obj.via.boolean);
}

TEST(MSGPACKC, init_msgpack_obj_unsigned_integer) {
msgpack_object obj;
msgpack_object_init_unsigned_integer(&obj, 123);
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type);
EXPECT_EQ(static_cast<uint64_t>(123), obj.via.u64);
}

TEST(MSGPACKC, init_msgpack_obj_signed_integer1) {
msgpack_object obj;
msgpack_object_init_signed_integer(&obj, -123);
EXPECT_EQ(MSGPACK_OBJECT_NEGATIVE_INTEGER, obj.type);
EXPECT_EQ(-123, obj.via.i64);
}

TEST(MSGPACKC, init_msgpack_obj_signed_integer2) {
msgpack_object obj;
msgpack_object_init_signed_integer(&obj, 123);
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type);
EXPECT_EQ(static_cast<uint64_t>(123), obj.via.u64);
}

TEST(MSGPACKC, init_msgpack_obj_float32) {
msgpack_object obj;
float val = 1.23f;
msgpack_object_init_float32(&obj, val);
EXPECT_EQ(MSGPACK_OBJECT_FLOAT32, obj.type);
EXPECT_TRUE(fabs(obj.via.f64 - val) <= kEPS);
}

TEST(MSGPACKC, init_msgpack_obj_float64) {
msgpack_object obj;
double val = 1.23;
msgpack_object_init_float64(&obj, val);
EXPECT_EQ(MSGPACK_OBJECT_FLOAT64, obj.type);
EXPECT_TRUE(fabs(obj.via.f64 - val) <= kEPS);
}


TEST(MSGPACKC, init_msgpack_obj_string) {
msgpack_object obj;
char buffer[] = "test";
msgpack_object_init(&obj, MSGPACK_OBJECT_STR, (void *)buffer, strlen(buffer));
msgpack_object_init_str(&obj, buffer, (uint32_t)strlen(buffer));
EXPECT_EQ(MSGPACK_OBJECT_STR, obj.type);
EXPECT_STREQ(buffer, obj.via.str.ptr);
}

TEST(MSGPACKC, init_msgpack_obj_bin) {
msgpack_object obj;
char buffer[] = "test";
msgpack_object_init(&obj, MSGPACK_OBJECT_BIN, (void *)buffer, strlen(buffer));
msgpack_object_init_bin(&obj, buffer, (uint32_t)strlen(buffer));
EXPECT_EQ(MSGPACK_OBJECT_BIN, obj.type);
EXPECT_STREQ(buffer, obj.via.bin.ptr);
}

TEST(MSGPACKC, init_msgpack_obj_ext) {
msgpack_object obj;
char buffer[] = "test";
msgpack_object_init_ext(&obj, 1, buffer, (uint32_t)strlen(buffer));
EXPECT_EQ(MSGPACK_OBJECT_EXT, obj.type);
EXPECT_EQ(1, obj.via.ext.type);
EXPECT_STREQ(buffer, obj.via.ext.ptr);
}

TEST(MSGPACKC, init_msgpack_obj_array) {
msgpack_object obj;
char buffer[][7] = {"test_1", "test_2", "test_3", "test_4"};
size_t buffer_size = 4;
uint32_t buffer_size = 4;
msgpack_object array[buffer_size];
for(size_t i = 0; i < buffer_size; i++) {
msgpack_object_init(&array[i], MSGPACK_OBJECT_STR, (void *)buffer[i], strlen(buffer[i]));
msgpack_object_init_str(&array[i], buffer[i], (uint32_t)strlen(buffer[i]));
}
msgpack_object_init(&obj, MSGPACK_OBJECT_ARRAY, (void *)array, buffer_size);
msgpack_object_init_array(&obj, array, buffer_size);
EXPECT_EQ(MSGPACK_OBJECT_ARRAY, obj.type);
for(size_t i = 0; i < buffer_size; i++) {
EXPECT_STREQ(buffer[i], obj.via.array.ptr[i].via.str.ptr);
Expand All @@ -1648,10 +1708,10 @@ TEST(MSGPACKC, init_msgpack_obj_map) {
char key_str[] = "test_key";
char value_str[] = "test_value";
msgpack_object key,value;
msgpack_object_init(&key, MSGPACK_OBJECT_STR, (void *)key_str, strlen(key_str));
msgpack_object_init(&value, MSGPACK_OBJECT_STR, (void *)value_str, strlen(value_str));
msgpack_object_init_str(&key, key_str, (uint32_t)strlen(key_str));
msgpack_object_init_str(&value, value_str, (uint32_t)strlen(value_str));
msgpack_object_kv map = { key, value };
msgpack_object_init(&obj, MSGPACK_OBJECT_MAP, (void *)&map, 1);
msgpack_object_init_map(&obj, &map, 1);
EXPECT_EQ(MSGPACK_OBJECT_MAP, obj.type);
EXPECT_STREQ(key_str, obj.via.map.ptr->key.via.str.ptr);
EXPECT_STREQ(value_str, obj.via.map.ptr->val.via.str.ptr);
Expand Down

0 comments on commit 6f24d24

Please sign in to comment.