Browse code

app_lua: add support for ARRAY and DICT types

Victor Seva authored on 13/04/2020 15:10:01
Showing 1 changed files
... ...
@@ -806,6 +806,85 @@ int sr_kemi_lua_return_int(lua_State* L, sr_kemi_t *ket, int rc)
806 806
 	return app_lua_return_false(L);
807 807
 }
808 808
 
809
+void sr_kemi_lua_push_dict_item(lua_State *L, sr_kemi_dict_item_t *item);
810
+
811
+/**
812
+ * creates and push a table to the lua stack with
813
+ * the elements of the list
814
+ */
815
+void sr_kemi_lua_push_array(lua_State *L, sr_kemi_dict_item_t *item) {
816
+	int i = 1;
817
+	sr_kemi_dict_item_t *k;
818
+	if(!item) {
819
+		LM_CRIT("BUG: dict field empty\n");
820
+		return;
821
+	}
822
+	if (item->vtype == SR_KEMIP_ARRAY) {
823
+		k = item->v.dict;
824
+	} else {
825
+		k = item;
826
+	}
827
+	if(k) {
828
+		lua_newtable(L);
829
+	}
830
+	while(k){
831
+		lua_pushnumber(L, i++);
832
+		sr_kemi_lua_push_dict_item(L, k);
833
+		lua_settable(L, -3);
834
+		k = k->next;
835
+	}
836
+}
837
+
838
+void sr_kemi_lua_push_dict(lua_State *L, sr_kemi_dict_item_t *item) {
839
+	sr_kemi_dict_item_t *k = item;
840
+	if(!item) {
841
+		LM_CRIT("BUG: dict field empty\n");
842
+		return;
843
+	}
844
+	lua_newtable(L);
845
+	while(k){
846
+		sr_kemi_lua_push_dict_item(L, k->v.dict);
847
+		lua_setfield(L, -2, k->name.s);
848
+		k = k->next;
849
+	}
850
+}
851
+
852
+void
853
+sr_kemi_lua_push_dict_item(lua_State *L, sr_kemi_dict_item_t *item)
854
+{
855
+	switch(item->vtype) {
856
+		case SR_KEMIP_NONE:
857
+			LM_CRIT("BUG: vtype is NONE\n");
858
+			lua_pushnil(L);
859
+		break;
860
+		case SR_KEMIP_INT:
861
+			lua_pushinteger(L, item->v.n);
862
+		break;
863
+		case SR_KEMIP_STR:
864
+			lua_pushlstring(L, item->v.s.s, item->v.s.len);
865
+		break;
866
+		case SR_KEMIP_BOOL:
867
+			if(item->v.n!=SR_KEMI_FALSE) {
868
+				lua_pushboolean(L, SRLUA_TRUE);
869
+			} else {
870
+				lua_pushboolean(L, SRLUA_FALSE);
871
+			}
872
+		break;
873
+		case SR_KEMIP_NULL:
874
+			lua_pushnil(L);
875
+		break;
876
+		case SR_KEMIP_ARRAY:
877
+			sr_kemi_lua_push_array(L, item);
878
+		break;
879
+		case SR_KEMIP_DICT:
880
+			sr_kemi_lua_push_dict(L, item);
881
+		break;
882
+		default:
883
+			LM_DBG("unknown type:%d\n", item->vtype);
884
+			/* unknown type - return false */
885
+			lua_pushboolean(L, SRLUA_FALSE);
886
+	}
887
+}
809 888
 
810 889
 /**
811 890
  *
... ...
@@ -835,6 +914,14 @@ int sr_kemi_lua_return_xval(lua_State* L, sr_kemi_t *ket, sr_kemi_xval_t *rx)
835 914
 		case SR_KEMIP_NULL:
836 915
 			lua_pushnil(L);
837 916
 			return 1;
917
+		case SR_KEMIP_ARRAY:
918
+			sr_kemi_lua_push_array(L, rx->v.dict);
919
+			sr_kemi_xval_free(rx);
920
+			return 1;
921
+		case SR_KEMIP_DICT:
922
+			sr_kemi_lua_push_dict_item(L, rx->v.dict);
923
+			sr_kemi_xval_free(rx);
924
+			return 1;
838 925
 		default:
839 926
 			/* unknown type - return false */
840 927
 			lua_pushboolean(L, SRLUA_FALSE);