toDartCode method

String toDartCode({
  1. String classNamePrefix = '',
  2. bool fieldStatic = false,
  3. List<String> classNamePath = const [],
  4. bool generateInstance = false,
  5. String? instanceName,
})

to dart code classNamePrefix 類別名稱的前綴 fieldStatic 所有欄位是否為static classNamePath 類別名稱的路徑 generateInstance 是否產生實體 instanceName 實體名稱

Implementation

String toDartCode({
  String classNamePrefix = '',
  bool fieldStatic = false,
  List<String> classNamePath = const [],
  bool generateInstance = false,
  String? instanceName,
}) {
  // 第一層結構才會有前綴文字, 並且field需要是static

  final classCode = _toSelfDartCode(
    classNamePrefix: classNamePrefix,
    fieldStatic: fieldStatic,
    classNamePath: [...classNamePath, name],
    generateInstance: generateInstance,
    instanceName: instanceName,
  );

  final structCode = structs.map((e) {
    return e.toDartCode(
      classNamePrefix: classNamePrefix,
      fieldStatic: false,
      classNamePath: [...classNamePath, name],
      generateInstance: false,
      instanceName: null,
    );
  }).join('\n');

  return '$classCode\n\n$structCode';
}