Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
joanne-ter-maat committed Nov 14, 2024
1 parent 52d26a8 commit 77f2489
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 49 deletions.
24 changes: 16 additions & 8 deletions packages/kiota_abstractions/lib/src/api_client_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ class ApiClientBuilder {

static SerializationWriterFactory
enableBackingStoreForSerializationWriterFactory(
SerializationWriterFactory original,) {
SerializationWriterFactory original,
) {
var result = original;
if (original is SerializationWriterFactoryRegistry) {
_enableBackingStoreForSerializationRegistry(original);
if (original != SerializationWriterFactoryRegistry.defaultInstance) {
// if the registry is the default instance, we already enabled it above. No need to do it twice
_enableBackingStoreForSerializationRegistry(
SerializationWriterFactoryRegistry.defaultInstance,);
SerializationWriterFactoryRegistry.defaultInstance,
);
}
}
if (result is BackingStoreSerializationWriterProxyFactory) {
Expand All @@ -47,14 +49,16 @@ class ApiClientBuilder {
}

static ParseNodeFactory enableBackingStoreForParseNodeFactory(
ParseNodeFactory original,) {
ParseNodeFactory original,
) {
var result = original;
if (original is ParseNodeFactoryRegistry) {
_enableBackingStoreForParseNodeRegistry(original);
if (original != ParseNodeFactoryRegistry.defaultInstance) {
// if the registry is the default instance, we already enabled it above. No need to do it twice
_enableBackingStoreForParseNodeRegistry(
ParseNodeFactoryRegistry.defaultInstance,);
ParseNodeFactoryRegistry.defaultInstance,
);
}
}
if (result is BackingStoreParseNodeFactory) {
Expand All @@ -68,7 +72,8 @@ class ApiClientBuilder {
}

static void _enableBackingStoreForParseNodeRegistry(
ParseNodeFactoryRegistry registry,) {
ParseNodeFactoryRegistry registry,
) {
final keysToUpdate = <String>[];
registry.contentTypeAssociatedFactories.forEach((key, value) {
if (value is! BackingStoreParseNodeFactory) {
Expand All @@ -79,12 +84,14 @@ class ApiClientBuilder {
keysToUpdate.forEach((key) {
registry.contentTypeAssociatedFactories[key] =
BackingStoreParseNodeFactory(
concrete: registry.contentTypeAssociatedFactories[key]!,);
concrete: registry.contentTypeAssociatedFactories[key]!,
);
});
}

static void _enableBackingStoreForSerializationRegistry(
SerializationWriterFactoryRegistry registry,) {
SerializationWriterFactoryRegistry registry,
) {
final keysToUpdate = <String>[];
registry.contentTypeAssociatedFactories.forEach((key, value) {
{
Expand All @@ -97,7 +104,8 @@ class ApiClientBuilder {
keysToUpdate.forEach((key) {
registry.contentTypeAssociatedFactories[key] =
BackingStoreSerializationWriterProxyFactory(
concrete: registry.contentTypeAssociatedFactories[key]!,);
concrete: registry.contentTypeAssociatedFactories[key]!,
);
});
}
}
89 changes: 52 additions & 37 deletions packages/kiota_abstractions/test/api_client_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import 'package:test/test.dart';
import 'api_client_builder_test.mocks.dart';

@GenerateMocks(
[SerializationWriter, SerializationWriterFactory, ParseNodeFactory],)
[SerializationWriter, SerializationWriterFactory, ParseNodeFactory],
)
void main() {
const streamContentType = 'application/octet-stream';

Expand All @@ -16,20 +17,23 @@ void main() {
.putIfAbsent(streamContentType, () => mockSerializationWriterFactory);

expect(
serializationFactoryRegistry
.contentTypeAssociatedFactories[streamContentType]
is BackingStoreSerializationWriterProxyFactory,
isFalse,);
serializationFactoryRegistry
.contentTypeAssociatedFactories[streamContentType]
is BackingStoreSerializationWriterProxyFactory,
isFalse,
);

// Act
ApiClientBuilder.enableBackingStoreForSerializationWriterFactory(
serializationFactoryRegistry,);
serializationFactoryRegistry,
);

// Assert the type has changed due to backing store enabling
expect(
serializationFactoryRegistry
.contentTypeAssociatedFactories[streamContentType],
isA<BackingStoreSerializationWriterProxyFactory>(),);
serializationFactoryRegistry
.contentTypeAssociatedFactories[streamContentType],
isA<BackingStoreSerializationWriterProxyFactory>(),
);
});
test(
'enableBackingStoreForSerializationWriterFactoryAlsoEnablesForDefaultInstance',
Expand All @@ -43,43 +47,49 @@ void main() {
.putIfAbsent(streamContentType, () => mockSerializationWriterFactory);

expect(
serializationFactoryRegistry
.contentTypeAssociatedFactories[streamContentType]
is BackingStoreSerializationWriterProxyFactory,
isFalse,);
serializationFactoryRegistry
.contentTypeAssociatedFactories[streamContentType]
is BackingStoreSerializationWriterProxyFactory,
isFalse,
);

// Act
ApiClientBuilder.enableBackingStoreForSerializationWriterFactory(
serializationFactoryRegistry,);
serializationFactoryRegistry,
);

// Assert the type has changed due to backing store enabling
expect(
serializationFactoryRegistry
.contentTypeAssociatedFactories[streamContentType],
isA<BackingStoreSerializationWriterProxyFactory>(),);
serializationFactoryRegistry
.contentTypeAssociatedFactories[streamContentType],
isA<BackingStoreSerializationWriterProxyFactory>(),
);

expect(
SerializationWriterFactoryRegistry.defaultInstance
.contentTypeAssociatedFactories[streamContentType],
isA<BackingStoreSerializationWriterProxyFactory>(),);
SerializationWriterFactoryRegistry
.defaultInstance.contentTypeAssociatedFactories[streamContentType],
isA<BackingStoreSerializationWriterProxyFactory>(),
);
});
test('enableBackingStoreForParseNodeFactory', () {
final parseNodeRegistry = ParseNodeFactoryRegistry();
final mockParseNodeFactory = MockParseNodeFactory();
parseNodeRegistry.contentTypeAssociatedFactories
.putIfAbsent(streamContentType, () => mockParseNodeFactory);
expect(
parseNodeRegistry.contentTypeAssociatedFactories[streamContentType]
is BackingStoreParseNodeFactory,
isFalse,);
parseNodeRegistry.contentTypeAssociatedFactories[streamContentType]
is BackingStoreParseNodeFactory,
isFalse,
);

// Act
ApiClientBuilder.enableBackingStoreForParseNodeFactory(parseNodeRegistry);

// Assert the type has changed due to backing store enabling
expect(
parseNodeRegistry.contentTypeAssociatedFactories[streamContentType],
isA<BackingStoreParseNodeFactory>(),);
parseNodeRegistry.contentTypeAssociatedFactories[streamContentType],
isA<BackingStoreParseNodeFactory>(),
);
});

