卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章34096本站已运行391

有条件地将多个属性分配给结构的正确方法是什么

有条件地将多个属性分配给结构的正确方法是什么

问题内容

我正在为我用 go 编写的 be 的 graphql 查询开发一个解析器函数。在解析器中,我有想要更新的用户数据,使用包含多个可能的更新属性的输入值。

在 javascript 中,这可以通过解构(伪)快速完成:

const mergedobj = {...oldprops, ...newprops}

目前,我的解析器函数如下所示(使用 gqlgen 作为 graphql go 解析器):

func (r *mutationResolver) ModifyUser(ctx context.Context, input *model.ModifyUserInput) (*model.User, error) {
    id := input.ID
    us, ok := r.Resolver.UserStore[id]
    if !ok {
        return nil, fmt.Errorf("not found")
    }

    if input.FirstName != nil {
        us.FirstName = *input.FirstName
    }

    if input.LastName != nil {
        us.LastName = *input.LastName
    }

    if input.ProfileImage != nil {
        us.ProfileImage = input.ProfileImage
    }

    if input.Password != nil {
        us.Password = *input.Password
    }

    if input.Email != nil {
        us.Email = *input.Email
    }

    if input.InTomorrow != nil {
        us.InTomorrow = input.InTomorrow
    }

    if input.DefaultDaysIn != nil {
        us.DefaultDaysIn = input.DefaultDaysIn
    }

    r.Resolver.UserStore[id] = us

    return &us, nil
}

这感觉很简单。在这种情况下迭代结构键有意义吗?或者我还缺少另一种模式吗?


正确答案


使用函数来减少样板:

func mergef[t any](a, b *t) {
    if b != nil {
        *a = *b
    }
}

...
mergef(&us.firstname, input.firstname)
mergef(&us.lastname, input.lastname)
...

使用反射包来减少更多样板:

// merge sets fields in struct pointed to by d to 
// dereferenced fields in struct pointed to by s. 
//
// argument s must point to a struct with pointer type
// fields.   
// argument d must point to a struct with fields that 
// correspond to the fields in s: there must be a field
// in d with the same name as a field in s; the type of
// the field in s must be a pointer to the type of the field
// in d.   
func merge(d, s any) {
    sv := reflect.valueof(s).elem()
    dv := reflect.valueof(d).elem()
    for i := 0; i < sv.numfield(); i++ {
        sf := sv.field(i)
        if sf.isnil() {
            continue
        }
        df := dv.fieldbyname(sv.type().field(i).name)
        df.set(sf.elem())
    }
}

使用这样的函数:

merge(us, input)
卓越飞翔博客
上一篇: 为什么立即调用内联函数,而不是仅仅调用其包含的函数?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