PaesslerAG/gval

Extending variable() to support custom selectors on value

darh opened this issue · 3 comments

darh commented

I'm struggling to get a bit more control over how path selection is done.

I would like to expose one of my structs with prop that's internally slice of structs ({ name, value, position }) to be accessed in more "fluent" way (record.values.foo or record.values.bar[0]). More info here: https://github.com/cortezaproject/corteza-server/blob/develop/compose/types/record.go#L38

Currently, I'm doing this by casting into map[string]interface{} but I would like to avoid this because it requires keeping copies of the original data and constantly syncing and casting.

I've tried with adding an operator but I have to admit I'm lacking know-how of the internal workings of gval to make even a proof of concept. A custom function (getValue(record, name, position)) would work but that ruins the fluentness :)

After digging more into gval, I'm thniking that a) variable fn could be modified to get additional case that would cover a special interface interface { GvalSelect(key string) (interface{}, error) } (I'm not 100% happy with the fn name here... ).

If you think this would be a good approach I can open a PR in next couple of days.

Take a look at the VariableSelector. You can use this Option to override gvals variable selection.

https://pkg.go.dev/github.com/PaesslerAG/gval#VariableSelector

This seems to be what you want.

darh commented

Ok, yes, I agree - this is how I got the idea in the first place...

If I override it I need to reimplement everything that variable fn (and reflectSelect) already does (fn is not exported). I plan to use that Record struct with everything gval already supports.

Ok. I don't like the aspect that it makes a requirement on the method of a data struct. But I see how a feature like this comes handy and would merge this.