writeClassDecode method

  1. @override
void writeClassDecode(
  1. ArkTSOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. Class klass,
  5. Set<String> customClassNames,
  6. Set<String> customEnumNames, {
  7. required String dartPackageName,
})
override

编写dataclass的fromlist方法 fromList(arr: Object[]):Class { let instance: Class = new Class(arr0 as xxx,arr1 as xxx); return instance; }

Implementation

@override
void writeClassDecode(
  ArkTSOptions generatorOptions,
  Root root,
  Indent indent,
  Class klass,
  Set<String> customClassNames,
  Set<String> customEnumNames, {
  required String dartPackageName,
}) {
  indent.newln();
  indent.write('static fromList(arr: Object[]): ${klass.name} ');
  indent.addScoped('{', '}', () {
    indent.write('let instance: ${klass.name} = new ${klass.name}(');
    for (int i = 0; i < klass.fields.length; i++) {
      final NamedType field = klass.fields[i];
      if (customEnumNames.contains(field.type.baseName)) {
        indent.add(
            '${field.type.baseName}[${field.type.baseName}[arr[$i] as number]]');
      } else if (customClassNames.contains(field.type.baseName)) {
        indent.add('arr[$i] instanceof Array ? ${field.type.baseName}.fromList(arr[$i] as Object[]) : null');
      } else {
        final String type = _arkTSTypeForDartType(field.type);
        indent.add('arr[$i] as $type');
      }
      if (i != klass.fields.length - 1) {
        indent.add(', ');
      }
    }
    indent.add(');');
    indent.newln();
    indent.writeln('return instance;');
  });
}