Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add xml converter and update fakes #57

Merged
merged 4 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ jobs:
- name: Run tests
run: |
cd xml_serializable
dart test
dart test --coverage './coverage'
- name: Format coverage
run: |
cd xml_serializable
dart pub global activate coverage
dart pub global run coverage:format_coverage --report-on 'lib' --lcov --out './coverage/lcov.info' --in './coverage'
- name: Upload coverage
uses: actions/upload-artifact@v4
with:
name: coverage-${{ matrix.os }}-${{ matrix.sdk }}
path: ./xml_serializable/coverage/lcov.info
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extension ClassElementExtensions on ClassElement {
for (var field in fields) field.name: field,
};

for (var supertype in allSupertypes) {
for (var supertype in thisType.allSupertypes) {
for (var field in supertype.element.fields) {
allFields.putIfAbsent(field.name, () => field);
}
Expand Down
151 changes: 39 additions & 112 deletions xml_serializable/lib/src/extensions/dart_object_extensions.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:xml_annotation/xml_annotation.dart';

import 'dart_type_extensions.dart';

extension DartObjectExtensions on DartObject {
/// Returns the value of the field with the given [name] as a [bool] or `null` if the field is not a [bool].
@Deprecated(
Expand Down Expand Up @@ -30,19 +30,10 @@ extension DartObjectExtensions on DartObject {

/// Returns a [FieldRename] corresponding to the value of the object being represented or `null` if this object is not of type [FieldRename] or the value of the object being represented is `null`.
FieldRename? toFieldRenameValue() {
final type = this.type;

if (type is InterfaceType) {
final element = type.element;

if (element is EnumElement &&
element.library.identifier.startsWith('package:xml_annotation') &&
element.name == 'FieldRename') {
final index = getField('index')?.toIntValue();

if (index != null) {
return FieldRename.values[index];
}
if (type?.isXmlAnnotationFieldRename == true) {
final index = getField('index')?.toIntValue();
if (index != null) {
return FieldRename.values[index];
}
}

Expand All @@ -51,152 +42,88 @@ extension DartObjectExtensions on DartObject {

/// Returns an [XmlAttribute] corresponding to the value of the object being represented or `null` if this object is not of type [XmlAttribute].
XmlAttribute? toXmlAttributeValue() {
final type = this.type;

if (type is InterfaceType) {
final element = type.element;

if (element is ClassElement &&
element.library.identifier.startsWith('package:xml_annotation') &&
element.name == 'XmlAttribute') {
return XmlAttribute(
name: getField('name')?.toStringValue(),
namespace: getField('namespace')?.toStringValue(),
);
}
if (type?.isXmlAnnotationXmlAttribute == true) {
return XmlAttribute(
name: getField('name')?.toStringValue(),
namespace: getField('namespace')?.toStringValue(),
);
}

return null;
}

/// Returns an [XmlCDATA] corresponding to the value of the object being represented or `null` if this object is not of type [XmlCDATA].
XmlCDATA? toXmlCDATAValue() {
final type = this.type;

if (type is InterfaceType) {
final element = type.element;

if (element is ClassElement &&
element.library.identifier.startsWith('package:xml_annotation') &&
element.name == 'XmlCDATA') {
return XmlCDATA();
}
if (type?.isXmlAnnotationXmlCDATA == true) {
return XmlCDATA();
}

return null;
}

/// Returns an [XmlElement] corresponding to the value of the object being represented or `null` if this object is not of type [XmlElement].
XmlElement? toXmlElementValue() {
final type = this.type;

if (type is InterfaceType) {
final element = type.element;

if (element is ClassElement &&
element.library.identifier.startsWith('package:xml_annotation') &&
element.name == 'XmlElement') {
return XmlElement(
name: getField('name')?.toStringValue(),
namespace: getField('namespace')?.toStringValue(),
isSelfClosing: getField('isSelfClosing')?.toBoolValue(),
includeIfNull: getField('includeIfNull')?.toBoolValue(),
);
}
if (type?.isXmlAnnotationXmlElement == true) {
return XmlElement(
name: getField('name')?.toStringValue(),
namespace: getField('namespace')?.toStringValue(),
isSelfClosing: getField('isSelfClosing')?.toBoolValue(),
includeIfNull: getField('includeIfNull')?.toBoolValue(),
);
}

return null;
}

/// Returns an [XmlEnum] corresponding to the value of the object being represented or `null` if this object is not of type [XmlEnum].
XmlEnum? toXmlEnumValue() {
final type = this.type;

if (type is InterfaceType) {
final element = type.element;

if (element is ClassElement &&
element.library.identifier.startsWith('package:xml_annotation') &&
element.name == 'XmlEnum') {
return XmlEnum(
fieldRename: getField('fieldRename')?.toFieldRenameValue(),
);
}
if (type?.isXmlAnnotationXmlEnum == true) {
return XmlEnum(
fieldRename: getField('fieldRename')?.toFieldRenameValue(),
);
}

return null;
}

/// Returns an [XmlRootElement] corresponding to the value of the object being represented or `null` if this object is not of type [XmlRootElement].
XmlRootElement? toXmlRootElementValue() {
final type = this.type;

if (type is InterfaceType) {
final element = type.element;

if (element is ClassElement &&
element.library.identifier.startsWith('package:xml_annotation') &&
element.name == 'XmlRootElement') {
return XmlRootElement(
name: getField('name')?.toStringValue(),
namespace: getField('namespace')?.toStringValue(),
isSelfClosing: getField('isSelfClosing')?.toBoolValue(),
);
}
if (type?.isXmlAnnotationXmlRootElement == true) {
return XmlRootElement(
name: getField('name')?.toStringValue(),
namespace: getField('namespace')?.toStringValue(),
isSelfClosing: getField('isSelfClosing')?.toBoolValue(),
);
}

return null;
}

/// Returns an [XmlSerializable] corresponding to the value of the object being represented or `null` if this object is not of type [XmlSerializable].
XmlSerializable? toXmlSerializableValue() {
final type = this.type;

if (type is InterfaceType) {
final element = type.element;

if (element is ClassElement &&
element.library.identifier.startsWith('package:xml_annotation') &&
element.name == 'XmlSerializable') {
return XmlSerializable(
createMixin: getField('createMixin')?.toBoolValue(),
fieldRename: getField('fieldRename')?.toFieldRenameValue(),
);
}
if (type?.isXmlAnnotationXmlSerializable == true) {
return XmlSerializable(
createMixin: getField('createMixin')?.toBoolValue(),
fieldRename: getField('fieldRename')?.toFieldRenameValue(),
);
}

return null;
}

/// Returns an [XmlText] corresponding to the value of the object being represented or `null` if this object is not of type [XmlText].
XmlText? toXmlTextValue() {
final type = this.type;

if (type is InterfaceType) {
final element = type.element;

if (element is ClassElement &&
element.library.identifier.startsWith('package:xml_annotation') &&
element.name == 'XmlText') {
return XmlText();
}
if (type?.isXmlAnnotationXmlText == true) {
return XmlText();
}

return null;
}

/// Returns an [XmlValue] corresponding to the value of the object being represented or `null` if this object is not of type [XmlValue].
XmlValue? toXmlValueValue() {
final type = this.type;

if (type is InterfaceType) {
final element = type.element;

if (element is ClassElement &&
element.library.identifier.startsWith('package:xml_annotation') &&
element.name == 'XmlValue') {
return XmlValue(getField('value')!.toStringValue()!);
}
if (type?.isXmlAnnotationXmlValue == true) {
return XmlValue(getField('value')!.toStringValue()!);
}

return null;
Expand Down
5 changes: 5 additions & 0 deletions xml_serializable/lib/src/extensions/dart_type_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ extension DartTypeExtensions on DartType {
bool get isNullable =>
this is DynamicType || nullabilitySuffix == NullabilitySuffix.question;

/// Returns `true` if this type represents the type 'FieldRename' defined in the xml_annotation library.
bool get isXmlAnnotationFieldRename =>
element?.library?.identifier == '$_prefix/xml_serializable.dart' &&
element?.name == 'FieldRename';

/// Returns `true` if this type represents the type 'XmlAttribute' defined in the xml_annotation library.
bool get isXmlAnnotationXmlAttribute =>
element?.library?.identifier == '$_prefix/xml_attribute.dart' &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ extension ElementExtensions on Element {
/// ```
Element? getXmlValueElement() {
for (final annotation in metadata) {
if (annotation.isXmlCDATA) {
if (annotation.isXmlValue) {
final element = annotation.element;
if (element is ConstructorElement) {
return element.enclosingElement;
Expand Down
Loading