flutter_rust_bridge icon indicating copy to clipboard operation
flutter_rust_bridge copied to clipboard

Generated freezed class incapsulating factory instead defining own class

Open Vedsaga opened this issue 1 year ago • 6 comments

Is your feature request related to a problem? Please describe. As in dart, it is standard to have the class name is written as CamelCase. Instead of snake_case.

for e.g. if we use enum for grouping the struct to represent same type,

enum MediaFile {
    Local(LocalMediaFile),
    Processed(ProcessedMediaFile),
}

#[frb(dart_metadata=("freezed"))]
struct LocalMediaFile {
    uuid: String,       // unique file id which we locally assigned to the file
    local_path: String, // file path where the selected file is stored in device
    file_type: FileType, // file type either video or image
}
#[frb(dart_metadata=("freezed"))]
struct ProcessedMediaFile {
    local_file: LocalMediaFile,
    path_post_process: String,
    md5hash_signature: String,
}

enum FileType {
    Video,
    Image,
}

actual output


@freezed
class LocalMediaFile with _$LocalMediaFile {
  const factory LocalMediaFile({
    required String uuid,
    required String localPath,
    required FileType fileType,
  }) = _LocalMediaFile;
}

@freezed
sealed class MediaFile with _$MediaFile {
  const factory MediaFile.local(
    LocalMediaFile field0,
  ) = MediaFile_Local;
  const factory MediaFile.processed(
    ProcessedMediaFile field0,
  ) = MediaFile_Processed;
}

@freezed
class ProcessedMediaFile with _$ProcessedMediaFile {
  const factory ProcessedMediaFile({
    required LocalMediaFile localFile,
    required String pathPostProcess,
    required String md5HashSignature,
  }) = _ProcessedMediaFile;
}

Describe the solution you'd like

expected the output

@freezed
sealed class MediaFile with _$MediaFile {
  // model representing file which user have selected & to be processed &
  // then to upload uploaded
  const factory MediaFile.local({
    // unique file id which we locally assigned to the file
    // so that when file is being uploaded in the data base we set the same id
    required String uuid,
    // file path where the selected file is stored in device
    required String localPath,
    // file type either video or image
    required FileType fileType,

  }) = LocalMediaFile;

    // this file is processed & ready to be uploaded
  const factory MediaFile.processed({
    required LocalMediaFile localFile,
    required String pathPostProcess,
    required String md5hashSignature,
  }) = ProcessedMediaFile;

}

As we can see that, we were able to encapsulate the various possible object representing the MeidaFile, as factory while writing dart.

Describe alternatives you've considered It would allow using the match cases in switch statement ( which we can still do with current approach) however, as we would notice that when using the switch cases you would to read field0 to access the respective object. As, in currently generated factory LocalMediaFile & ProcessedMediaFile need to be passed as instance ( which is fine not big deal).

Sorry, if I might not aware if it's already then great, was wondering if can control the name of the factory class by pass in dart_metadata?

Additional context Add any other context or screenshots about the feature request here.

Vedsaga avatar Jan 15 '24 14:01 Vedsaga

Hi! Thanks for opening your first issue here! :smile:

welcome[bot] avatar Jan 15 '24 14:01 welcome[bot]

Looks reasonable! Firstly, what about this one, does it work:

enum MediaFile {
    Local {
      uuid: String,       // unique file id which we locally assigned to the file
      local_path: String, // file path where the selected file is stored in device
      file_type: FileType, // file type either video or image
    },
    Processed { .. },
}

fzyzcjy avatar Jan 15 '24 14:01 fzyzcjy

    Local {
      uuid: String,       // unique file id which we locally assigned to the file
      local_path: String, // file path where the selected file is stored in device
      file_type: FileType, // file type either video or image
    },

At some level it does worked, what I mean is that if we have the local then it wokred, however as we see that Proceeded struct contains the Local Struct then it does not seems work...

below rust code


enum MediaFile {
    Local {
        uuid: String,        // unique file id which we locally assigned to the file
        local_path: String,  // file path where the selected file is stored in device
        file_type: FileType, // file type either video or image
    },
    Processed{
    local_file: LocalMediaFile,
    path_post_process: String,
    md5hash_signature: String,
},
}

yields


@freezed
class LocalMediaFile with _$LocalMediaFile {
  const factory LocalMediaFile({
    required String uuid,
    required String localPath,
    required FileType fileType,
  }) = _LocalMediaFile;
}

@freezed
sealed class MediaFile with _$MediaFile {
  const factory MediaFile.local({
    required String uuid,
    required String localPath,
    required FileType fileType,
  }) = MediaFile_Local;
  const factory MediaFile.processed({
    required LocalMediaFile localFile,
    required String pathPostProcess,
    required String md5HashSignature,
  }) = MediaFile_Processed;
}

as you can see that procceed not getting generted...

Vedsaga avatar Jan 15 '24 14:01 Vedsaga

Oh I see the problem... Yes that sounds pretty reasonable, feel free to PR! (I may try to find time doing it, but since this is an enhancement instead of a bug, I may not be able to squeeze out some time)

fzyzcjy avatar Jan 15 '24 14:01 fzyzcjy

Oh I see the problem... Yes that sounds pretty reasonable, feel free to PR! (I may try to find time doing it, but since this is an enhancement instead of a bug, I may not be able to squeeze out some time)

Thank you so much, you are putting souch time. And keeping this lively.

Will try to submit PR.

Vedsaga avatar Jan 15 '24 14:01 Vedsaga

You are welcome, take your time, and looking forward to the PR!

fzyzcjy avatar Jan 15 '24 23:01 fzyzcjy

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 22 '24 09:03 stale[bot]

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.

github-actions[bot] avatar Apr 14 '24 01:04 github-actions[bot]