Add support for custom struct tags like `custom:"name"`

未关闭
#234 3 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

  • #829 来自 @NickYadance —— 已关闭,未合并
  • #942 来自 @samlown —— 已关闭,未合并

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
go

调研方向

从 expr.Env() 选项开始,跟踪 struct 字段和方法如何暴露给表达式上下文。将该行为与 protobuf 生成的 MyType 示例及其 json tags 进行比较。当可以使用基于 tag 的名称,并排除未导出的字段和重复方法时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

feature

When passing structs to the expr.Env() option, it would be nice to have a way to modify the field extraction behavior to be able to:

  • extract or map names based on tags such as the common json tag
  • ignore non-exported fields
  • ignore methods

In my particular case, I'm working with protobuf-generated structs and using the expr library as a way to implement a filtering grammar similar to what is described in https://google.aip.dev/160.

Here's an example struct:

type MyType struct {
    state protoimpl.MessageState
    sizeCache protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    Id         string                 `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
    Name       string                 `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
    CreateTime *timestamppb.Timestamp `protobuf:"bytes,3,opt,name=create_time,json=createTime,proto3" json:"
}

func (x *MyType) GetId() string {
    if x != nil {
        return x.Id
    }
    return ""
}

/* other generated functions for exported fields */

The above struct results in both the non-exported fields and duplicate methods being made available to the expression context. It also means that the default names in the expression (prior to patching) don't match the names given in the protobuf.

主要语言
Go
星标
8.1k
派生
544
平均合并
1 天 9 小时
30 天内合并 PR
25

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

expr-lang/expr 的其他 Issue

查看 expr-lang/expr 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。