Skip to content

Commit

Permalink
Convert internal serdes from beans to ordinary classes (#761)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov authored Feb 8, 2024
1 parent 68e063e commit 6489c8e
Show file tree
Hide file tree
Showing 94 changed files with 3,616 additions and 1,964 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package io.micronaut.serde.bson
import io.micronaut.core.type.Argument
import io.micronaut.json.JsonMapper
import io.micronaut.serde.AbstractBasicSerdeCompileSpec
import io.micronaut.serde.AbstractBasicSerdeSpec
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import org.bson.BsonDocument

class BsonBinaryBasicSerdeCompileSpec extends AbstractBasicSerdeCompileSpec implements BsonBinarySpec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micronaut.serde.oracle.jdbc.json.serde;

import io.micronaut.context.annotation.Secondary;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Order;
import io.micronaut.core.type.Argument;
Expand All @@ -35,9 +36,9 @@
@Singleton
@Order(-100)
public class OracleJsonBinarySerde extends AbstractOracleJsonSerde<byte[]> {
private final DefaultSerdeRegistry.ByteArraySerde byteArraySerde;
private final Serde<byte[]> byteArraySerde;

public OracleJsonBinarySerde(DefaultSerdeRegistry.ByteArraySerde byteArraySerde) {
public OracleJsonBinarySerde(@Secondary Serde<byte[]> byteArraySerde) {
this.byteArraySerde = byteArraySerde;
}

Expand Down
1,670 changes: 371 additions & 1,299 deletions serde-support/src/main/java/io/micronaut/serde/support/DefaultSerdeRegistry.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.serde.support;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.order.Ordered;
import io.micronaut.core.type.Argument;
import io.micronaut.serde.Deserializer;

import java.util.Collections;

/**
* The registrar of {@link Deserializer}.
* @param <T> The serde type
*/
@Internal
public interface DeserializerRegistrar<T> extends Deserializer<T>, Ordered {

/**
* @return The serde argument type
*/
@NonNull
Argument<T> getType();

/**
* @return The multiple serde argument types
*/
@NonNull
default Iterable<Argument<?>> getTypes() {
return Collections.singleton(getType());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright 2017-2021 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.serde.support;

import io.micronaut.context.BeanContext;
import io.micronaut.context.BeanProvider;
import io.micronaut.context.annotation.Any;
import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Prototype;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.type.Argument;
import io.micronaut.inject.ArgumentInjectionPoint;
import io.micronaut.inject.InjectionPoint;
import io.micronaut.serde.Serde;
import io.micronaut.serde.SerdeIntrospections;
import io.micronaut.serde.config.DeserializationConfiguration;
import io.micronaut.serde.config.SerdeConfiguration;
import io.micronaut.serde.config.SerializationConfiguration;
import io.micronaut.serde.support.deserializers.ObjectDeserializer;
import io.micronaut.serde.support.deserializers.SerdeDeserializationPreInstantiateCallback;
import io.micronaut.serde.support.serdes.InetAddressSerde;
import io.micronaut.serde.support.serdes.InstantSerde;
import io.micronaut.serde.support.serdes.LocalDateSerde;
import io.micronaut.serde.support.serdes.LocalDateTimeSerde;
import io.micronaut.serde.support.serdes.LocalTimeSerde;
import io.micronaut.serde.support.serdes.ObjectArraySerde;
import io.micronaut.serde.support.serdes.OffsetDateTimeSerde;
import io.micronaut.serde.support.serdes.YearSerde;
import io.micronaut.serde.support.serdes.ZonedDateTimeSerde;
import io.micronaut.serde.support.serializers.ObjectSerializer;
import jakarta.annotation.Nullable;
import jakarta.inject.Singleton;

/**
* Factory for the legacy beans removed from the bean context.
*/
@Internal
@Factory
@BootstrapContextCompatible
final class LegacyBeansFactory {

@Any
@Prototype
@BootstrapContextCompatible
<S extends Serde<T>, T> S provideSerde(InjectionPoint<Serde<T>> serdeInjectionPoint,
BeanProvider<DefaultSerdeRegistry> serdeRegistry) {
if (serdeInjectionPoint instanceof ArgumentInjectionPoint<?, ?> argumentInjectionPoint) {
Argument typeParameter = argumentInjectionPoint.getArgument().getTypeParameters()[0];
if (typeParameter.getType() == Object[].class) {
return (S) new ObjectArraySerde();
}
DefaultSerdeRegistry defaultSerdeRegistry = serdeRegistry.get();
return (S) defaultSerdeRegistry.findInternalSerde(typeParameter);
}
return null;
}

@Singleton
@BootstrapContextCompatible
ObjectSerializer provideObjectSerializer(BeanContext beanContext,
SerdeIntrospections introspections,
SerdeConfiguration serdeConfiguration,
SerializationConfiguration serializationConfiguration) {

return new ObjectSerializer(
introspections,
serdeConfiguration,
serializationConfiguration,
beanContext);
}

@Singleton
@BootstrapContextCompatible
ObjectDeserializer provideObjectDeserializer(SerdeIntrospections introspections,
SerdeConfiguration serdeConfiguration,
DeserializationConfiguration deserializationConfiguration,
@Nullable SerdeDeserializationPreInstantiateCallback instantiateCallback) {
return new ObjectDeserializer(introspections,
deserializationConfiguration,
serdeConfiguration,
instantiateCallback
);
}

@Singleton
@BootstrapContextCompatible
ObjectArraySerde provideObjectArraySerde() {
return new ObjectArraySerde();
}

@Singleton
@BootstrapContextCompatible
InetAddressSerde inetAddressSerde(SerdeConfiguration serdeConfiguration) {
return new InetAddressSerde(serdeConfiguration);
}

@Singleton
@BootstrapContextCompatible
InstantSerde instantSerde(SerdeConfiguration serdeConfiguration) {
return new InstantSerde(serdeConfiguration);
}

@Singleton
@BootstrapContextCompatible
LocalDateSerde localDateSerde(SerdeConfiguration serdeConfiguration) {
return new LocalDateSerde(serdeConfiguration);
}

@Singleton
@BootstrapContextCompatible
LocalDateTimeSerde localDateTimeSerde(SerdeConfiguration serdeConfiguration) {
return new LocalDateTimeSerde(serdeConfiguration);
}

@Singleton
@BootstrapContextCompatible
LocalTimeSerde localTimeSerde(SerdeConfiguration serdeConfiguration) {
return new LocalTimeSerde(serdeConfiguration);
}

@Singleton
@BootstrapContextCompatible
OffsetDateTimeSerde offsetDateTimeSerde(SerdeConfiguration serdeConfiguration) {
return new OffsetDateTimeSerde(serdeConfiguration);
}

@Singleton
@BootstrapContextCompatible
YearSerde yearSerde() {
return new YearSerde();
}

@Singleton
@BootstrapContextCompatible
ZonedDateTimeSerde zonedDateTimeSerde(SerdeConfiguration serdeConfiguration) {
return new ZonedDateTimeSerde(serdeConfiguration);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.serde.support;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.type.Argument;
import io.micronaut.serde.Serde;

import java.util.Collections;

/**
* The registrar of {@link Serde}.
* @param <T> The serde type
*/
@Internal
public interface SerdeRegistrar<T> extends Serde<T>, SerializerRegistrar<T>, DeserializerRegistrar<T> {

/**
* @return The serde argument type
*/
@NonNull
Argument<T> getType();

/**
* @return The multiple serde argument types
*/
@NonNull
default Iterable<Argument<?>> getTypes() {
return Collections.singleton(getType());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.serde.support;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.order.Ordered;
import io.micronaut.core.type.Argument;
import io.micronaut.serde.Serializer;

import java.util.Collections;

/**
* The registrar of {@link Serializer}.
* @param <T> The serde type
*/
@Internal
public interface SerializerRegistrar<T> extends Serializer<T>, Ordered {

/**
* @return The serde argument type
*/
@NonNull
Argument<T> getType();

/**
* @return The multiple serde argument types
*/
@NonNull
default Iterable<Argument<?>> getTypes() {
return Collections.singleton(getType());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micronaut.serde.support.deserializers;

import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.type.Argument;
Expand All @@ -31,6 +32,7 @@
import java.io.IOException;
import java.util.Map;

@Internal
@Singleton
@Requires(classes = HealthResult.class)
final class HealthResultDeserializer implements CustomizableDeserializer<HealthResult> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micronaut.serde.support.deserializers;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.reflect.exception.InstantiationException;
import io.micronaut.core.type.Argument;
Expand All @@ -30,6 +31,7 @@
* @author Denis Stepanov
* @since 2.5.0
*/
@Internal
final class JsonValueDeserializer implements Deserializer<Object> {
private final DeserBean<? super Object> deserBean;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/
package io.micronaut.serde.support.deserializers;

import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.context.annotation.Primary;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.beans.BeanIntrospection;
import io.micronaut.core.beans.exceptions.IntrospectionException;
Expand All @@ -32,7 +31,6 @@
import io.micronaut.serde.exceptions.SerdeException;
import io.micronaut.serde.support.util.SerdeArgumentConf;
import io.micronaut.serde.util.CustomizableDeserializer;
import jakarta.inject.Singleton;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -44,9 +42,7 @@
* @author graemerocher
* @since 1.0.0
*/
@Singleton
@Primary
@BootstrapContextCompatible
@Internal
public class ObjectDeserializer implements CustomizableDeserializer<Object>, DeserBeanRegistry {
private final SerdeIntrospections introspections;
private final Map<DeserBeanKey, Supplier<DeserBean<?>>> deserBeanMap = new ConcurrentHashMap<>(50);
Expand Down
Loading

0 comments on commit 6489c8e

Please sign in to comment.