tnc1997 / dart-xml-serializable

Generates utilities to aid in serializing to/from XML.

Home Page:https://pub.dev/packages/xml_serializable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enum and other types

Zekfad opened this issue · comments

Is your feature request related to a problem? Please describe.
I'd like to be able to parse enums (for example S3 Object#ChecksumAlgorithm is a clear enum).
Also it would be nice if it would be possible to parse arbitrary type if it has factory constructor parse(String string)/static function tryParse(String string) or factory constructor fromXmlElement(XmlElement element).

Describe the solution you'd like
Test if required type has has factory constructor parse(String input) or factory constructor fromXmlElement(XmlElement element) and use it.

Describe alternatives you've considered
Don't have any yet.

Additional context
Enum from link could be like this:

enum ChecksumAlgorithm {
  crc32('CRC32'),
  crc32c('CRC32C'),
  sha1('SHA1'),
  sha256('SHA256');

  const ChecksumAlgorithm(this.value);

  factory ChecksumAlgorithm.fromXmlElement(XmlElement element) =>
    values.firstWhere((_enum) => _enum.value == element.text);

  final String value;
}

Hi @Zekfad, thank you very much for your feature request.

It is certainly something that would be useful to support so I will aim to investigate it over the next few weeks.

I will likely try to follow a similar approach to how json_serializable handles enums to keep things consistent.

In the meantime you could workaround the lack of support by storing the field as a string and adding constants.

Thank you very much again for your feature request, I am aiming to release this functionality within the next week.

Special thanks to @marius-h for implementing the functionality.

Nice to see this feature, thanks @tnc1997, @marius-h !