source_gen_test icon indicating copy to clipboard operation
source_gen_test copied to clipboard

feat: add support for custom output formatting

Open tnc1997 opened this issue 1 year ago • 0 comments

This change adds support for custom output formatting and is inspired by #29 and https://github.com/google/json_serializable.dart/pull/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});
}

tnc1997 avatar May 29 '23 09:05 tnc1997