Skip to content

Commit

Permalink
Refactor getting serialization writer in registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndiritu committed Oct 7, 2024
1 parent 81d3b24 commit 6a8b996
Showing 1 changed file with 30 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.microsoft.kiota.store.BackingStoreSerializationWriterProxyFactory;

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;

import java.util.HashMap;
import java.util.Objects;
Expand Down Expand Up @@ -34,62 +35,56 @@ public SerializationWriterFactoryRegistry() {

@Override
@Nonnull public SerializationWriter getSerializationWriter(@Nonnull final String contentType) {
Objects.requireNonNull(contentType, "parameter contentType cannot be null");
if (contentType.isEmpty()) {
throw new NullPointerException("contentType cannot be empty");
}
final ContentTypeWrapper contentTypeWrapper = new ContentTypeWrapper(contentType);
final SerializationWriterFactory serializationWriterFactory =
getSerializationWriterFactory(contentTypeWrapper);
return serializationWriterFactory.getSerializationWriter(
contentTypeWrapper.cleanedContentType);
return getSerializationWriter(contentType, true);
}

/**
* Get a Serialization Writer with backing store configured with serializeOnlyChangedValues
* @param contentType
* @param serializeOnlyChangedValues control backing store functionality
* @return the serialization writer
* @throws RuntimeException when no factory is found for content type
*/
@Nonnull public SerializationWriter getSerializationWriter(
@Nonnull final String contentType, final boolean serializeOnlyChangedValues) {
Objects.requireNonNull(contentType, "parameter contentType cannot be null");
if (contentType.isEmpty()) {
throw new NullPointerException("contentType cannot be empty");
}
String cleanedContentType = getVendorSpecificContentType(contentType);
SerializationWriterFactory factory = getSerializationWriterFactory(cleanedContentType);
if (factory == null) {
cleanedContentType = getCleanedVendorSpecificContentType(cleanedContentType);
factory =
getSerializationWriterFactory(
getCleanedVendorSpecificContentType(cleanedContentType));
if (factory == null) {
throw new RuntimeException(
"Content type "
+ contentType
+ " does not have a factory to be serialized");
}
}
if (!serializeOnlyChangedValues) {
final ContentTypeWrapper contentTypeWrapper = new ContentTypeWrapper(contentType);
final SerializationWriterFactory factory =
getSerializationWriterFactory(contentTypeWrapper);
if (factory instanceof BackingStoreSerializationWriterProxyFactory) {
return ((BackingStoreSerializationWriterProxyFactory) factory)
.getSerializationWriter(
contentTypeWrapper.cleanedContentType, serializeOnlyChangedValues);
.getSerializationWriter(cleanedContentType, serializeOnlyChangedValues);
}
}
return getSerializationWriter(contentType);
return factory.getSerializationWriter(cleanedContentType);
}

/**
* Gets a SerializationWriterFactory that is mapped to a cleaned content type string
* @param contentTypeWrapper wrapper object carrying initial content type and result of parsing it
* @return the serialization writer factory
* @throws RuntimeException when no mapped factory is found
* @param contentType wrapper object carrying initial content type and result of parsing it
* @return the serialization writer factory or null if no mapped factory is found
*/
@Nonnull private SerializationWriterFactory getSerializationWriterFactory(
@Nonnull final ContentTypeWrapper contentTypeWrapper) {
final String vendorSpecificContentType =
getVendorSpecificContentType(contentTypeWrapper.contentType);
if (contentTypeAssociatedFactories.containsKey(vendorSpecificContentType)) {
contentTypeWrapper.cleanedContentType = vendorSpecificContentType;
return contentTypeAssociatedFactories.get(contentTypeWrapper.cleanedContentType);
}
final String cleanedContentType =
getCleanedVendorSpecificContentType(vendorSpecificContentType);
if (contentTypeAssociatedFactories.containsKey(cleanedContentType)) {
contentTypeWrapper.cleanedContentType = cleanedContentType;
return contentTypeAssociatedFactories.get(contentTypeWrapper.cleanedContentType);
@Nullable private SerializationWriterFactory getSerializationWriterFactory(
@Nonnull final String contentType) {
if (contentTypeAssociatedFactories.containsKey(contentType)) {
return contentTypeAssociatedFactories.get(contentType);
}
throw new RuntimeException(
"Content type "
+ contentTypeWrapper.contentType
+ " does not have a factory to be serialized");
return null;
}

/**
Expand All @@ -113,17 +108,4 @@ public SerializationWriterFactoryRegistry() {
@Nonnull private String getCleanedVendorSpecificContentType(@Nonnull final String contentType) {
return contentTypeVendorCleanupPattern.matcher(contentType).replaceAll("");
}

/**
* Wrapper class to carry the cleaned version of content-type after parsing in multiple stages
*/
private static final class ContentTypeWrapper {
String contentType;
String cleanedContentType;

ContentTypeWrapper(@Nonnull final String contentType) {
this.contentType = contentType;
this.cleanedContentType = "";
}
}
}

0 comments on commit 6a8b996

Please sign in to comment.