Skip to content

Commit 4cc8929

Browse files
committed
1 parent e8ca6c2 commit 4cc8929

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

pb.c

+17-10
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,7 @@ static int Lpb_enum(lua_State *L) {
12311231
}
12321232

12331233
static int lpb_pushdefault(lua_State *L, lpb_State *LS, pb_Field *f, int is_proto3) {
1234+
pb_Type *type = f->type;
12341235
int ret = 0;
12351236
char *end;
12361237
if (f == NULL) return 0;
@@ -1243,12 +1244,17 @@ static int lpb_pushdefault(lua_State *L, lpb_State *LS, pb_Field *f, int is_prot
12431244
ret = 1, lua_pushliteral(L, "");
12441245
break;
12451246
case PB_Tenum:
1246-
if ((f = pb_fname(f->type, f->default_value)) != NULL) {
1247-
if (!LS->enum_as_value)
1247+
if ((f = pb_fname(type, f->default_value)) != NULL) {
1248+
if (LS->enum_as_value)
1249+
ret = 1, lpb_pushinteger(L, f->number, LS->int64_mode);
1250+
else
12481251
ret = 1, lua_pushstring(L, (char*)f->name);
1252+
} else if (is_proto3) {
1253+
if ((f = pb_field(type, 0)) == NULL || LS->enum_as_value)
1254+
ret = 1, lua_pushinteger(L, 0);
12491255
else
1250-
ret = 1, lpb_pushinteger(L, f->number, LS->int64_mode);
1251-
} else if (is_proto3) ret = 1, lua_pushinteger(L, 0);
1256+
ret = 1, lua_pushstring(L, (char*)f->name);
1257+
}
12521258
break;
12531259
case PB_Tmessage:
12541260
return 0;
@@ -1534,6 +1540,13 @@ static void lpbD_field(lpb_Env *e, pb_Field *f, uint32_t tag) {
15341540
pb_Field *ev = NULL;
15351541
uint64_t u64;
15361542

1543+
if (!f->packed && pb_wtypebytype(f->type_id) != (int)pb_gettype(tag))
1544+
luaL_error(L, "type mismatch at offset %d, %s expected for type %s, got %s",
1545+
lpb_offset(s),
1546+
pb_wtypename(pb_wtypebytype(f->type_id), NULL),
1547+
pb_typename(f->type_id, NULL),
1548+
pb_wtypename(pb_gettype(tag), NULL));
1549+
15371550
switch (f->type_id) {
15381551
case PB_Tenum:
15391552
if (pb_readvarint64(&s->base, &u64) == 0)
@@ -1555,12 +1568,6 @@ static void lpbD_field(lpb_Env *e, pb_Field *f, uint32_t tag) {
15551568
break;
15561569

15571570
default:
1558-
if (!f->packed && pb_wtypebytype(f->type_id) != (int)pb_gettype(tag))
1559-
luaL_error(L, "type mismatch at offset %d, %s expected for type %s, got %s",
1560-
lpb_offset(s),
1561-
pb_wtypename(pb_wtypebytype(f->type_id), NULL),
1562-
pb_typename(f->type_id, NULL),
1563-
pb_wtypename(pb_gettype(tag), NULL));
15641571
lpb_readtype(L, e->LS, f->type_id, s);
15651572
}
15661573
}

test.lua

+11
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,18 @@ function _G.test_default()
373373
TestNest nest = 17;
374374
repeated int32 array = 18;
375375
} ]]
376+
377+
table_eq(copy_no_meta(pb.defaults "TestDefault"), {
378+
defaulted_int = 0,
379+
defaulted_bool = false,
380+
defaulted_str = "",
381+
defaulted_num = 0.0,
382+
color = "RED",
383+
bool1 = false,
384+
bool2 = false,
385+
})
376386
pb.option "enum_as_value"
387+
pb.defaults("TestDefault", "clear")
377388
table_eq(copy_no_meta(pb.defaults "TestDefault"), {
378389
defaulted_int = 0,
379390
defaulted_bool = false,

0 commit comments

Comments
 (0)