Skip to content

Commit

Permalink
Merge branch 'main' into issues/1-missing-data-types
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss authored Feb 12, 2024
2 parents 9cf0bb4 + 038091e commit 3fb40d1
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/kiota_abstractions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ part 'src/response_handler.dart';
part 'src/response_handler_option.dart';
part 'src/serialization/parsable.dart';
part 'src/serialization/parsable_factory.dart';
part 'src/serialization/parsable_hook.dart';
part 'src/serialization/parse_node.dart';
part 'src/serialization/parse_node_factory.dart';
part 'src/serialization/parse_node_factory_registry.dart';
part 'src/serialization/parse_node_proxy_factory.dart';
part 'src/serialization/serialization_writer.dart';
part 'src/serialization/serialization_writer_factory.dart';
part 'src/serialization/serialization_writer_factory_registry.dart';
part 'src/time_only.dart';
part 'src/serialization/serialization_writer_proxy_factory.dart';
7 changes: 7 additions & 0 deletions lib/src/serialization/parsable_hook.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
part of '../../kiota_abstractions.dart';

/// A hook that can be added before or after the parsing of a [Parsable] object.
///
/// See [ParseNode] or [ParseNodeProxyFactory] for abilities to add hooks to the
/// parsing of a [Parsable] object.
typedef ParsableHook = void Function(Parsable);
4 changes: 2 additions & 2 deletions lib/src/serialization/parse_node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ abstract class ParseNode {
Uint8List? getByteArrayValue();

/// Callback called before the node is deserialized.
void Function(Parsable)? onBeforeAssignFieldValues;
ParsableHook? onBeforeAssignFieldValues;

/// Callback called after the node is deserialized.
void Function(Parsable)? onAfterAssignFieldValues;
ParsableHook? onAfterAssignFieldValues;
}
40 changes: 40 additions & 0 deletions lib/src/serialization/parse_node_proxy_factory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
part of '../../kiota_abstractions.dart';

/// A factory that creates a proxy for a [ParseNodeFactory] that allows for
/// hooks to be added before and after the parsing of a [Parsable] object.
abstract class ParseNodeProxyFactory implements ParseNodeFactory {
/// Creates a new instance of the [ParseNodeProxyFactory] class.
ParseNodeProxyFactory({
required ParseNodeFactory concrete,
ParsableHook? onBefore,
ParsableHook? onAfter,
}) : _concrete = concrete,
_onBefore = onBefore,
_onAfter = onAfter;

final ParseNodeFactory _concrete;
final ParsableHook? _onBefore;
final ParsableHook? _onAfter;

@override
String get validContentType => _concrete.validContentType;

@override
ParseNode getRootParseNode(String contentType, Uint8List content) {
final node = _concrete.getRootParseNode(contentType, content);

final originalBefore = node.onBeforeAssignFieldValues;
node.onBeforeAssignFieldValues = (parsable) {
originalBefore?.call(parsable);
_onBefore?.call(parsable);
};

final originalAfter = node.onAfterAssignFieldValues;
node.onAfterAssignFieldValues = (parsable) {
originalAfter?.call(parsable);
_onAfter?.call(parsable);
};

return node;
}
}
4 changes: 2 additions & 2 deletions lib/src/serialization/serialization_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ abstract class SerializationWriter {
Uint8List getSerializedContent();

/// Callback called before the serialization process starts.
void Function(Parsable)? onBeforeObjectSerialization;
ParsableHook? onBeforeObjectSerialization;

/// Callback called after the serialization process ends.
void Function(Parsable)? onAfterObjectSerialization;
ParsableHook? onAfterObjectSerialization;

/// Callback called right after the serialization process starts.
void Function(Parsable, SerializationWriter)? onStartObjectSerialization;
Expand Down
47 changes: 47 additions & 0 deletions lib/src/serialization/serialization_writer_proxy_factory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
part of '../../kiota_abstractions.dart';

abstract class SerializationWriterProxyFactory
implements SerializationWriterFactory {
SerializationWriterProxyFactory({
required SerializationWriterFactory concrete,
ParsableHook? onBefore,
ParsableHook? onAfter,
void Function(Parsable, SerializationWriter)? onStart,
}) : _concrete = concrete,
_onBefore = onBefore,
_onAfter = onAfter,
_onStart = onStart;

final SerializationWriterFactory _concrete;
final ParsableHook? _onBefore;
final ParsableHook? _onAfter;
final void Function(Parsable, SerializationWriter)? _onStart;

@override
String get validContentType => _concrete.validContentType;

@override
SerializationWriter getSerializationWriter(String contentType) {
final writer = _concrete.getSerializationWriter(contentType);

final originalBefore = writer.onBeforeObjectSerialization;
writer.onBeforeObjectSerialization = (p) {
_onBefore?.call(p);
originalBefore?.call(p);
};

final originalAfter = writer.onAfterObjectSerialization;
writer.onAfterObjectSerialization = (p) {
_onAfter?.call(p);
originalAfter?.call(p);
};

final originalStart = writer.onStartObjectSerialization;
writer.onStartObjectSerialization = (p, w) {
_onStart?.call(p, w);
originalStart?.call(p, w);
};

return writer;
}
}

0 comments on commit 3fb40d1

Please sign in to comment.