Commit dd951bbb authored by Anton Nayshtut's avatar Anton Nayshtut Committed by Jim Harris
Browse files

lib/json: introduce spdk_json_write_reset



The spdk_json_write_reset API resets the state of an
spdk_json_write_ctx.

It can be used to reset a write operation in progress.

In upcoming patches, it will be used to reset RPC server error responses
when they are sent after partial data has already been written.

Change-Id: I600d547f4280fec37ae7c4374c7f3411bb715249
Signed-off-by: default avatarAnton Nayshtut <anayshtut@nvidia.com>
Reviewed-on: https://review.spdk.io/c/spdk/spdk/+/25985


Reviewed-by: default avatarKonrad Sztyber <ksztyber@nvidia.com>
Reviewed-by: default avatarJim Harris <jim.harris@nvidia.com>
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Tested-by: default avatarSPDK Automated Test System <spdkbot@gmail.com>
Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
Community-CI: Mellanox Build Bot
parent e863d072
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -195,6 +195,7 @@ typedef int (*spdk_json_write_cb)(void *cb_ctx, const void *data, size_t size);
struct spdk_json_write_ctx *spdk_json_write_begin(spdk_json_write_cb write_cb, void *cb_ctx,
		uint32_t flags);
int spdk_json_write_end(struct spdk_json_write_ctx *w);
void spdk_json_write_reset(struct spdk_json_write_ctx *w);
int spdk_json_write_null(struct spdk_json_write_ctx *w);
int spdk_json_write_bool(struct spdk_json_write_ctx *w, bool val);
int spdk_json_write_uint8(struct spdk_json_write_ctx *w, uint8_t val);
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk

SO_VER := 6
SO_MINOR := 0
SO_MINOR := 1

C_SRCS = json_parse.c json_util.c json_write.c
LIBNAME = json
+14 −0
Original line number Diff line number Diff line
@@ -87,6 +87,20 @@ spdk_json_write_end(struct spdk_json_write_ctx *w)
	return failed ? -1 : 0;
}

void
spdk_json_write_reset(struct spdk_json_write_ctx *w)
{
	if (w == NULL) {
		return;
	}

	w->buf_filled = 0;
	w->failed = false;
	w->first_value = true;
	w->new_indent = false;
	w->indent = 0;
}

static inline int
emit(struct spdk_json_write_ctx *w, const void *data, size_t size)
{
+1 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@

	spdk_json_write_begin;
	spdk_json_write_end;
	spdk_json_write_reset;
	spdk_json_write_null;
	spdk_json_write_bool;
	spdk_json_write_uint8;
+35 −1
Original line number Diff line number Diff line
@@ -12,6 +12,8 @@

#include "spdk/util.h"

#define UT_UUID "e524acae-8c26-43e4-882a-461b8690583b"

static uint8_t g_buf[1000];
static uint8_t *g_write_pos;

@@ -56,6 +58,9 @@ write_cb(void *cb_ctx, const void *data, size_t size)
#define END_FAIL() \
	CU_ASSERT(spdk_json_write_end(w) < 0)

#define RESET() \
	spdk_json_write_reset(w)

#define VAL_STRING(str) \
	CU_ASSERT(spdk_json_write_string_raw(w, str, sizeof(str) - 1) == 0)

@@ -555,7 +560,6 @@ test_write_number_double(void)
static void
test_write_uuid(void)
{
#define UT_UUID "e524acae-8c26-43e4-882a-461b8690583b"
	struct spdk_json_write_ctx *w;
	struct spdk_uuid uuid;
	int rc;
@@ -863,6 +867,35 @@ test_write_val(void)
	END("{\"a\":[1,2,3],\"b\":{\"c\":\"d\"},\"e\":true,\"f\":false,\"g\":null}");
}

/* Write reset test */
static void
test_write_reset(void)
{
	struct spdk_json_write_ctx *w;
	struct spdk_uuid uuid;
	int rc;

	rc = spdk_uuid_parse(&uuid, UT_UUID);
	CU_ASSERT_EQUAL(rc, 0);

	BEGIN();
	RESET();
	VAL_INT32(1234);
	END_SIZE("1234", 4);

	BEGIN();
	VAL_STRING("http://www.example.com/image/481989943");
	RESET();
	VAL_INT32(2345);
	END_SIZE("2345", 4);

	BEGIN();
	VAL_UUID(&uuid);
	RESET();
	VAL_INT64(34567890);
	END_SIZE("34567890", 8);
}

static void
test_object_end_fail(void)
{
@@ -900,6 +933,7 @@ main(int argc, char **argv)
	CU_ADD_TEST(suite, test_write_object);
	CU_ADD_TEST(suite, test_write_nesting);
	CU_ADD_TEST(suite, test_write_val);
	CU_ADD_TEST(suite, test_write_reset);
	CU_ADD_TEST(suite, test_object_end_fail);


Loading