Source

hob / hob / templates / js / js-message-classes.mako

Full commit
<%
  INDENT = indent or '  '
  local_comments = comments or ["/** \n", "  * ", "  */"]
  version = ".".join(service.options["version"].value.split('.')[:2])
  def get_submessages(fields, names=[]):
      ret = []
      for field in fields:
          if field.message and isinstance(field.message, proto.Message) and not field.message.name in names:
              names.append(field.message.name)
              ret.append(field)
              ret += get_submessages(field.message.fields, names)
      return ret
  submessages = get_submessages(message.fields)
%>\
  ## ************************************************************** ##
  ## def print_doc
  ## ************************************************************** ##
<%def name="print_doc(doc, indent_level=0, indent='  ')">\
${(local_comments[0] and indent_level or 0)*indent}${local_comments[0]}\
      % for line in doc.text.splitlines():
${(indent_level)*indent}${local_comments[1]}${line.strip('\r\n')}
      % endfor
${(indent_level)*indent}${local_comments[2]}\
</%def>\
  ## ************************************************************** ##
  ## def print_enum
  ## ************************************************************** ##
<%def name="print_enum(enum)">\
    % if enum.doc:
${print_doc(enum.doc, 0, INDENT)}
    % endif
cls.${service.name}["${version}"].${".".join(enum.path())} =
{
    % for value in enum.values:
      % if value.doc:
${print_doc(value.doc, 1, INDENT)}
      % endif
${INDENT}${value.value}: "${value.name}",
    % endfor
};
  % for value in enum.values:
    % if value.doc:
${print_doc(value.doc, 0, INDENT)}
    % endif
cls.${service.name}["${version}"].${".".join(enum.path())}.${value.name} = ${value.value};
  % endfor
</%def>\
  ## ************************************************************** ##
  ## def message_class
  ## ************************************************************** ##
<%def name="message_class(message, submessages=None)">\
  % if message.doc:
${print_doc(message.doc, 0, INDENT)}
  % endif
  % if message.comment:
${INDENT}// ${message.comment}
  % endif
cls.${service.name}["${version}"].${message.name} = function(arr)
{
  % for index, field in enumerate(message.fields):
    % if field.doc:
${print_doc(field.doc, 1, INDENT)}
    % endif
    % if field.comment:
${INDENT}// ${field.comment}
    % endif
    % if field.q.name == "repeated":
      % if field.type.name in ["message"]:
${INDENT}this.${field.name} = (arr[${str(index)}] || []).map(function(item)
${INDENT}{
${INDENT * 2}return new cls.${service.name}["${version}"].${field.typename()}(item);
${INDENT}});
      % else:
${INDENT}this.${field.name} = arr[${str(index)}] || [];
      % endif
    % elif field.message and isinstance(field.message, proto.Message):
${INDENT}this.${field.name} = arr[${str(index)}] ? new cls.${service.name}["${version}"].${field.typename()}(arr[${str(index)}]) : null;
    % else:
      % if field.message and isinstance(field.message, proto.Enum):
${INDENT}// cls.${service.name}["${version}"].${".".join(field.message.path())}
      % endif
${INDENT}this.${field.name} = arr[${str(index)}];
    % endif
  % endfor
};
% for field in message.fields:
  % if field.message and isinstance(field.message, proto.Enum):
${print_enum(field.message)}
  % endif
% endfor
</%def>\
  ## ************************************************************** ##
  ## template
  ## ************************************************************** ##
// Autogenerated by hob
window.cls || (window.cls = {});
cls.${service.name} || (cls.${service.name} = {});
cls.${service.name}["${version}"] || (cls.${service.name}["${version}"] = {});

${message_class(message, submessages=submessages)}
% for submsg in submessages:
${message_class(submsg.message, submessages=None)}
% endfor