-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major rework in order to support embedded properties and a protobuf s…
…erializer
- Loading branch information
Showing
25 changed files
with
568 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
name: Package & Deploy to GitHub | ||
|
||
on: | ||
workflow_dispatch: | ||
release: | ||
types: [created] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 52 additions & 17 deletions
69
cursorpaging-jpa-serial/src/main/java/io/vigier/cursorpaging/jpa/serial/Serializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,70 @@ | ||
package io.vigier.cursorpaging.jpa.serial; | ||
|
||
|
||
import io.vigier.cursor.Attribute; | ||
import io.vigier.cursor.PageRequest; | ||
import io.vigier.cursorpaging.jpa.serial.dto.DtoCursor.DtoPageRequest; | ||
import jakarta.persistence.metamodel.SingularAttribute; | ||
import java.util.Collection; | ||
import java.util.LinkedList; | ||
import lombok.RequiredArgsConstructor; | ||
import io.vigier.cursorpaging.jpa.serial.dto.Cursor; | ||
import java.util.Base64; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Consumer; | ||
import lombok.Builder; | ||
import lombok.SneakyThrows; | ||
|
||
@RequiredArgsConstructor( staticName = "of" ) | ||
public class Serializer<E> { | ||
@Builder | ||
public class Serializer { | ||
|
||
private final Class<E> entityType; | ||
private final Collection<SingularAttribute<E, ? extends Comparable<?>>> attributes = new LinkedList<>(); | ||
@Builder.Default | ||
private Map<String, Attribute> attributes = new ConcurrentHashMap<>(); | ||
|
||
private final Encrypter encrypter; | ||
@Builder.Default | ||
private final Encrypter encrypter = Encrypter.getInstance(); | ||
|
||
public Serializer<E> use( final SingularAttribute<E, ? extends Comparable<?>> attribute ) { | ||
attributes.add( attribute ); | ||
return this; | ||
public static class SerializerBuilder { | ||
|
||
public SerializerBuilder use( final Attribute attribute ) { | ||
if ( this.attributes$value == null ) { | ||
this.attributes$value = new ConcurrentHashMap<>(); | ||
} | ||
this.attributes$value.put( attribute.name(), attribute ); | ||
attributes$set = true; | ||
return this; | ||
} | ||
} | ||
|
||
public static Serializer create( final Consumer<SerializerBuilder> c ) { | ||
final var builder = builder(); | ||
c.accept( builder ); | ||
return builder.build(); | ||
} | ||
|
||
public byte[] toBytes( final PageRequest<E> page ) { | ||
final DtoPageRequest dtoRequest = ToDtoMapper.of( page ).map(); | ||
public static Serializer create() { | ||
return create( b -> { | ||
} ); | ||
} | ||
|
||
public <E> byte[] toBytes( final PageRequest<E> page ) { | ||
updateAttributes( page ); | ||
final Cursor.PageRequest dtoRequest = ToDtoMapper.of( page ).map(); | ||
return encrypter.encrypt( dtoRequest.toByteArray() ); | ||
} | ||
|
||
private void updateAttributes( final PageRequest<?> page ) { | ||
page.positions().forEach( p -> attributes.putIfAbsent( p.attribute().name(), p.attribute() ) ); | ||
page.filters().forEach( f -> attributes.putIfAbsent( f.attribute().name(), f.attribute() ) ); | ||
} | ||
|
||
public String toBase64( final PageRequest<?> page ) { | ||
return new String( Base64.getUrlEncoder().encode( toBytes( page ) ) ); | ||
} | ||
|
||
@SneakyThrows | ||
public PageRequest<E> toPageRequest( final byte[] data ) { | ||
final var request = DtoPageRequest.parseFrom( encrypter.decrypt( data ) ); | ||
public <E> PageRequest<E> toPageRequest( final byte[] data ) { | ||
final var request = Cursor.PageRequest.parseFrom( encrypter.decrypt( data ) ); | ||
return FromDtoMapper.<E>of( request ).using( attributes ).map(); | ||
} | ||
|
||
public <E> PageRequest<E> toPageRequest( final String base64 ) { | ||
return toPageRequest( Base64.getUrlDecoder().decode( base64 ) ); | ||
} | ||
} |
Oops, something went wrong.