Alternative names (aliases) for a protobuf field

838 views
Skip to first unread message

Charith Ellawala

unread,
Dec 15, 2021, 9:40:06 AM12/15/21
to CEL Go Discussion Forum
Hello,

I am wondering whether it is possible to create aliases for protobuf fields so that the CEL parser automatically treats an expression like `obj.non_existent_attribute` as `obj.actual_attribute` instead. 

The users of my application usually deal with the JSON representation of protobufs. One of the quirks arising from that is that a protobuf definition like `string some_field = 1` is represented as `someField` in the JSON representation. So when they want to refer to that field in a CEL expression, they would write `obj.someField` but CEL can't resolve that because `obj` is declared as a protobuf type in the Env and CEL expects the field to be referenced like `obj.some_field`.

I hope that makes sense. I could't find an way to achieve the above in cel-go but I feel like I am missing something obvious. Any help is much appreciated.

Thank you.


Tristan Swadell

unread,
Dec 15, 2021, 6:00:12 PM12/15/21
to Charith Ellawala, CEL Go Discussion Forum
Hi Charith,

Unfortunately, you're not missing something obvious. One approach is to introduce a new `ref.TypeProvider` which can map from the camelCase `json` tag on the protobuf field to the snake_case name of the field. Your approach can be very robust and read the Go metadata information, or it can also be fairly simple and just recase from camelCase to snake_case when attempting to find a field name and that might just be enough to make things work.

Hopefully this helps!

-Tristan

--
You received this message because you are subscribed to the Google Groups "CEL Go Discussion Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cel-go-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cel-go-discuss/860b704d-8516-4164-b2cf-709561d37052n%40googlegroups.com.

Charith Ellawala

unread,
Dec 16, 2021, 8:55:54 AM12/16/21
to CEL Go Discussion Forum
Hi Tristan,

I somehow overlooked the `ref.TypeProvider.FindFieldType` method so thank you very much for that tip. I think it would work for my use case. 

BTW, the cel-go library is amazing in its extensibility. Thanks to you and the rest of the team for maintaining such a brilliant open source project.

Charith.  
Reply all
Reply to author
Forward
0 new messages