schema.sbn 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. {{~for enum in __enums~}}{{~if enum.comment != '' ~}}
  2. /**
  3. * {{enum.comment | html.escape}}
  4. */
  5. {{~end~}}
  6. export const {{full_name enum}} = Object.freeze({
  7. {{~for item in enum.items ~}}
  8. {{~if item.comment != '' ~}}
  9. /**
  10. * {{escape_comment item.comment}}
  11. */
  12. {{~end~}}
  13. {{item.name}}: {{item.int_value}},
  14. {{~end~}}
  15. });
  16. {{~end~}}
  17. {{~
  18. func get_ref_name
  19. ret (format_property_name __code_style $0.name) + '_ref'
  20. end
  21. func generate_resolve_field_ref
  22. field = $0
  23. fieldName = format_property_name __code_style field.name
  24. refTable = get_ref_table field
  25. if can_generate_ref field
  26. tableName = refTable.name
  27. if field.is_nullable
  28. ret 'this.'+(get_ref_name field) + ' = this.' + fieldName + '!= null ? tables.' + tableName + '.get(this.' + fieldName + ') : null'
  29. else
  30. ret 'this.'+(get_ref_name field) + ' = tables.' + tableName + '.get(this.' + fieldName + ')'
  31. end
  32. else
  33. if (is_field_bean_need_resolve_ref field)
  34. ret 'this.'+fieldName + '?.resolve(tables);'
  35. else if (is_field_array_like_need_resolve_ref field)
  36. ret 'for (let _e of ' + 'this.' + fieldName + ') { _e?.resolve(tables); }'
  37. else if (is_field_map_need_resolve_ref field)
  38. ret 'for (let [_, _e] of ' + 'this.' + fieldName + ') { _e?.resolve(tables); }'
  39. else
  40. ret ''
  41. end
  42. end
  43. end
  44. ~}}
  45. {{~for bean in __beans~}}
  46. {{~if bean.comment != '' ~}}
  47. /**
  48. * {{escape_comment bean.comment}}
  49. */
  50. {{~end~}}
  51. export class {{full_name bean}}{{if bean.parent_def_type}} extends {{full_name bean.parent_def_type}}{{end}} {
  52. {{~if bean.is_abstract_type~}}
  53. static constructorFrom(_buf_) {
  54. switch (_buf_.readInt()) {
  55. {{~ for child in bean.hierarchy_not_abstract_children~}}
  56. case {{child.id}}: return new {{full_name child}}(_buf_);
  57. {{~end~}}
  58. default: throw new Error();
  59. }
  60. }
  61. {{~end~}}
  62. constructor(_buf_) {
  63. {{~if bean.parent_def_type~}}
  64. super(_buf_);
  65. {{~end~}}
  66. {{~ for field in bean.export_fields ~}}
  67. {{deserialize ('this.' + format_field_name __code_style field.name) '_buf_' field.ctype}};
  68. {{~end~}}
  69. }
  70. resolve(tables) {
  71. {{~if bean.parent_def_type~}}
  72. super.resolve(tables);
  73. {{~end~}}
  74. {{~ for field in bean.export_fields ~}}
  75. {{generate_resolve_field_ref field}}
  76. {{~end~}}
  77. }
  78. }
  79. {{~end~}}
  80. {{~for table in __tables
  81. key_type = table.key_ttype
  82. value_type = table.value_ttype
  83. ~}}
  84. {{~if table.comment != '' ~}}
  85. /**
  86. * {{escape_comment table.comment}}
  87. */
  88. {{~end~}}
  89. export class {{full_name table}} {
  90. {{~if table.is_map_table ~}}
  91. constructor(_buf_) {
  92. this._dataMap = new Map();
  93. this._dataList = [];
  94. for(let n = _buf_.readInt(); n > 0; n--) {
  95. let _v;
  96. {{deserialize '_v' '_buf_' value_type}};
  97. this._dataList.push(_v);
  98. this._dataMap.set(_v.{{format_field_name __code_style table.index_field.name}}, _v);
  99. }
  100. }
  101. getDataMap() { return this._dataMap; }
  102. getDataList() { return this._dataList; }
  103. get(key) { return this._dataMap.get(key); }
  104. resolve(tables) {
  105. for(let data of this._dataList) {
  106. data.resolve(tables);
  107. }
  108. }
  109. {{~else if table.is_list_table ~}}
  110. constructor(_buf_) {
  111. this._dataList = [];
  112. for(let n = _buf_.readInt(); n > 0; n--) {
  113. let _v;
  114. {{deserialize '_v' '_buf_' value_type}};
  115. this._dataList.push(_v);
  116. }
  117. }
  118. getDataList() { return this._dataList }
  119. get(index) { return this._dataList[index] }
  120. resolve(tables) {
  121. for(let data of this._dataList) {
  122. data.resolve(tables);
  123. }
  124. }
  125. {{~else~}}
  126. constructor(_buf_) {
  127. if (_buf_.readInt() != 1) throw new Error('table mode=one, but size != 1')
  128. {{deserialize 'this._data' '_buf_' value_type}}
  129. }
  130. getData() { return this._data; }
  131. {{~ for field in value_type.def_bean.hierarchy_export_fields ~}}
  132. {{~if field.comment != '' ~}}
  133. /**
  134. * {{escape_comment field.comment}}
  135. */
  136. {{~end~}}
  137. get {{format_field_name __code_style field.name}}() { return this._data.{{format_field_name __code_style field.name}}; }
  138. {{~if can_generate_ref field~}}
  139. get {{format_field_name __code_style field.name}}_ref(): { return this._data.{{format_field_name __code_style field.name}}_ref; }
  140. {{~end~}}
  141. {{~end~}}
  142. resolve(tables) {
  143. this._data.resolve(tables);
  144. }
  145. {{end}}
  146. }
  147. {{~end~}}
  148. export class {{__name}} {
  149. {{~ for table in __tables ~}}
  150. {{~if table.comment != '' ~}}
  151. /**
  152. * {{escape_comment table.comment}}
  153. */
  154. {{~end~}}
  155. get {{table.name}}() { return this._{{table.name}};}
  156. {{~end~}}
  157. constructor(loader) {
  158. {{~for table in __tables ~}}
  159. this._{{table.name}} = new {{full_name table}}(loader('{{table.output_data_file}}'));
  160. {{~end~}}
  161. {{~for table in __tables ~}}
  162. this._{{table.name}}.resolve(this);
  163. {{~end~}}
  164. }
  165. }