test('enableBackingStoreForParseNodeFactoryAlsoEnablesForDefaultInstance',
Expand All @@ -92,21 +102,24 @@ void main() {
.putIfAbsent(streamContentType, () => mockParseNodeFactory);

expect(
parseNodeRegistry.contentTypeAssociatedFactories[streamContentType]
is BackingStoreParseNodeFactory,
isFalse,);
parseNodeRegistry.contentTypeAssociatedFactories[streamContentType]
is BackingStoreParseNodeFactory,
isFalse,
);

// Act
ApiClientBuilder.enableBackingStoreForParseNodeFactory(parseNodeRegistry);

// Assert the type has changed due to backing store enabling for the default instance as well.
expect(
parseNodeRegistry.contentTypeAssociatedFactories[streamContentType],
isA<BackingStoreParseNodeFactory>(),);
parseNodeRegistry.contentTypeAssociatedFactories[streamContentType],
isA<BackingStoreParseNodeFactory>(),
);
expect(
ParseNodeFactoryRegistry.defaultInstance
.contentTypeAssociatedFactories[streamContentType],
isA<BackingStoreParseNodeFactory>(),);
ParseNodeFactoryRegistry
.defaultInstance.contentTypeAssociatedFactories[streamContentType],
isA<BackingStoreParseNodeFactory>(),
);
});
test(
'enableBackingStoreForParseNodeFactoryAlsoEnablesForDefaultInstanceMultipleCallsDoesNotDoubleWrap',
Expand All @@ -119,14 +132,16 @@ void main() {
.putIfAbsent(streamContentType, () => mockParseNodeFactory);

expect(
parseNodeRegistry.contentTypeAssociatedFactories[streamContentType]
is BackingStoreParseNodeFactory,
isFalse,);
parseNodeRegistry.contentTypeAssociatedFactories[streamContentType]
is BackingStoreParseNodeFactory,
isFalse,
);

// Act
final firstResult =
ApiClientBuilder.enableBackingStoreForParseNodeFactory(
parseNodeRegistry,);
parseNodeRegistry,
);
final secondResult =
ApiClientBuilder.enableBackingStoreForParseNodeFactory(firstResult);
final thirdResult =
Expand Down
3 changes: 2 additions & 1 deletion packages/kiota_http/lib/src/http_client_request_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ class HttpClientRequestAdapter implements RequestAdapter {
ApiClientBuilder.enableBackingStoreForParseNodeFactory(_pNodeFactory);
_sWriterFactory =
ApiClientBuilder.enableBackingStoreForSerializationWriterFactory(
_sWriterFactory,);
_sWriterFactory,
);
BackingStoreFactorySingleton.instance = backingStoreFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ void main() {
'someNull': const UntypedNull(),
'untypedArray': const UntypedArray(untypedNodesList),
'object': const UntypedObject(
{'name': UntypedString('a name'), 'age': UntypedInteger(8)},),
{'name': UntypedString('a name'), 'age': UntypedInteger(8)},
),
};

final writer = JsonSerializationWriter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class MultipartSerializationWriter implements SerializationWriter {
@override
void writeCollectionOfPrimitiveValues<T>(String? key, Iterable<T>? values) {
throw UnsupportedError(
'$errorMessagePrefix collection of primitive values',);
'$errorMessagePrefix collection of primitive values',
);
}

@override
Expand Down Expand Up @@ -116,7 +117,8 @@ class MultipartSerializationWriter implements SerializationWriter {
value.serialize(this);
} else {
throw Exception(
'Expected MultipartBody instance but got ${value.runtimeType}',);
'Expected MultipartBody instance but got ${value.runtimeType}',
);
}
if (onAfterObjectSerialization != null) {
onAfterObjectSerialization?.call(value);
Expand Down

0 comments on commit 77f2489

Please sign in to comment.