Skip to content

Commit

Permalink
BREAKING CHANGE: Changed TLD-format for enums where values are now pl…
Browse files Browse the repository at this point in the history
…aced in under key "values". This to be able to solve issue #59 and issue #61
  • Loading branch information
wc-duck committed May 17, 2018
1 parent 9dec203 commit b3bc709
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 28 deletions.
21 changes: 17 additions & 4 deletions src/dl_typelib_read_txt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, '}' );
Expand Down
7 changes: 4 additions & 3 deletions src/dl_typelib_write_txt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 )
Expand Down
43 changes: 34 additions & 9 deletions tests/dl_tests_typelib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" } ] },
Expand Down Expand Up @@ -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 )
Expand All @@ -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 )
Expand Down Expand Up @@ -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" : {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions tests/small.tld
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

"enums" : {
"simple_enum" : {
"SIMPLE_VAL1" : 0,
"SIMPLE_VAL2" : 1
"values" : {
"SIMPLE_VAL1" : 0,
"SIMPLE_VAL2" : 1
}
}
},

Expand Down
26 changes: 16 additions & 10 deletions tests/unittest.tld
Original file line number Diff line number Diff line change
Expand Up @@ -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" ] }
}
}
},

Expand Down

0 comments on commit b3bc709

Please sign in to comment.