generateConstants method

void generateConstants(
  1. IndentingWriter out
)

Writes a Dart constant containing the JSON for the ProtoDescriptor. Also writes a separate constant for each nested message, to avoid duplication.

Implementation

void generateConstants(IndentingWriter out) {
  const nestedTypeTag = 3;
  const enumTypeTag = 4;
  assert(_descriptor.info_.fieldInfo[nestedTypeTag]!.name == 'nestedType');
  assert(_descriptor.info_.fieldInfo[enumTypeTag]!.name == 'enumType');

  final name = getJsonConstant(fileGen);
  final json = _descriptor.writeToJsonMap();
  final nestedTypeNames =
      _messageGenerators.map((m) => m.getJsonConstant(fileGen)).toList();
  final nestedEnumNames =
      _enumGenerators.map((e) => e.getJsonConstant(fileGen)).toList();

  out.println('@$coreImportPrefix.Deprecated'
      '(\'Use ${toplevelParent!.binaryDescriptorName} instead\')');
  out.addBlock('const $name = {', '};', () {
    for (final key in json.keys) {
      out.print("'$key': ");
      if (key == '$nestedTypeTag') {
        // refer to message constants by name instead of repeating each value
        out.println("[${nestedTypeNames.join(", ")}],");
        continue;
      } else if (key == '$enumTypeTag') {
        // refer to enum constants by name
        out.println("[${nestedEnumNames.join(", ")}],");
        continue;
      }
      writeJsonConst(out, json[key]);
      out.println(',');
    }
  });
  out.println();

  for (final m in _messageGenerators) {
    m.generateConstants(out);
  }

  for (final e in _enumGenerators) {
    e.generateConstants(out);
  }
}