Skip to content

Commit

Permalink
feat(nextcloud)!: webdav deserialize duration fields
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
  • Loading branch information
Leptopoda committed Apr 3, 2024
1 parent d49c27c commit d10b22a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
6 changes: 5 additions & 1 deletion packages/nextcloud/generate_props.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void main() {
'lock-owner-editor': 'String',
'lock-owner-type': 'int',
'lock-time': 'unixEpoch',
'lock-timeout': 'int',
'lock-timeout': 'seconds',
'lock-token': 'String',
'metadata_etag': 'String',
'mount-type': 'String',
Expand Down Expand Up @@ -102,6 +102,9 @@ void main() {
case 'unixEpoch':
value.writeln(' @UnixEpochXMLConverter()');
type = 'tz.TZDateTime';
case 'seconds':
value.writeln(' @DurationXMLConverter()');
type = 'Duration';
}

value.write(' final $type? $variable;');
Expand All @@ -124,6 +127,7 @@ void main() {
'// coverage:ignore-file',
"import 'package:meta/meta.dart';",
"import 'package:nextcloud/src/utils/date_time.dart';",
"import 'package:nextcloud/src/webdav/utils.dart';",
"import 'package:nextcloud/src/webdav/webdav.dart';",
"import 'package:timezone/timezone.dart' as tz;",
"import 'package:xml/xml.dart';",
Expand Down
7 changes: 5 additions & 2 deletions packages/nextcloud/lib/src/webdav/props.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// coverage:ignore-file
import 'package:meta/meta.dart';
import 'package:nextcloud/src/utils/date_time.dart';
import 'package:nextcloud/src/webdav/utils.dart';
import 'package:nextcloud/src/webdav/webdav.dart';
import 'package:timezone/timezone.dart' as tz;
import 'package:xml/xml.dart';
Expand Down Expand Up @@ -938,7 +939,8 @@ class WebDavProp with _$WebDavPropXmlSerializableMixin {
namespace: namespaceNextcloud,
includeIfNull: false,
)
final int? ncLockTimeout;
@DurationXMLConverter()
final Duration? ncLockTimeout;

@annotation.XmlElement(
name: 'lock-token',
Expand Down Expand Up @@ -1418,7 +1420,8 @@ class WebDavOcFilterRules with _$WebDavOcFilterRulesXmlSerializableMixin {
namespace: namespaceNextcloud,
includeIfNull: false,
)
final int? ncLockTimeout;
@DurationXMLConverter()
final Duration? ncLockTimeout;

@annotation.XmlElement(
name: 'lock-token',
Expand Down
28 changes: 16 additions & 12 deletions packages/nextcloud/lib/src/webdav/props.g.dart

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

58 changes: 58 additions & 0 deletions packages/nextcloud/lib/src/webdav/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'package:meta/meta.dart';
import 'package:nextcloud/src/webdav/models.dart';
import 'package:nextcloud/src/webdav/path_uri.dart';
import 'package:nextcloud/src/webdav/webdav.dart';
import 'package:xml/xml.dart' as xml;
import 'package:xml_annotation/xml_annotation.dart' as xml_annotation;

/// Base path used on the server
final webdavBase = PathUri.parse('/remote.php/webdav');
Expand Down Expand Up @@ -47,3 +49,59 @@ final class WebDavResponseConverter with Converter<http.StreamedResponse, Future
return WebDavMultistatus.fromXmlElement(xml!);
}
}

@internal
final class DurationXMLConverter implements xml_annotation.XmlConverter<Duration?> {
const DurationXMLConverter();

@override
void buildXmlChildren(
Duration? instance,
xml.XmlBuilder builder, {
Map<String, String> namespaces = const {},
}) {
if (instance == null) {
return;
}

final serialized = instance.inSeconds.toString();
builder.text(serialized);
}

@override
Duration? fromXmlElement(
xml.XmlElement element,
) {
final value = element.getText();

if (value != null) {
final seconds = int.parse(value);
return Duration(seconds: seconds);
}

return null;
}

@override
List<xml.XmlAttribute> toXmlAttributes(
Duration? instance, {
Map<String, String?> namespaces = const {},
}) {
return const <xml.XmlAttribute>[];
}

@override
List<xml.XmlNode> toXmlChildren(
Duration? instance, {
Map<String, String?> namespaces = const {},
}) {
if (instance == null) {
return const <xml.XmlNode>[];
}

final serialized = instance.inSeconds.toString();
return <xml.XmlNode>[
xml.XmlText(serialized),
];
}
}
2 changes: 1 addition & 1 deletion packages/nextcloud/lib/webdav.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export 'src/webdav/file.dart';
export 'src/webdav/models.dart';
export 'src/webdav/path_uri.dart';
export 'src/webdav/props.dart';
export 'src/webdav/utils.dart' hide constructUri;
export 'src/webdav/utils.dart' hide DurationXMLConverter, constructUri;
export 'src/webdav/webdav.dart';

// ignore: public_member_api_docs
Expand Down

0 comments on commit d10b22a

Please sign in to comment.