Commit f6c73ca7279c2073d848468064774426eda32441

Authored by totoro
1 parent cc81f7cd

add to_JSON function for convert the HK_Database into JSON format

types/model/app.anubis
... ... @@ -12,7 +12,8 @@ transmit icons.anubis
12 12  
13 13 read tools/basis.anubis
14 14 transmit system/muscle.anubis
15   -
  15 +transmit calexium_lib/web/CXM_json.anubis
  16 +
16 17 public type HK_App:
17 18 hk_app(
18 19 String app_name,
... ... @@ -81,6 +82,20 @@ public define Message
81 82 hk_app_message
82 83 .
83 84  
  85 +public define JsonValue
  86 + to_JSON
  87 + (
  88 + HK_App app
  89 + )=
  90 + json_object("_HK_APP",
  91 + [
  92 + json_member("name", json_string(app.app_name)),
  93 + json_member("icon", to_JSON(app.icon)),
  94 + json_member("help", to_JSON(app.help)),
  95 + json_member("tables", json_array(map((HK_Table table) |-> to_JSON(table), app.hk_tables)))
  96 + ]).
  97 +
  98 +
84 99 public define Maybe(HK_App)
85 100 from_Message
86 101 (
... ...
types/model/columns.anubis
... ... @@ -10,6 +10,7 @@ read tools/basis.anubis
10 10  
11 11 transmit help.anubis
12 12 transmit fields.anubis
  13 +transmit calexium_lib/web/CXM_json.anubis
13 14  
14 15 public type HK_Model_Attr:
15 16 unique, // by default, the values in a column are not required to be all different
... ... @@ -35,6 +36,27 @@ public define Message
35 36 hk_model_attr_message
36 37 .
37 38  
  39 +public define JsonValue
  40 + to_JSON
  41 + (
  42 + HK_Model_Attr hk_model_attr
  43 + )=
  44 + with members =
  45 + if hk_model_attr is
  46 + {
  47 + unique then [ json_member("hk_column_attr", json_string("unique")) ],
  48 +
  49 + indexed then [ json_member("hk_column_attr", json_string("indexed")) ],
  50 +
  51 + default(value) then [ json_member("hk_column_attr", json_string("default")),
  52 + json_member("value", json_string(value))
  53 + ],
  54 +
  55 + not_null then [ json_member("hk_column_attr", json_string("not_null")) ]
  56 + },
  57 +
  58 + json_object("_HK_COLUMN_ATTRIBUTES", members).
  59 +
38 60 public define Maybe(HK_Model_Attr)
39 61 from_Message
40 62 (
... ... @@ -113,7 +135,20 @@ public define Message
113 135 //print_to_stream(hk_model_column);
114 136 hk_model_column
115 137 .
116   -
  138 +
  139 +public define JsonValue
  140 + to_JSON
  141 + (
  142 + HK_Model_Column hk_column
  143 + )=
  144 + json_object("_HK_COLUMN",
  145 + [
  146 + json_member("name", json_string(hk_column.name)),
  147 + json_member("field_type", to_JSON(hk_column.type)),
  148 + json_member("attributes", json_array(map((HK_Model_Attr attribute) |-> to_JSON(attribute), hk_column.attributes))),
  149 + json_member("text", to_JSON(hk_column.help))
  150 + ]).
  151 +
117 152  
118 153 public define Maybe(HK_Model_Column)
119 154 from_Message
... ...
types/model/database.anubis
... ... @@ -11,6 +11,7 @@ transmit app.anubis
11 11 transmit tools/basis.anubis
12 12 transmit system/muscle.anubis
13 13 transmit calexium_lib/CXM_message_constants.anubis
  14 +transmit calexium_lib/web/CXM_json.anubis
14 15  
15 16  
16 17 public type HK_Database:
... ... @@ -20,7 +21,7 @@ public type HK_Database:
20 21  
21 22  
22 23 _HK_DATABASE message format:
23   - =========================
  24 + ============================
24 25  
25 26 +-----------+---------------+--------------------------
26 27 | name | type | description
... ... @@ -40,8 +41,33 @@ public define Message
40 41 //print_to_stream(hk_db_message);
41 42 hk_db_message
42 43 .
43   -
44   -
  44 +
  45 + _HK_DATABASE JSON format:
  46 + =========================
  47 +
  48 + {
  49 + "obj_type": "_HK_DATABASE", //_HK_DATABASE object type describe a database
  50 + "name": "XXXX", //name of database
  51 + "apps": //list of applications
  52 + [
  53 + { "obj_type": "_HK_APP", //_HK_APP object type describe an application
  54 + "name" : "app_name" //name of the current application
  55 + },
  56 + ... //anothers applications
  57 + ]
  58 + }
  59 +
  60 +public define JsonValue
  61 + to_JSON
  62 + (
  63 + HK_Database hk_db
  64 + )=
  65 + json_object(
  66 + [ json_member("obj_type", json_string("_HK_DATABASE")),
  67 + json_member("name", json_string(hk_db.name)),
  68 + json_member("apps", json_array(map((HK_App app) |-> to_JSON(app), hk_db.apps)))
  69 + ]).
  70 +
45 71 public define Maybe(HK_Database)
46 72 from_Message
47 73 (
... ...
types/model/display.anubis
... ... @@ -9,6 +9,7 @@
9 9 transmit system/muscle.anubis
10 10 transmit tools/basis.anubis
11 11 transmit calexium_lib/CXM_message_constants.anubis
  12 +transmit calexium_lib/web/CXM_json.anubis
12 13  
13 14 public type HK_List_View:
14 15 list_view_all,
... ... @@ -39,6 +40,28 @@ public define Message
39 40 };
40 41 _msg
41 42 .
  43 +
  44 +
  45 +public define JsonValue
  46 + to_JSON
  47 + (
  48 + HK_List_View list_view
  49 + )=
  50 + with members =
  51 + if list_view is
  52 + {
  53 + list_view_all then
  54 + [ json_member("type", json_string("list_view_all")) ],
  55 +
  56 + list_view(name, columns, link_id_action) then
  57 + [ json_member("type", json_string("list_view")),
  58 + json_member("view_name", json_string(name)),
  59 + json_member("link_id_action", json_string(link_id_action)),
  60 + json_member("columns", json_array(map((String column) |-> json_string(column), columns)))
  61 + ]
  62 + },
  63 +
  64 + json_object("_HK_LIST_VIEW", members).
42 65  
43 66 public define Maybe(HK_List_View)
44 67 from_Message
... ... @@ -73,13 +96,33 @@ public define Message
73 96 {
74 97 edit_view_all then forget(add_string(_msg, "type", "edit_view_all")),
75 98 edit_view(name, columns) then
76   - forget(add_string(_msg, "type", "list_view"));
  99 + forget(add_string(_msg, "type", "edit_view"));
77 100 forget(add_string(_msg, "view_name", name));
78 101 map_forget((String column) |-> forget(add_string(_msg, "columns", column)), columns)
79 102 };
80 103 _msg
81 104 .
82 105  
  106 +public define JsonValue
  107 + to_JSON
  108 + (
  109 + HK_Edit_View edit_view
  110 + )=
  111 + with members =
  112 + if edit_view is
  113 + {
  114 + edit_view_all then
  115 + [ json_member("type", json_string("edit_view_all")) ],
  116 +
  117 + edit_view(name, columns) then
  118 + [ json_member("type", json_string("edit_view")),
  119 + json_member("view_name", json_string(name)),
  120 + json_member("columns", json_array(map((String column) |-> json_string(column), columns)))
  121 + ]
  122 + },
  123 +
  124 + json_object("_HK_EDIT_VIEW", members).
  125 +
83 126 public define Maybe(HK_Edit_View)
84 127 from_Message
85 128 (
... ... @@ -127,6 +170,18 @@ public define Message
127 170 // map_forget((String _str) |-> add_string(hk_display_msg, "edits", _str), list_edit);
128 171 hk_display_msg
129 172 .
  173 +
  174 +public define JsonValue
  175 + to_JSON
  176 + (
  177 + HK_Display display
  178 + )=
  179 + since display is hk_display(list_views, list_edits),
  180 + json_object("_HK_MODEL_DISPLAY",
  181 + [ json_member("list_views", json_array(map((HK_List_View _view) |-> to_JSON(_view), list_views))),
  182 + json_member("list_edits", json_array(map((HK_Edit_View _edit) |-> to_JSON(_edit), list_edits)))
  183 + ]
  184 + ).
130 185  
131 186 public define Maybe(HK_Display)
132 187 get_hk_display
... ...
types/model/fields.anubis
... ... @@ -10,7 +10,7 @@ transmit tools/basis.anubis
10 10 transmit system/convert.anubis
11 11 transmit system/muscle.anubis
12 12 transmit calexium_lib/CXM_message_constants.anubis
13   -
  13 +transmit calexium_lib/web/CXM_json.anubis
14 14  
15 15 *** 'HK_Model_Field' (data types of table columns).
16 16  
... ... @@ -51,6 +51,25 @@ public define Message
51 51 hk_app_name_message
52 52 .
53 53  
  54 +public define JsonValue
  55 + to_JSON
  56 + (
  57 + HK_App_Name hk_app_name
  58 + )=
  59 + with members =
  60 + if hk_app_name is
  61 + {
  62 + this then [ json_member("type", json_string("this")) ],
  63 +
  64 + app_name(name) then [ json_member("type", json_string("app_name")),
  65 + json_member("name", json_string(name))
  66 + ],
  67 +
  68 + none then [ json_member("type", json_string("none")) ],
  69 + },
  70 +
  71 + json_object("_HK_APP_NAME", members).
  72 +
54 73 public define Maybe(HK_App_Name)
55 74 from_Message
56 75 (
... ... @@ -236,6 +255,62 @@ public define Message
236 255 hk_model_field_message
237 256 .
238 257  
  258 +public define JsonValue
  259 + to_JSON
  260 + (
  261 + HK_Model_Field hk_model_field
  262 + )=
  263 + with members =
  264 + if hk_model_field is
  265 + {
  266 + p_key then
  267 + [ json_member("type", json_string("p_key")) ],
  268 +
  269 + boolean_field then
  270 + [ json_member("type", json_string("boolean_field")) ],
  271 +
  272 + date_field(attr) then
  273 + [ json_member("type", json_string("date_field")),
  274 + json_member("dt_attribute", json_string(to_String(attr)))
  275 + ],
  276 +
  277 + time_field(attr) then
  278 + [ json_member("type", json_string("time_field")),
  279 + json_member("dt_attribute", json_string(to_String(attr)))
  280 + ],
  281 +
  282 + datetime_field(attr) then
  283 + [ json_member("type", json_string("datetime_field")),
  284 + json_member("dt_attribute", json_string(to_String(attr)))
  285 + ],
  286 +
  287 + foreign_key(app_name, table_name) then // foreign key to 'id' in another (or same) table and column_name for choice selector.
  288 + [ json_member("type", json_string("foreign_key")),
  289 + json_member("app_name", to_JSON(app_name)),
  290 + json_member("table_name", json_string(table_name)),
  291 + ],
  292 +
  293 + integer_field(choices) then // integer of arbitrary size
  294 + //TODO add choices if need
  295 + [ json_member("type", json_string("integer_field")) ],
  296 +
  297 + char_field (choices, size) then // text of maximal size 'size' (number of characters)
  298 + //TODO add choices if need
  299 + [ json_member("type", json_string("char_field")),
  300 + json_member("size", json_int(size))
  301 + ],
  302 +
  303 + password_field(size) then
  304 + [ json_member("type", json_string("password_field")),
  305 + json_member("size", json_int(size))
  306 + ],
  307 +
  308 + text_field then // text of variable size
  309 + [ json_member("type", json_string("text_field"))]
  310 + },
  311 +
  312 + json_object("_HK_FIELD", members).
  313 +
239 314 define Maybe(HK_Model_Field)
240 315 get_field
241 316 (
... ...
types/model/help.anubis
... ... @@ -9,6 +9,7 @@
9 9 transmit tools/basis.anubis
10 10 transmit system/muscle.anubis
11 11 transmit calexium_lib/CXM_message_constants.anubis
  12 +transmit calexium_lib/web/CXM_json.anubis
12 13  
13 14 public type HK_Help_Text:
14 15 no_help_text, //no help text available
... ... @@ -65,6 +66,25 @@ public define Message
65 66 };
66 67 hk_help_message
67 68 .
  69 +
  70 +public define JsonValue
  71 + to_JSON
  72 + (
  73 + HK_Help_Text hk_help
  74 + )=
  75 + with members =
  76 + if hk_help is
  77 + {
  78 + no_help_text then [ json_member("hk_help_text", json_string("no_help"))
  79 + ],
  80 + help_text_TAG(tag) then [ json_member("hk_help_text", json_string("TAG")),
  81 + json_member("value", json_string(tag))
  82 + ]
  83 + help_text(value) then [ json_member("hk_help_text", json_string("text")),
  84 + json_member("value", json_string(value))
  85 + ]
  86 + },
  87 + json_object("_HK_HELP_TEXT", members).
68 88  
69 89 public define Maybe(HK_Help_Text)
70 90 from_Message
... ...
types/model/icons.anubis
... ... @@ -9,6 +9,7 @@
9 9 transmit tools/basis.anubis
10 10 transmit system/muscle.anubis
11 11 transmit calexium_lib/CXM_message_constants.anubis
  12 +transmit calexium_lib/web/CXM_json.anubis
12 13  
13 14 public type HK_Icon:
14 15 no_icon,
... ... @@ -40,7 +41,7 @@ public define String
40 41 icon16(icn_str) then "\""+icn_str+"_icon\""
41 42 }.
42 43  
43   - * *
  44 + /* *
44 45 _HK_ICON message format:
45 46 =======================
46 47 +-----------+-----------+--------------------------------
... ... @@ -73,7 +74,25 @@ public define Message
73 74 forget(add_string(hk_icon_message, "icn_str", icn_str));
74 75 hk_icon_message
75 76 }.
  77 +
  78 +public define JsonValue
  79 + to_JSON
  80 + (
  81 + HK_Icon hk_icon
  82 + )=
  83 + with members =
  84 + if hk_icon is
  85 + {
  86 + no_icon then [ json_member("hk_icon", json_string("no_icon"))
  87 + ],
  88 +
  89 + icon16(icn_str) then [ json_member("hk_icon", json_string("icon_16")),
  90 + json_member("icn_str", json_string(icn_str))
  91 + ]
  92 + },
76 93  
  94 + json_object("_HK_ICON", members).
  95 +
77 96 public define Maybe(HK_Icon)
78 97 from_Message
79 98 (
... ...
types/model/model.anubis
... ... @@ -8,6 +8,7 @@
8 8  
9 9 transmit tools/basis.anubis
10 10 transmit columns.anubis
  11 +transmit calexium_lib/web/CXM_json.anubis
11 12  
12 13 public type HK_Model:
13 14 hk_model( List(HK_Model_Column) columns, // columns other than the primary key column (if any)
... ... @@ -37,7 +38,17 @@ public define Message
37 38 hk_model_message
38 39 .
39 40  
40   -
  41 +public define JsonValue
  42 + to_JSON
  43 + (
  44 + HK_Model hk_model
  45 + )=
  46 + json_object("_HK_MODEL",
  47 + [
  48 + json_member("columns", json_array(map((HK_Model_Column column) |-> to_JSON(column), hk_model.columns))),
  49 + json_member("show_str", json_string(hk_model.show_string))
  50 + ]).
  51 +
41 52 public define Maybe(HK_Model)
42 53 from_Message
43 54 (
... ...
types/model/tables.anubis
... ... @@ -9,6 +9,7 @@
9 9 read tools/basis.anubis
10 10 transmit display.anubis
11 11 transmit model.anubis
  12 +transmit calexium_lib/web/CXM_json.anubis
12 13  
13 14 public type HK_Table:
14 15 hk_table ( String name, //name of the table
... ... @@ -49,6 +50,19 @@ public define Message
49 50 forget(add_message(hk_table_message, "hk_display", to_Message(table.display)));
50 51 //print_to_stream(hk_table_message);
51 52 hk_table_message.
  53 +
  54 +public define JsonValue
  55 + to_JSON
  56 + (
  57 + HK_Table table
  58 + )=
  59 + json_object("_HK_TABLE",
  60 + [ json_member("name", json_string(table.name)),
  61 + json_member("short_name", json_string(table.name)),
  62 + json_member("model", to_JSON(table.model)),
  63 + json_member("hk_display", to_JSON(table.display))
  64 + ]
  65 + ).
52 66  
53 67 public define Maybe(HK_Table)
54 68 from_Message
... ...