Skip to content

Conversation

tnc1997
Copy link

@tnc1997 tnc1997 commented May 29, 2023

This change adds support for custom output formatting and is inspired by #29 and google/json_serializable.dart#179. I am currently in the process of rewriting the tests in xml_serializable to use this package instead of the generator directly and it would be very useful (and more performant) to be able to skip the formatting of the generated source code.

Before
@ShouldGenerate(r'''
void _$StringHappyPathBuildXmlChildren(
    StringHappyPath instance, XmlBuilder builder,
    {Map<String, String> namespaces = const {}}) {
  final value = instance.value;
  final valueSerialized = value;
  builder.attribute('value', valueSerialized);
}

StringHappyPath _$StringHappyPathFromXmlElement(XmlElement element) {
  final value = element.getAttribute('value')!;
  return StringHappyPath(value: value);
}

List<XmlAttribute> _$StringHappyPathToXmlAttributes(StringHappyPath instance,
    {Map<String, String?> namespaces = const {}}) {
  final attributes = <XmlAttribute>[];
  final value = instance.value;
  final valueSerialized = value;
  final valueConstructed = XmlAttribute(XmlName('value'), valueSerialized);
  attributes.add(valueConstructed);
  return attributes;
}

List<XmlNode> _$StringHappyPathToXmlChildren(StringHappyPath instance,
    {Map<String, String?> namespaces = const {}}) {
  final children = <XmlNode>[];
  return children;
}
''')
@XmlSerializable()
class StringHappyPath {
  @XmlAttribute()
  String value;

  StringHappyPath({required this.value});
}
After
@ShouldGenerate(r'''
void _$StringHappyPathBuildXmlChildren(StringHappyPath instance, XmlBuilder builder, {Map<String, String> namespaces = const {}}) {
final value = instance.value;
final valueSerialized = value;
builder.attribute('value', valueSerialized);
}

StringHappyPath _$StringHappyPathFromXmlElement(XmlElement element) {
final value = element.getAttribute('value')!;
return StringHappyPath(value: value);
}

List<XmlAttribute> _$StringHappyPathToXmlAttributes(StringHappyPath instance, {Map<String, String?> namespaces = const {}}) {
final attributes = <XmlAttribute>[];
final value = instance.value;
final valueSerialized = value;
final valueConstructed = XmlAttribute(XmlName('value'), valueSerialized);
attributes.add(valueConstructed);
return attributes;
}

List<XmlNode> _$StringHappyPathToXmlChildren(StringHappyPath instance, {Map<String, String?> namespaces = const {}}) {
final children = <XmlNode>[];
return children;
}''')
@XmlSerializable()
class StringHappyPath {
  @XmlAttribute()
  String value;

  StringHappyPath({required this.value});
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant