MCPcopy Create free account
hub / github.com/bloomberg/comdb2 / db_json_to_table

Function db_json_to_table

lua/sp.c:4028–4068  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4026
4027static int cson_to_table_annotated(Lua, cson_value *, int);
4028static int db_json_to_table(Lua lua)
4029{
4030 int rc;
4031 if (lua_gettop(lua) < 2) {
4032 return luaL_error(lua, "bad arguments to 'json_to_table'");
4033 }
4034 luaL_checkudata(lua, 1, dbtypes.db);
4035 int annotate = 0;
4036 if (lua_gettop(lua) == 3) {
4037 luaL_checktype(lua, 3, LUA_TTABLE);
4038 lua_pushnil(lua);
4039 int processed = 0;
4040 while (lua_next(lua, -2)) {
4041 const char *key = luabb_tostring(lua, -2);
4042 if (strcmp(key, "type_annotate") == 0) {
4043 if (luabb_type(lua, -1) == DBTYPES_LBOOLEAN) {
4044 annotate = lua_toboolean(lua, -2);
4045 processed = 1;
4046 lua_pop(lua, 1);
4047 break;
4048 }
4049 }
4050 }
4051 if (processed == 0) {
4052 return luaL_error(lua, "bad argument #2 to 'json_to_table'");
4053 }
4054 lua_pop(lua, 1);
4055 }
4056 const char *json = luabb_tostring(lua, 2);
4057 cson_value *cson = NULL;
4058 if ((rc = cson_parse_string(&cson, json, strlen(json))) != 0) {
4059 luaL_error(lua, "Parsing JSON rc:%d err:%s", rc, cson_rc_string(rc));
4060 }
4061 lua_newtable(lua);
4062 rc = cson_to_table_annotated(lua, cson, annotate);
4063 cson_free_value(cson);
4064 if (rc != 0) {
4065 luaL_error(lua, "bad argument #1 to json_to_table");
4066 }
4067 return 1;
4068}
4069
4070typedef struct {
4071#define CONV_FLAG_UTF8_FATAL 0x01

Callers

nothing calls this directly

Calls 13

lua_gettopFunction · 0.85
luaL_errorFunction · 0.85
luaL_checkudataFunction · 0.85
luaL_checktypeFunction · 0.85
lua_pushnilFunction · 0.85
lua_nextFunction · 0.85
luabb_tostringFunction · 0.85
luabb_typeFunction · 0.85
lua_tobooleanFunction · 0.85
cson_parse_stringFunction · 0.85
cson_rc_stringFunction · 0.85
cson_to_table_annotatedFunction · 0.85

Tested by

no test coverage detected