Giter Club home page Giter Club logo

Comments (3)

wukan1986 avatar wukan1986 commented on July 17, 2024

same problem with expect api

mpack_reader_t reader;

mpack_expect_map_match(&reader, 5);

mpack_expect_cstr_match(&reader, "A");
char buf1[128] = { 0 };
mpack_expect_utf8_cstr(&reader, buf1, sizeof(buf1));

mpack_expect_cstr_match(&reader, "B");
char buf2[128] = { 0 };
mpack_expect_utf8_cstr(&reader, buf2, sizeof(buf2));

// ......

mpack_done_map(&reader);

from mpack.

ludocode avatar ludocode commented on July 17, 2024

This behaviour may be a bit counter-intuitive in your example but it is intentional. When you call mpack_node_strlen() on a node, that node is expected to be a string. If it isn't, it means the data must be corrupt: the whole tree is placed in an error state and all subsequent calls on the tree return safe nil/zero values. This guarantees that the data matches the hardcoded schema you are expecting and your app won't crash if it doesn't. If you check the tree for errors after parsing you would get mpack_error_type because there was a nil node where a str was expected. The same happens with mpack_expect_utf8_cstr().

If you want to avoid flagging an error, call mpack_node_is_nil() first.

	for (size_t i = 0; i < 5; ++i)
	{
		char key[] = "A";
		key[0] += i;
		auto node_1 = mpack_node_map_cstr(root, key);
		if (mpack_node_is_nil(node_1)) {
			std::cout << "NIL" << std::endl;
		} else {
			auto len_1 = mpack_node_strlen(node_1);
			auto str_1 = mpack_node_str(node_1);
			std::cout << std::string(str_1, len_1) << std::endl;
		}
	}

This prints:

11
22
NIL
44
55

from mpack.

wukan1986 avatar wukan1986 commented on July 17, 2024

I got it
Thank you very mush

from mpack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.