From f6c73ca7279c2073d848468064774426eda32441 Mon Sep 17 00:00:00 2001 From: totoro Date: Sat, 1 Oct 2016 22:35:05 +0200 Subject: [PATCH] add to_JSON function for convert the HK_Database into JSON format --- types/model/app.anubis | 17 ++++++++++++++++- types/model/columns.anubis | 37 ++++++++++++++++++++++++++++++++++++- types/model/database.anubis | 32 +++++++++++++++++++++++++++++--- types/model/display.anubis | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- types/model/fields.anubis | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- types/model/help.anubis | 20 ++++++++++++++++++++ types/model/icons.anubis | 21 ++++++++++++++++++++- types/model/model.anubis | 13 ++++++++++++- types/model/tables.anubis | 14 ++++++++++++++ 9 files changed, 279 insertions(+), 9 deletions(-) diff --git a/types/model/app.anubis b/types/model/app.anubis index 7fb61c5..832075b 100644 --- a/types/model/app.anubis +++ b/types/model/app.anubis @@ -12,7 +12,8 @@ transmit icons.anubis read tools/basis.anubis transmit system/muscle.anubis - +transmit calexium_lib/web/CXM_json.anubis + public type HK_App: hk_app( String app_name, @@ -81,6 +82,20 @@ public define Message hk_app_message . +public define JsonValue + to_JSON + ( + HK_App app + )= + json_object("_HK_APP", + [ + json_member("name", json_string(app.app_name)), + json_member("icon", to_JSON(app.icon)), + json_member("help", to_JSON(app.help)), + json_member("tables", json_array(map((HK_Table table) |-> to_JSON(table), app.hk_tables))) + ]). + + public define Maybe(HK_App) from_Message ( diff --git a/types/model/columns.anubis b/types/model/columns.anubis index c1dc12b..d290799 100644 --- a/types/model/columns.anubis +++ b/types/model/columns.anubis @@ -10,6 +10,7 @@ read tools/basis.anubis transmit help.anubis transmit fields.anubis +transmit calexium_lib/web/CXM_json.anubis public type HK_Model_Attr: unique, // by default, the values in a column are not required to be all different @@ -35,6 +36,27 @@ public define Message hk_model_attr_message . +public define JsonValue + to_JSON + ( + HK_Model_Attr hk_model_attr + )= + with members = + if hk_model_attr is + { + unique then [ json_member("hk_column_attr", json_string("unique")) ], + + indexed then [ json_member("hk_column_attr", json_string("indexed")) ], + + default(value) then [ json_member("hk_column_attr", json_string("default")), + json_member("value", json_string(value)) + ], + + not_null then [ json_member("hk_column_attr", json_string("not_null")) ] + }, + + json_object("_HK_COLUMN_ATTRIBUTES", members). + public define Maybe(HK_Model_Attr) from_Message ( @@ -113,7 +135,20 @@ public define Message //print_to_stream(hk_model_column); hk_model_column . - + +public define JsonValue + to_JSON + ( + HK_Model_Column hk_column + )= + json_object("_HK_COLUMN", + [ + json_member("name", json_string(hk_column.name)), + json_member("field_type", to_JSON(hk_column.type)), + json_member("attributes", json_array(map((HK_Model_Attr attribute) |-> to_JSON(attribute), hk_column.attributes))), + json_member("text", to_JSON(hk_column.help)) + ]). + public define Maybe(HK_Model_Column) from_Message diff --git a/types/model/database.anubis b/types/model/database.anubis index e0ca04f..4c40d4d 100644 --- a/types/model/database.anubis +++ b/types/model/database.anubis @@ -11,6 +11,7 @@ transmit app.anubis transmit tools/basis.anubis transmit system/muscle.anubis transmit calexium_lib/CXM_message_constants.anubis +transmit calexium_lib/web/CXM_json.anubis public type HK_Database: @@ -20,7 +21,7 @@ public type HK_Database: _HK_DATABASE message format: - ========================= + ============================ +-----------+---------------+-------------------------- | name | type | description @@ -40,8 +41,33 @@ public define Message //print_to_stream(hk_db_message); hk_db_message . - - + + _HK_DATABASE JSON format: + ========================= + + { + "obj_type": "_HK_DATABASE", //_HK_DATABASE object type describe a database + "name": "XXXX", //name of database + "apps": //list of applications + [ + { "obj_type": "_HK_APP", //_HK_APP object type describe an application + "name" : "app_name" //name of the current application + }, + ... //anothers applications + ] + } + +public define JsonValue + to_JSON + ( + HK_Database hk_db + )= + json_object( + [ json_member("obj_type", json_string("_HK_DATABASE")), + json_member("name", json_string(hk_db.name)), + json_member("apps", json_array(map((HK_App app) |-> to_JSON(app), hk_db.apps))) + ]). + public define Maybe(HK_Database) from_Message ( diff --git a/types/model/display.anubis b/types/model/display.anubis index d4f9557..a640aa3 100644 --- a/types/model/display.anubis +++ b/types/model/display.anubis @@ -9,6 +9,7 @@ transmit system/muscle.anubis transmit tools/basis.anubis transmit calexium_lib/CXM_message_constants.anubis +transmit calexium_lib/web/CXM_json.anubis public type HK_List_View: list_view_all, @@ -39,6 +40,28 @@ public define Message }; _msg . + + +public define JsonValue + to_JSON + ( + HK_List_View list_view + )= + with members = + if list_view is + { + list_view_all then + [ json_member("type", json_string("list_view_all")) ], + + list_view(name, columns, link_id_action) then + [ json_member("type", json_string("list_view")), + json_member("view_name", json_string(name)), + json_member("link_id_action", json_string(link_id_action)), + json_member("columns", json_array(map((String column) |-> json_string(column), columns))) + ] + }, + + json_object("_HK_LIST_VIEW", members). public define Maybe(HK_List_View) from_Message @@ -73,13 +96,33 @@ public define Message { edit_view_all then forget(add_string(_msg, "type", "edit_view_all")), edit_view(name, columns) then - forget(add_string(_msg, "type", "list_view")); + forget(add_string(_msg, "type", "edit_view")); forget(add_string(_msg, "view_name", name)); map_forget((String column) |-> forget(add_string(_msg, "columns", column)), columns) }; _msg . +public define JsonValue + to_JSON + ( + HK_Edit_View edit_view + )= + with members = + if edit_view is + { + edit_view_all then + [ json_member("type", json_string("edit_view_all")) ], + + edit_view(name, columns) then + [ json_member("type", json_string("edit_view")), + json_member("view_name", json_string(name)), + json_member("columns", json_array(map((String column) |-> json_string(column), columns))) + ] + }, + + json_object("_HK_EDIT_VIEW", members). + public define Maybe(HK_Edit_View) from_Message ( @@ -127,6 +170,18 @@ public define Message // map_forget((String _str) |-> add_string(hk_display_msg, "edits", _str), list_edit); hk_display_msg . + +public define JsonValue + to_JSON + ( + HK_Display display + )= + since display is hk_display(list_views, list_edits), + json_object("_HK_MODEL_DISPLAY", + [ json_member("list_views", json_array(map((HK_List_View _view) |-> to_JSON(_view), list_views))), + json_member("list_edits", json_array(map((HK_Edit_View _edit) |-> to_JSON(_edit), list_edits))) + ] + ). public define Maybe(HK_Display) get_hk_display diff --git a/types/model/fields.anubis b/types/model/fields.anubis index 04f980d..a005706 100644 --- a/types/model/fields.anubis +++ b/types/model/fields.anubis @@ -10,7 +10,7 @@ transmit tools/basis.anubis transmit system/convert.anubis transmit system/muscle.anubis transmit calexium_lib/CXM_message_constants.anubis - +transmit calexium_lib/web/CXM_json.anubis *** 'HK_Model_Field' (data types of table columns). @@ -51,6 +51,25 @@ public define Message hk_app_name_message . +public define JsonValue + to_JSON + ( + HK_App_Name hk_app_name + )= + with members = + if hk_app_name is + { + this then [ json_member("type", json_string("this")) ], + + app_name(name) then [ json_member("type", json_string("app_name")), + json_member("name", json_string(name)) + ], + + none then [ json_member("type", json_string("none")) ], + }, + + json_object("_HK_APP_NAME", members). + public define Maybe(HK_App_Name) from_Message ( @@ -236,6 +255,62 @@ public define Message hk_model_field_message . +public define JsonValue + to_JSON + ( + HK_Model_Field hk_model_field + )= + with members = + if hk_model_field is + { + p_key then + [ json_member("type", json_string("p_key")) ], + + boolean_field then + [ json_member("type", json_string("boolean_field")) ], + + date_field(attr) then + [ json_member("type", json_string("date_field")), + json_member("dt_attribute", json_string(to_String(attr))) + ], + + time_field(attr) then + [ json_member("type", json_string("time_field")), + json_member("dt_attribute", json_string(to_String(attr))) + ], + + datetime_field(attr) then + [ json_member("type", json_string("datetime_field")), + json_member("dt_attribute", json_string(to_String(attr))) + ], + + foreign_key(app_name, table_name) then // foreign key to 'id' in another (or same) table and column_name for choice selector. + [ json_member("type", json_string("foreign_key")), + json_member("app_name", to_JSON(app_name)), + json_member("table_name", json_string(table_name)), + ], + + integer_field(choices) then // integer of arbitrary size + //TODO add choices if need + [ json_member("type", json_string("integer_field")) ], + + char_field (choices, size) then // text of maximal size 'size' (number of characters) + //TODO add choices if need + [ json_member("type", json_string("char_field")), + json_member("size", json_int(size)) + ], + + password_field(size) then + [ json_member("type", json_string("password_field")), + json_member("size", json_int(size)) + ], + + text_field then // text of variable size + [ json_member("type", json_string("text_field"))] + }, + + json_object("_HK_FIELD", members). + define Maybe(HK_Model_Field) get_field ( diff --git a/types/model/help.anubis b/types/model/help.anubis index 77ac8ac..fc85cb8 100644 --- a/types/model/help.anubis +++ b/types/model/help.anubis @@ -9,6 +9,7 @@ transmit tools/basis.anubis transmit system/muscle.anubis transmit calexium_lib/CXM_message_constants.anubis +transmit calexium_lib/web/CXM_json.anubis public type HK_Help_Text: no_help_text, //no help text available @@ -65,6 +66,25 @@ public define Message }; hk_help_message . + +public define JsonValue + to_JSON + ( + HK_Help_Text hk_help + )= + with members = + if hk_help is + { + no_help_text then [ json_member("hk_help_text", json_string("no_help")) + ], + help_text_TAG(tag) then [ json_member("hk_help_text", json_string("TAG")), + json_member("value", json_string(tag)) + ] + help_text(value) then [ json_member("hk_help_text", json_string("text")), + json_member("value", json_string(value)) + ] + }, + json_object("_HK_HELP_TEXT", members). public define Maybe(HK_Help_Text) from_Message diff --git a/types/model/icons.anubis b/types/model/icons.anubis index 43e0fa7..14591bc 100644 --- a/types/model/icons.anubis +++ b/types/model/icons.anubis @@ -9,6 +9,7 @@ transmit tools/basis.anubis transmit system/muscle.anubis transmit calexium_lib/CXM_message_constants.anubis +transmit calexium_lib/web/CXM_json.anubis public type HK_Icon: no_icon, @@ -40,7 +41,7 @@ public define String icon16(icn_str) then "\""+icn_str+"_icon\"" }. - * * + /* * _HK_ICON message format: ======================= +-----------+-----------+-------------------------------- @@ -73,7 +74,25 @@ public define Message forget(add_string(hk_icon_message, "icn_str", icn_str)); hk_icon_message }. + +public define JsonValue + to_JSON + ( + HK_Icon hk_icon + )= + with members = + if hk_icon is + { + no_icon then [ json_member("hk_icon", json_string("no_icon")) + ], + + icon16(icn_str) then [ json_member("hk_icon", json_string("icon_16")), + json_member("icn_str", json_string(icn_str)) + ] + }, + json_object("_HK_ICON", members). + public define Maybe(HK_Icon) from_Message ( diff --git a/types/model/model.anubis b/types/model/model.anubis index 2ec5f0f..1e75d1d 100644 --- a/types/model/model.anubis +++ b/types/model/model.anubis @@ -8,6 +8,7 @@ transmit tools/basis.anubis transmit columns.anubis +transmit calexium_lib/web/CXM_json.anubis public type HK_Model: hk_model( List(HK_Model_Column) columns, // columns other than the primary key column (if any) @@ -37,7 +38,17 @@ public define Message hk_model_message . - +public define JsonValue + to_JSON + ( + HK_Model hk_model + )= + json_object("_HK_MODEL", + [ + json_member("columns", json_array(map((HK_Model_Column column) |-> to_JSON(column), hk_model.columns))), + json_member("show_str", json_string(hk_model.show_string)) + ]). + public define Maybe(HK_Model) from_Message ( diff --git a/types/model/tables.anubis b/types/model/tables.anubis index dd67b5b..69d79db 100644 --- a/types/model/tables.anubis +++ b/types/model/tables.anubis @@ -9,6 +9,7 @@ read tools/basis.anubis transmit display.anubis transmit model.anubis +transmit calexium_lib/web/CXM_json.anubis public type HK_Table: hk_table ( String name, //name of the table @@ -49,6 +50,19 @@ public define Message forget(add_message(hk_table_message, "hk_display", to_Message(table.display))); //print_to_stream(hk_table_message); hk_table_message. + +public define JsonValue + to_JSON + ( + HK_Table table + )= + json_object("_HK_TABLE", + [ json_member("name", json_string(table.name)), + json_member("short_name", json_string(table.name)), + json_member("model", to_JSON(table.model)), + json_member("hk_display", to_JSON(table.display)) + ] + ). public define Maybe(HK_Table) from_Message -- libgit2 0.21.4