From b3bc7090ce6b0d8029d87724e88a7e7e64270510 Mon Sep 17 00:00:00 2001 From: Fredrik Kihlander Date: Thu, 17 May 2018 10:51:47 +0200 Subject: [PATCH] BREAKING CHANGE: Changed TLD-format for enums where values are now placed in under key "values". This to be able to solve issue #59 and issue #61 --- src/dl_typelib_read_txt.cpp | 21 ++++++++++++++---- src/dl_typelib_write_txt.cpp | 7 +++--- tests/dl_tests_typelib.cpp | 43 ++++++++++++++++++++++++++++-------- tests/small.tld | 6 +++-- tests/unittest.tld | 26 +++++++++++++--------- 5 files changed, 75 insertions(+), 28 deletions(-) diff --git a/src/dl_typelib_read_txt.cpp b/src/dl_typelib_read_txt.cpp index d6ce05d..4f0ed82 100644 --- a/src/dl_typelib_read_txt.cpp +++ b/src/dl_typelib_read_txt.cpp @@ -567,19 +567,32 @@ static void dl_context_load_txt_type_library_read_enum_value( dl_ctx_t ctx, dl_t static void dl_context_load_txt_type_library_read_enum( dl_ctx_t ctx, dl_txt_read_ctx* read_state, dl_txt_read_substr* name ) { - dl_txt_read_substr value_name = {0,0}; uint32_t value_start = ctx->enum_value_count; uint32_t alias_start = ctx->enum_alias_count; dl_txt_eat_char( ctx, read_state, '{' ); do { - value_name = dl_txt_eat_and_expect_string( ctx, read_state ); - + dl_txt_read_substr key = dl_txt_eat_and_expect_string( ctx, read_state ); dl_txt_eat_char( ctx, read_state, ':' ); dl_txt_eat_white( read_state ); - dl_context_load_txt_type_library_read_enum_value( ctx, read_state, &value_name ); + if( strncmp( "values", key.str, 6 ) == 0 ) + { + dl_txt_eat_char( ctx, read_state, '{' ); + do + { + dl_txt_read_substr value_name = dl_txt_eat_and_expect_string( ctx, read_state ); + + dl_txt_eat_char( ctx, read_state, ':' ); + dl_txt_eat_white( read_state ); + + dl_context_load_txt_type_library_read_enum_value( ctx, read_state, &value_name ); + } while( dl_txt_try_eat_char( read_state, ',') ); + dl_txt_eat_char( ctx, read_state, '}' ); + } + else + dl_txt_read_failed( ctx, read_state, DL_ERROR_MALFORMED_DATA, "unexpected key '%.*s' in type, valid keys are 'values'", key.len, key.str ); } while( dl_txt_try_eat_char( read_state, ',') ); dl_txt_eat_char( ctx, read_state, '}' ); diff --git a/src/dl_typelib_write_txt.cpp b/src/dl_typelib_write_txt.cpp index 61650e3..181daea 100644 --- a/src/dl_typelib_write_txt.cpp +++ b/src/dl_typelib_write_txt.cpp @@ -54,14 +54,15 @@ static void dl_context_write_txt_enum( dl_ctx_t ctx, dl_binary_writer* writer, d dl_enum_info_t enum_info; dl_reflect_get_enum_info( ctx, tid, &enum_info ); - dl_binary_writer_write_fmt( writer, " \"%s\" : {\n", enum_info.name ); + dl_binary_writer_write_fmt( writer, " \"%s\" : {\n" + " \"values\" : {\n", enum_info.name ); dl_enum_value_info_t* values = (dl_enum_value_info_t*)malloc( enum_info.value_count * sizeof( dl_enum_value_info_t ) ); dl_reflect_get_enum_values( ctx, tid, values, enum_info.value_count ); for( unsigned int j = 0; j < enum_info.value_count; ++j ) { - dl_binary_writer_write_fmt( writer, " \"%s\" : %u", values[j].name, values[j].value ); + dl_binary_writer_write_fmt( writer, " \"%s\" : %u", values[j].name, values[j].value ); if( j < enum_info.value_count - 1 ) dl_binary_writer_write( writer, ",\n", 2 ); else @@ -70,7 +71,7 @@ static void dl_context_write_txt_enum( dl_ctx_t ctx, dl_binary_writer* writer, d free( values ); - dl_binary_writer_write_fmt( writer, " }", 1 ); + dl_binary_writer_write_fmt( writer, " }\n }", 1 ); } static void dl_context_write_txt_enums( dl_ctx_t ctx, dl_binary_writer* writer ) diff --git a/tests/dl_tests_typelib.cpp b/tests/dl_tests_typelib.cpp index 1d38ca9..cc95c59 100644 --- a/tests/dl_tests_typelib.cpp +++ b/tests/dl_tests_typelib.cpp @@ -121,8 +121,8 @@ TEST_F( DLTypeLibTxt, crash1 ) { const char single_member_typelib[] = STRINGIFY({ "enums": { - "anim_spline_type": { "ANIM_SPLINE_TYPE_ONCE": 0, "ANIM_SPLINE_TYPE_PING_PONG": 1 }, - "timer_type": { "LOOP": 1, "ONCE": 0 } + "anim_spline_type": { "values" : { "ANIM_SPLINE_TYPE_ONCE": 0, "ANIM_SPLINE_TYPE_PING_PONG": 1 } }, + "timer_type": { "values" : { "LOOP": 1, "ONCE": 0 } } }, "types": { "a": { "members": [ { "type": "uint8", "name": "a" } ] }, @@ -153,7 +153,7 @@ TEST_F( DLTypeLibTxt, nonexisting_type ) TEST_F( DLTypeLibTxt, empty_types ) { - typelibtxt_expect_error( ctx, DL_ERROR_OK, STRINGIFY({ "enums" : { "e" : { "a" : 1 } }, "types" : {} }) ); + typelibtxt_expect_error( ctx, DL_ERROR_OK, STRINGIFY({ "enums" : { "e" : { "values" : { "a" : 1 } } }, "types" : {} }) ); } TEST_F( DLTypeLibTxt, empty_enums ) @@ -178,8 +178,29 @@ TEST_F( DLTypeLibTxt, invalid_default_value_array ) TEST_F( DLTypeLibTxt, invalid_alias_array ) { - typelibtxt_expect_error( ctx, DL_ERROR_TXT_PARSE_ERROR, STRINGIFY( { "enums" : { "e" : { "v" : { "value" : 1, "aliases" : [ "apa" } } } } } ) ); - typelibtxt_expect_error( ctx, DL_ERROR_MALFORMED_DATA, STRINGIFY( { "enums" : { "e" : { "v" : { "aliases" : [ "apa" ] } } } } ) ); + typelibtxt_expect_error( ctx, DL_ERROR_TXT_PARSE_ERROR, + STRINGIFY( { + "enums" : { + "e" : { + "values" : { + "v" : { "value" : 1, "aliases" : [ "apa" } + } + } + } + }) + ); + + typelibtxt_expect_error( ctx, DL_ERROR_MALFORMED_DATA, + STRINGIFY( { + "enums" : { + "e" : { + "values" : { + "v" : { "aliases" : [ "apa" ] } + } + } + } + } ) + ); } TEST_F( DLTypeLibTxt, invalid_type_fmt_array ) @@ -227,8 +248,12 @@ TEST_F( DLTypeLibUnpackTxt, round_about ) { const char* testlib1 = STRINGIFY({ "enums" : { - "e1" : { "val1" : 1, "val2" : 2 }, - "e2" : { "val3" : 3, "val2" : 4 } + "e1" : { + "values" : { "val1" : 1, "val2" : 2 } + }, + "e2" : { + "values" : { "val3" : 3, "val2" : 4 } + } }, "types" : { "t1" : { @@ -315,8 +340,8 @@ TEST_F( DLTypeLib, enum_in_2_tlds ) // capturing bug where enum-values would not be loaded as expected in binary tld. // alias-offests were not patched correctly. - const char typelib1[] = STRINGIFY({ "module" : "tl1", "enums" : { "e1" : { "e1_v1" : 1, "e1_v2" : 2 } }, "types" : { "tl1_type" : { "members" : [ { "name" : "m1", "type" : "e1" } ] } } }); - const char typelib2[] = STRINGIFY({ "module" : "tl2", "enums" : { "e2" : { "e2_v1" : 3, "e2_v2" : 4 } }, "types" : { "tl2_type" : { "members" : [ { "name" : "m2", "type" : "e2" } ] } } }); + const char typelib1[] = STRINGIFY({ "module" : "tl1", "enums" : { "e1" : { "values" : { "e1_v1" : 1, "e1_v2" : 2 } } }, "types" : { "tl1_type" : { "members" : [ { "name" : "m1", "type" : "e1" } ] } } }); + const char typelib2[] = STRINGIFY({ "module" : "tl2", "enums" : { "e2" : { "values" : { "e2_v1" : 3, "e2_v2" : 4 } } }, "types" : { "tl2_type" : { "members" : [ { "name" : "m2", "type" : "e2" } ] } } }); size_t tl1_size; size_t tl2_size; diff --git a/tests/small.tld b/tests/small.tld index 49c59c7..42b1052 100644 --- a/tests/small.tld +++ b/tests/small.tld @@ -3,8 +3,10 @@ "enums" : { "simple_enum" : { - "SIMPLE_VAL1" : 0, - "SIMPLE_VAL2" : 1 + "values" : { + "SIMPLE_VAL1" : 0, + "SIMPLE_VAL2" : 1 + } } }, diff --git a/tests/unittest.tld b/tests/unittest.tld index 45c3023..3b0c84f 100755 --- a/tests/unittest.tld +++ b/tests/unittest.tld @@ -5,22 +5,28 @@ "enums" : { "TestEnum1" : { - "TESTENUM1_VALUE1" : 0, - "TESTENUM1_VALUE2" : 1, - "TESTENUM1_VALUE3" : 2, - "TESTENUM1_VALUE4" : 3 + "values" : { + "TESTENUM1_VALUE1" : 0, + "TESTENUM1_VALUE2" : 1, + "TESTENUM1_VALUE3" : 2, + "TESTENUM1_VALUE4" : 3 + } }, "TestEnum2" : { - "TESTENUM2_VALUE1" : 7, - "TESTENUM2_VALUE2" : 1337, - "TESTENUM2_VALUE3" : 1338, - "TESTENUM2_VALUE4" : 1337 + "values" : { + "TESTENUM2_VALUE1" : 7, + "TESTENUM2_VALUE2" : 1337, + "TESTENUM2_VALUE3" : 1338, + "TESTENUM2_VALUE4" : 1337 + } }, "multi_alias_enum" : { - "MULTI_ALIAS1" : { "value" : 7, "aliases" : [ "alias1", "alias2", "alias3" ] }, - "MULTI_ALIAS2" : { "value" : 8, "aliases" : [ "alias4" ] } + "values" : { + "MULTI_ALIAS1" : { "value" : 7, "aliases" : [ "alias1", "alias2", "alias3" ] }, + "MULTI_ALIAS2" : { "value" : 8, "aliases" : [ "alias4" ] } + } } },