Skip to content

Instantly share code, notes, and snippets.

View hhalex's full-sized avatar

Alexandre Careil hhalex

View GitHub Profile
@hhalex
hhalex / xpath_helper.rs
Created December 15, 2023 14:40
Handlebars rust template helper to execute xpath queries against xml documents
use handlebars::{
to_json, BlockContext, BlockParams, Context, Handlebars, Helper, HelperDef, HelperResult,
Output, PathAndJson, RenderContext, RenderError, RenderErrorReason, Renderable,
};
use serde_json::value::Value as Json;
use std::collections::HashMap;
use std::fmt::format;
use sxd_document::parser;
use sxd_xpath::evaluate_xpath;
use sxd_xpath::nodeset::Node;
@hhalex
hhalex / monficher.scala
Created March 24, 2020 11:03
Le code de Ul
.evalMap(req => {
println(req.headers)
println(req)
httpClientBuilder.resource
.map(Metrics(Prometheus(registry, "client").unsafeRunSync(), requestMethodClassifier)(_))
.use(.expect[String](req)) //.map(.withContentType(Content-Type.apply(MediaType.text)))
})
@hhalex
hhalex / CustomMappingCirceDecoder.scala
Created November 22, 2019 16:23
Case Class decoder, with custom mappings, with Circe
implicit val config: Configuration = Configuration.default
implicit val eventTypeDecoder = ProtobufEnumDecoder.create(PointerEvent.forNumber)
@ConfiguredJsonCodec(decodeOnly = true) case class SingleEvent(
@JsonKey("ts") timestamp: Long,
@JsonKey("x") x: Float,
@JsonKey("y") y: Float,
@JsonKey("pe") pointerEvent: PointerEvent
)
@hhalex
hhalex / FullCirceDecoder.scala
Created November 22, 2019 16:21
Final Circe Decoder
implicit val singleEventDecoder = deriveDecoder[SingleEvent]
implicit val eventTypeDecoder = ProtobufEnumDecoder.create(PointerEvent.forNumber)
case class SingleEvent(
timestamp: Long,
x: Float,
y: Float,
pointerEvent: PointerEvent
)
@hhalex
hhalex / ProtobufEnumDecoder.scala
Created November 22, 2019 16:20
Generating decoders for protobuf enums
object ProtobufEnumDecoder {
def create[T](decodingFunction: Int => T)(implicit manifest: Manifest[T]) = new Decoder[T] {
private val enumName = manifest.runtimeClass.getSimpleName
override def apply(c: HCursor): Result[T] =
c.value.as[Int].right flatMap { i =>
val enum = decodingFunction(i)
if (enum == null)
Left(DecodingFailure(s"$i is not a valid value for this enum '$enumName'", Nil))
else Right(enum)
}
@hhalex
hhalex / CirceSemiAutomatic.scala
Created November 22, 2019 16:18
Declaring a decoder for semi-automatic derivation
case class CustomStructure(a: Int)
object MyDecoders {
implicit customDecoder = deriveDecoder[CustomStructure]
}
object Main {
import MyDecoders._
val decoded: CustomStructure = decode[CustomStructure]("{a:1}")
}
@hhalex
hhalex / EnumFieldWrapperExample.scala
Created November 22, 2019 16:15
A wrapper to deserialize protobuf enums with Jackson
/*
* Class used to generically deserialize Protobuf enums in Jackson
* The exception thrown is caught by Jackson, and error messages belong to the same layer
* as the other json deserialization errors.
*/
class EnumFieldWrapper[T <: ProtocolMessageEnum](forNumber: Int => T, underlying: Int) {
val underlyingEnum = {
val enum = forNumber(underlying)
if (enum == null)
throw new Exception(s"$underlying is not a valid value for this enum")
@hhalex
hhalex / RawPointerEvent.scala
Last active November 22, 2019 16:12
A basic deserializer with Jackson/Finagle
case class RawPointerEvent(
@JsonProperty("ts") timestamp: Int,
@JsonProperty("x") x: Int,
@JsonProperty("y") y: Int,
@JsonProperty("pe") pointerEvent: Int //Protobuf enum
)