Access context information in transformer #442
-
Hi! Is there a way to read the current path/field index within the The concrete use-case is for decoding gRPC update messages with respect to a field mask: message UpdatePersonRequest {
string id = 1;
Person person = 2;
google.protobuf.FieldMask update_mask = 3;
}
message Person {
string the_name = 1;
int32 age = 2;
} case class PersonUpdate(
theName: UpdateOrIgnore[String],
age: UpdateOrIgnore[Int]
) A request like: {
"the_name": "Thomas",
"field_mask": [
"the_name"
]
} should result in: PersonUpdate(
Update("Thomas"),
Ignore
) In order to write a generic given [A]: Transformer[A, UpdateOrIgnore[A]] = ??? I primarily need the path of field indices to the field that's being transformed (since ScalaPB renames fields to camel case). For In summary, is there a way to get the indexed path of the current transformation? Alternatively, is there an easier solution to this? 🙂 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi, When it comes to reading the current field path, then currently Chimney does not provide such a feature - mostly because this error-path is built in the runtime, e.g. when we are building error paths in case class Foo(bar: Bar)
case class Bar(a: String)
case class Baz(bar: Bar2)
case class Bar2(a: Int)
implicit val bars: PartialTransformer[Bar, Bar2] = PartialTransformer { bar =>
partial.Result
.fromCatching(bar.a.toInt)
.prependErrorPath(partial.PathElement.Accessor("a"))
.map(a => Bar2(a))
}
implicit val fooBar: PartialTransformer[Foo, Baz] = PartialTransformrt { foo =>
bars.transform(foo.bar)
.prependErrorPath(partial.PathElement.Accessor("bar"))
.map(bar => Baz(bar))
} then when we would like to get the full path to the To make that path available we would have to change the way it is build:
so while it it possible, at this point it would be quite a lot of work to make it possible. However, you in you used
Your use case seem to be quite exotic, I can only guess it's about PB treating integers and strings as non-nullable, and you having to manually determine if this empty value means "reset" or "ignore". I'm afraid this cannot be done OOTB with Chimney alone, and I would implement it by:
If you are using
but such a code would not be completely autoderived, it would have to be semiautomatically derived for inputs which have |
Beta Was this translation helpful? Give feedback.
Hi,
When it comes to reading the current field path, then currently Chimney does not provide such a feature - mostly because this error-path is built in the runtime, e.g. when we are building error paths in
PartialTransformer
s: