-
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.
feat(scale utils): extract some reused SCALE en/decoding logic into u…
…tils
- Loading branch information
1 parent
b5b3aef
commit 61b00d4
Showing
3 changed files
with
106 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.limechain.utils.scale; | ||
|
||
import com.limechain.utils.scale.writers.PairWriter; | ||
import io.emeraldpay.polkaj.scale.ScaleCodecReader; | ||
import io.emeraldpay.polkaj.scale.ScaleCodecWriter; | ||
import io.emeraldpay.polkaj.scale.ScaleReader; | ||
import io.emeraldpay.polkaj.scale.reader.ListReader; | ||
import io.emeraldpay.polkaj.scale.writer.ListWriter; | ||
import kotlin.Pair; | ||
import lombok.experimental.UtilityClass; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
// TODO: This class is currently just a helper utility class, planned to grow into a unified scale encode/decode util class with whatever methods are useful | ||
// WIP | ||
// Currently trying out different approaches to spare some of the boilerplate around SCALE en/decoding | ||
@UtilityClass | ||
public class ScaleUtils { | ||
|
||
@UtilityClass | ||
public class Decode { | ||
public <T> T decode(byte[] encodedData, ScaleReader<T> reader) { | ||
try { | ||
return new ScaleCodecReader(encodedData).read(reader); | ||
} catch (RuntimeException e) { | ||
throw new RuntimeException("Error while SCALE decoding.", e); // TODO: is this a code smell? | ||
} | ||
} | ||
|
||
public <T> List<T> decodeList(byte[] encodedData, ScaleReader<T> listItemReader) { | ||
return decode(encodedData, new ListReader<>(listItemReader)); | ||
} | ||
} | ||
|
||
@UtilityClass | ||
public class Encode { | ||
public <K, V> byte[] encode(List<Pair<K, V>> pairs, Function<K, byte[]> fstSerializer, Function<V, byte[]> sndSerializer) throws | ||
IOException { | ||
return encode( | ||
pairs.stream() | ||
.map(p -> new Pair<>(fstSerializer.apply(p.getFirst()), sndSerializer.apply(p.getSecond()))) | ||
.toList()); | ||
} | ||
|
||
public byte[] encode(List<Pair<byte[], byte[]>> pairs) throws IOException { | ||
try (ByteArrayOutputStream buffer = new ByteArrayOutputStream(); | ||
ScaleCodecWriter writer = new ScaleCodecWriter(buffer)) { | ||
new ListWriter<>(new PairWriter<>(ScaleCodecWriter::writeAsList, ScaleCodecWriter::writeAsList)) | ||
.write(writer, pairs); | ||
return buffer.toByteArray(); | ||
} | ||
} | ||
|
||
public byte[] encode(byte[][] values) throws IOException { | ||
try (ByteArrayOutputStream buffer = new ByteArrayOutputStream(); | ||
ScaleCodecWriter writer = new ScaleCodecWriter(buffer)) { | ||
new ListWriter<>(ScaleCodecWriter::writeAsList) | ||
.write(writer, Arrays.asList(values)); | ||
return buffer.toByteArray(); | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/limechain/utils/scale/readers/PairReader.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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.limechain.utils.scale.readers; | ||
|
||
import io.emeraldpay.polkaj.scale.ScaleCodecReader; | ||
import io.emeraldpay.polkaj.scale.ScaleReader; | ||
import kotlin.Pair; | ||
import lombok.AllArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
public class PairReader<K, V> implements ScaleReader<Pair<K, V>> { | ||
protected ScaleReader<K> firstReader; | ||
protected ScaleReader<V> secondReader; | ||
|
||
@Override | ||
public Pair<K, V> read(ScaleCodecReader scaleCodecReader) { | ||
K first = scaleCodecReader.read(firstReader); | ||
V second = scaleCodecReader.read(secondReader); | ||
return new Pair<>(first, second); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/limechain/utils/scale/writers/PairWriter.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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.limechain.utils.scale.writers; | ||
|
||
import io.emeraldpay.polkaj.scale.ScaleCodecWriter; | ||
import io.emeraldpay.polkaj.scale.ScaleWriter; | ||
import kotlin.Pair; | ||
import lombok.AllArgsConstructor; | ||
|
||
import java.io.IOException; | ||
|
||
@AllArgsConstructor | ||
public class PairWriter<K, V> implements ScaleWriter<Pair<K, V>> { | ||
protected ScaleWriter<K> firstWriter; | ||
protected ScaleWriter<V> secondWriter; | ||
|
||
@Override | ||
public void write(ScaleCodecWriter scaleCodecWriter, Pair<K, V> pair) throws IOException { | ||
scaleCodecWriter.write(firstWriter, pair.getFirst()); | ||
scaleCodecWriter.write(secondWriter, pair.getSecond()); | ||
} | ||
} |