https://cerbos.dev logo
#help
Title
# help
r

Rasmus Dencker

06/22/2023, 2:18 PM
In the Go SDK, it would be nice with a way to access the getters of the, for instance, underlying engine.Resource in client.Resource, so that you could do resource.GetId(). I'm trying to wrap IsAllowed in a dataloader which ultimately calls CheckResourceBatch; I'm combining it with gqlgen directives. Right now the underlying 'enginev1' struct methods is not exported, making it immutable (which is nice), but leaves instances of client.Resource, client.Principal etc. a black box. My current workaround is really ugly, improvement suggestions are very welcome 😅
Copy code
type IDWrapper[T any] struct{
	Inner T
	ID    string
}

type Key struct {
	Principal IDWrapper[*client.Principal]
	Resource  IDWrapper[*client.Resource]
	Action    string
}
c

Charith (Cerbos)

06/22/2023, 2:52 PM
Looks like a reasonable implementation to me. I have raised a PR to add methods to get the ID and some of the other useful fields from the builders. Thanks for reporting.
r

Rasmus Dencker

06/22/2023, 2:54 PM
I ended up realizing I also needed the "kind", etc, so now I'm using enginev1.Principal for my arguments, and then converting them to client.Principal by hand when needed. And hey, insanely fast PR. Even before I could consider opening a PR myself 😄 once again great work from you guys, thanks!
(I also JSON encode it to generate a unique string key for dataloader)
c

Charith (Cerbos)

06/22/2023, 2:56 PM
I included
Kind
in the PR. You could get access to the underlying protobuf as well if you have other requirements.
What do you JSON encode? The kind?
r

Rasmus Dencker

06/22/2023, 2:57 PM
Yeah I saw that you also did the Proto() method, which would be quite useful for me. I actually JSON Encode the entire proto struct, but I'll probably refactor to only do the ID and kind
c

Charith (Cerbos)

06/22/2023, 3:00 PM
Right, I see. I am sure you know this already: protobuf objects should be encoded using the
protojson
package. BUT, they intentionally add random spaces to the output so if you're using the output as a key, it might not work as you expect.
r

Rasmus Dencker

06/22/2023, 3:00 PM
It might include a ton of weird protobuf fields too
interesting, didn't know about that one! I don't trust json encoding to be deterministic anyway, this is just a temporary quick'n'dirty for the proof of concept
2 Views