Skip to content

Feature request: allow ignoring struct fields with struct tag expr:"-" #805

Closed
@diegommm

Description

@diegommm

Description: Similar to what other Go libraries provide, it would be nice that in expr we could ignore a struct field in the environment if the field has a struct tag expr:"-".

Example happening in other libraries: in the standard library the package encoding/json ignores struct fields that have the struct tag json:"-", causing it to neither encode nor decode those fields.

Use case: reusing the same struct to avoid tedious copying that is error-prone.

Example of proposed feature:

type MyEnv struct {
	VisibleField string
	HiddenField  string `expr:"-"`
}

func main() {
	_, err := expr.Eval(`HiddenField`, MyEnv{})
	// we would get a `cannot fetch HiddenField` error
}

P.S.: I have created PR #806 as an example of how the feature could be implemented. Please, let me know if you would rather discuss a different approach if you think this proposal makes sense.

Thank you :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions