Skip to content

Commit

Permalink
adding support for DayOfWeek data type - pgjdbcgh-591
Browse files Browse the repository at this point in the history
  • Loading branch information
steverigney committed Aug 13, 2023
1 parent f77d357 commit da5a689
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/main/java/io/r2dbc/postgresql/codec/DayOfWeekCodec.java
@@ -0,0 +1,28 @@
/*
* Copyright 2017 the original author or 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.r2dbc.postgresql.codec;

import io.netty.buffer.ByteBufAllocator;

import java.time.DayOfWeek;

final class DayOfWeekCodec extends IntegerCodecDelegate<DayOfWeek> {

DayOfWeekCodec(ByteBufAllocator byteBufAllocator) {
super(DayOfWeek.class, byteBufAllocator, DayOfWeek::getValue, DayOfWeek::of);
}
}
1 change: 1 addition & 0 deletions src/main/java/io/r2dbc/postgresql/codec/DefaultCodecs.java
Expand Up @@ -244,6 +244,7 @@ private static List<Codec<?>> getDefaultCodecs(ByteBufAllocator byteBufAllocator
new BigIntegerCodec(byteBufAllocator),
new BooleanCodec(byteBufAllocator),
new CharacterCodec(byteBufAllocator),
new DayOfWeekCodec(byteBufAllocator),
new DoubleCodec(byteBufAllocator),
new FloatCodec(byteBufAllocator),
new InetAddressCodec(byteBufAllocator),
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/io/r2dbc/postgresql/codec/IntegerCodecDelegate.java
@@ -0,0 +1,73 @@
/*
* Copyright 2017 the original author or 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.r2dbc.postgresql.codec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.r2dbc.postgresql.client.EncodedParameter;
import io.r2dbc.postgresql.message.Format;
import io.r2dbc.postgresql.util.Assert;

import java.util.function.Function;

class IntegerCodecDelegate<T> extends AbstractCodec<T> {

private final IntegerCodec delegate;
private final Function<T, Integer> toIntegerConverter;
private final Function<Integer, T> fromIntegerConverter;

IntegerCodecDelegate(Class<T> type, ByteBufAllocator byteBufAllocator, Function<T,Integer> toIntegerConverter, Function<Integer, T> fromIntegerConverter) {
super(type);
this.delegate = new IntegerCodec(byteBufAllocator);
this.toIntegerConverter = toIntegerConverter;
this.fromIntegerConverter = fromIntegerConverter;
}

@Override
boolean doCanDecode(PostgresqlObjectId type, Format format) {
return delegate.doCanDecode(type, format);
}

@Override
T doDecode(ByteBuf buffer, PostgresTypeIdentifier dataType, Format format, Class<? extends T> type) {
final Integer number = delegate.doDecode(buffer, dataType, format, Integer.TYPE);
return fromIntegerConverter.apply(number);
}

@Override
EncodedParameter doEncode(T value) {
Assert.requireNonNull(value, "value must not be null");
return delegate.doEncode(toIntegerConverter.apply(value));
}

@Override
EncodedParameter doEncode(T value, PostgresTypeIdentifier dataType) {
Assert.requireNonNull(value, "value must not be null");
Assert.requireNonNull(dataType, "dataType must not be null");
return delegate.doEncode(toIntegerConverter.apply(value), dataType);
}

@Override
public Iterable<? extends PostgresTypeIdentifier> getDataTypes() {
return delegate.getDataTypes();
}

@Override
public EncodedParameter encodeNull() {
return delegate.encodeNull();
}
}
108 changes: 108 additions & 0 deletions src/test/java/io/r2dbc/postgresql/codec/DayOfWeekCodecTest.java
@@ -0,0 +1,108 @@
package io.r2dbc.postgresql.codec;

import io.r2dbc.postgresql.client.EncodedParameter;
import io.r2dbc.postgresql.client.ParameterAssert;
import org.junit.jupiter.api.Test;

import java.time.DayOfWeek;
import java.util.Arrays;
import java.util.function.Consumer;

import static io.r2dbc.postgresql.client.EncodedParameter.NULL_VALUE;
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.*;
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

class DayOfWeekCodecTest {

@Test
void constructorNoByteBufAllocator() {
assertThatIllegalArgumentException().isThrownBy(() -> new DayOfWeekCodec(null))
.withMessage("byteBufAllocator must not be null");
}

@Test
void decode() {
forEveryDayOfWeek(d ->
assertThat(new DayOfWeekCodec(TEST).decode(TEST.buffer().writeInt(d.getValue()), INT4, FORMAT_BINARY, DayOfWeek.class)).isEqualTo(d));
}

@Test
void decodeNoByteBuf() {
assertThat(new DayOfWeekCodec(TEST).decode(null, INT4.getObjectId(), FORMAT_BINARY, DayOfWeek.class)).isNull();
}

@Test
void doCanDecode() {
DayOfWeekCodec codec = new DayOfWeekCodec(TEST);

assertThat(codec.doCanDecode(INT4, FORMAT_BINARY)).isTrue();
assertThat(codec.doCanDecode(INT2, FORMAT_BINARY)).isTrue();
assertThat(codec.doCanDecode(INT8, FORMAT_BINARY)).isTrue();
assertThat(codec.doCanDecode(NUMERIC, FORMAT_TEXT)).isTrue();
assertThat(codec.doCanDecode(VARCHAR, FORMAT_TEXT)).isFalse();
}

@Test
void doCanDecodeNoType() {
assertThatIllegalArgumentException().isThrownBy(() -> new DayOfWeekCodec(TEST).doCanDecode(null, FORMAT_BINARY))
.withMessage("type must not be null");
}

@Test
void doEncodeInt() {

forEveryDayOfWeek(d -> {
ParameterAssert.assertThat(new DayOfWeekCodec(TEST).doEncode(d))
.hasFormat(FORMAT_BINARY)
.hasType(INT4.getObjectId())
.hasValue(TEST.buffer().writeInt(d.getValue()));
});
}

@Test
void doEncodeShort() {
forEveryDayOfWeek(d -> {
ParameterAssert.assertThat(new DayOfWeekCodec(TEST).doEncode(d, INT2))
.hasFormat(FORMAT_BINARY)
.hasType(INT2.getObjectId())
.hasValue(TEST.buffer().writeShort(d.getValue()));
});
}

@Test
void doEncodeLong() {

forEveryDayOfWeek(d -> {
ParameterAssert.assertThat(new DayOfWeekCodec(TEST).doEncode(d, INT8))
.hasFormat(FORMAT_BINARY)
.hasType(INT8.getObjectId())
.hasValue(TEST.buffer().writeLong(d.getValue()));
});
}

@Test
void doEncodeNoValue() {
assertThatIllegalArgumentException().isThrownBy(() -> new DayOfWeekCodec(TEST).doEncode(null))
.withMessage("value must not be null");
}

@Test
void encodeItemNoValue() {
assertThatIllegalArgumentException().isThrownBy(() -> new DayOfWeekCodec(TEST).encode(null))
.withMessage("value must not be null");
}

@Test
void encodeNull() {
ParameterAssert.assertThat(new DayOfWeekCodec(TEST).encodeNull())
.isEqualTo(new EncodedParameter(FORMAT_BINARY, INT4.getObjectId(), NULL_VALUE));
}

private void forEveryDayOfWeek(Consumer<DayOfWeek> assertion) {
Arrays.stream(DayOfWeek.values()).forEach(assertion);
}
}

0 comments on commit da5a689

Please sign in to comment.