Skip to content

Commit

Permalink
feat: add is self closing (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnc1997 committed Jan 2, 2021
1 parent baed31f commit 9956218
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
2 changes: 2 additions & 0 deletions xml_annotation/lib/src/annotations/xml_element.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class XmlElement {
final String? name;
final String? namespace;
final bool isSelfClosing;

const XmlElement({
this.name,
this.namespace,
this.isSelfClosing = true,
});
}
2 changes: 2 additions & 0 deletions xml_annotation/lib/src/annotations/xml_root_element.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class XmlRootElement {
final String? name;
final String? namespace;
final bool isSelfClosing;

const XmlRootElement({
this.name,
this.namespace,
this.isSelfClosing = true,
});
}
6 changes: 3 additions & 3 deletions xml_serializable/example/xml_serializable_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ void main() {
<title lang="english">XML Pocket Reference</title>
<author>Simon St. Laurent</author>
<author>Michael James Fitzgerald</author>
<price>6.63</price>
<price></price>
</book>
<book>
<title lang="english">HTML and XHTML Pocket Reference</title>
<author>Jennifer Niederst Robbins</author>
<price>7.09</price>
<price></price>
</book>
<price/>
</bookshelf>''',
Expand All @@ -38,7 +38,7 @@ class Book {
@annotation.XmlElement(name: 'author')
List<String>? authors;

@annotation.XmlElement(name: 'price')
@annotation.XmlElement(name: 'price', isSelfClosing: false)
String? price;

Book({
Expand Down
13 changes: 13 additions & 0 deletions xml_serializable/example/xml_serializable_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class XmlSerializableGenerator extends GeneratorForAnnotation<XmlSerializable> {

final name = annotation.peekStringValue('name') ?? element.name;
final namespace = annotation.peekStringValue('namespace');
final isSelfClosing = annotation.readBoolValue('isSelfClosing');

if (elementTypeElement.hasXmlSerializable) {
buffer.writeln(
Expand All @@ -135,6 +136,10 @@ class XmlSerializableGenerator extends GeneratorForAnnotation<XmlSerializable> {
);
}

buffer.writeln(
'isSelfClosing: $isSelfClosing,',
);

buffer.writeln(
'nest: () {',
);
Expand Down Expand Up @@ -173,6 +178,10 @@ class XmlSerializableGenerator extends GeneratorForAnnotation<XmlSerializable> {
);
}

buffer.writeln(
'isSelfClosing: $isSelfClosing,',
);

buffer.writeln(
'nest: () {',
);
Expand Down Expand Up @@ -224,6 +233,10 @@ class XmlSerializableGenerator extends GeneratorForAnnotation<XmlSerializable> {
);
}

buffer.writeln(
'isSelfClosing: $isSelfClosing,',
);

buffer.writeln(
'nest: () {',
);
Expand Down Expand Up @@ -270,6 +283,10 @@ class XmlSerializableGenerator extends GeneratorForAnnotation<XmlSerializable> {
);
}

buffer.writeln(
'isSelfClosing: $isSelfClosing,',
);

buffer.writeln(
'nest: () {',
);
Expand Down Expand Up @@ -645,6 +662,7 @@ class XmlSerializableGenerator extends GeneratorForAnnotation<XmlSerializable> {

final name = annotation.peekStringValue('name') ?? element.name;
final namespace = annotation.peekStringValue('namespace');
final isSelfClosing = annotation.readBoolValue('isSelfClosing');

if (elementTypeElement.hasXmlSerializable) {
buffer.writeln(
Expand Down Expand Up @@ -677,6 +695,10 @@ class XmlSerializableGenerator extends GeneratorForAnnotation<XmlSerializable> {
'[if (${element.name} != null) ...${element.name}.toXmlChildren(namespaces: namespaces,),],',
);

buffer.writeln(
'$isSelfClosing,',
);

buffer.writeln(
'),',
);
Expand Down Expand Up @@ -711,6 +733,10 @@ class XmlSerializableGenerator extends GeneratorForAnnotation<XmlSerializable> {
'[if (${element.name} != null) XmlText(${element.name},),],',
);

buffer.writeln(
'$isSelfClosing,',
);

buffer.writeln(
'),',
);
Expand Down Expand Up @@ -758,6 +784,10 @@ class XmlSerializableGenerator extends GeneratorForAnnotation<XmlSerializable> {
'value.toXmlChildren(namespaces: namespaces,),',
);

buffer.writeln(
'$isSelfClosing,',
);

buffer.writeln(
'),',
);
Expand Down Expand Up @@ -800,6 +830,10 @@ class XmlSerializableGenerator extends GeneratorForAnnotation<XmlSerializable> {
'[XmlText(value,),],',
);

buffer.writeln(
'$isSelfClosing,',
);

buffer.writeln(
'),',
);
Expand Down Expand Up @@ -835,6 +869,7 @@ class XmlSerializableGenerator extends GeneratorForAnnotation<XmlSerializable> {

final name = annotation.peekStringValue('name') ?? element.name;
final namespace = annotation.peekStringValue('namespace');
final isSelfClosing = annotation.readBoolValue('isSelfClosing');

buffer.writeln(
'return XmlElement(',
Expand Down Expand Up @@ -864,6 +899,10 @@ class XmlSerializableGenerator extends GeneratorForAnnotation<XmlSerializable> {
'instance.toXmlChildren(namespaces: namespaces,),',
);

buffer.writeln(
'$isSelfClosing,',
);

buffer.writeln(
');',
);
Expand Down

0 comments on commit 9956218

Please sign in to comment.