Custom type as a Mapper + Receiver

349 views
Skip to first unread message

Vivek Shankar

unread,
Jul 15, 2020, 8:04:25 AM7/15/20
to CEL Go Discussion Forum
I am trying to create a custom type with all the capabilities of a mapper, such as indexing and iteration, but with some additional function overloads. This doesn't seem to work and I get the "no such overload" error, probably because the "." operator looks for a field instead of calling into the receiver, but I wanted to confirm there is no way to do this.

The following is my custom type.

type User struct {
    ref.TypeAdapter
    context *vctx.VerifyContext
    m map[string]interface{}
    traits.Mapper
    traits.Receiver
}

var (
    UserType = types.NewTypeValue("User",
         traits.ContainerType,
         traits.IndexerType,
         traits.IterableType,
         traits.SizerType,
         traits.ReceiverType)
)


In the constructor func, it just creates a new dynamic map (types.NewDynamicMap) and passes in the source map[string]interface{} into it. This is assigned to Mapper in the struct instance. So basically the struct hands off all mapper logic to the underlying Mapper instance.

Thanks for your help.

Vivek Shankar

unread,
Jul 15, 2020, 9:29:35 AM7/15/20
to CEL Go Discussion Forum
My mistake. I did not override the Type() and so it was picking up the type that baseMap returns.

Espen Zachrisen

unread,
Jul 15, 2020, 10:55:35 AM7/15/20
to CEL Go Discussion Forum
I implemented support for native Go structs if you're interested in the code. 

This is in the "experimental" state, but would love to collaborate if you're interested. 

-- Espen 

Tristan Swadell

unread,
Jul 15, 2020, 1:08:52 PM7/15/20
to Espen Zachrisen, CEL Go Discussion Forum
Hi Espen,

I definitely think that it would be good to provide a GoTypeProvider as an alternative to proto for adding custom types to CEL. I more or less created an OpenAPISchema type provider for CEL Policy Templates, so I'm happy to see similarities between your implementation and my own since it speaks to how well the existing interfaces can be used to augment the type systems supported within CEL. 

-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/91070d48-1514-4c57-a688-2506de410f40o%40googlegroups.com.

Espen Zachrisen

unread,
Jul 16, 2020, 9:23:36 AM7/16/20
to CEL Go Discussion Forum
Yes, I agree, I found the interfaces easy to work with. (Having said that, if you see anything in my code that isn't right, let me know!) I used the Istio example you've mentioned before as a reference. 

It's pretty remarkable that CEL can be extended in this way. I know that flexibility doesn't happen by accident - kudos to whoever designed it! 

-- Espen
To unsubscribe from this group and stop receiving emails from it, send an email to cel-go-...@googlegroups.com.

Vivek Shankar

unread,
Jul 17, 2020, 8:02:20 PM7/17/20
to CEL Go Discussion Forum
Hi Espen,

This looks really useful. I implemented my custom types using the TypeAdapter approach. I am unsure when I should be using one over the other but this your AttributeProvider appears to be the way to go as the number of custom types grow?

Thanks for sharing this.

Cheers,
Vivek
Reply all
Reply to author
Forward
0 new messages