google / cel-go

Fast, portable, non-Turing complete expression evaluation with gradual typing (Go)
https://cel.dev
Apache License 2.0
2.19k stars 218 forks source link

Add support for struct tag name overrides in native types #941

Closed patrickpichler closed 3 months ago

patrickpichler commented 4 months ago

In order to customize how to fields of native types can be accessed via CEL, struct tag support has been added. It is now possible to provide a struct field tag, that will act as the name for the corresponding field.

Here an example

type Person struct {
  Name string
  Age  int `cel:"age"`
}

and here is how to access the Age field from CEL:

person.age

Pull Requests Guidelines

See CONTRIBUTING.md for more details about when to create a GitHub [Pull Request][1] and when other kinds of contributions or consultation might be more desirable.

When creating a new pull request, please fork the repo and work within a development branch.

Commit Messages

Background on why the change is being made with additional detail on consequences of the changes elsewhere in the code or to the general functionality of the library. Multiple paragraphs may be used, but please keep lines to 72 characters or less.



## Reviews

* Perform a self-review.
* Make sure the Travis CI build passes.
* Assign a reviewer once both the above have been completed.

## Merging

* If a CEL maintaner approves the change, it may be merged by the author if
  they have write access. Otherwise, the change will be merged by a maintainer.
* Multiple commits should be squashed before merging.
* Please append the line `closes #<issue-num>: description` in the merge message,
  if applicable.

[1]:  https://help.github.com/articles/about-pull-requests
TristonianJones commented 4 months ago

/gcbrun

TristonianJones commented 4 months ago

Thanks @patrickpichler! This seems like a very nice win.

quantumsheep commented 4 months ago

Wow I just discovered this PR after creating #942! My PR fixes an issue not fixed in yours but completely related to the same use-case

patrickpichler commented 3 months ago

@TristonianJones I've updated the code, would be nice if you could have another look :)

TristonianJones commented 3 months ago

/gcbrun

TristonianJones commented 3 months ago

@patrickpichler thanks for the contribution!