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

Jos*_*ill 0 go conditional-statements graphql gqlgen

我正在为我用 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
}
Run Code Online (Sandbox Code Playgroud)

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

Pen*_*ens 6

使用函数来减少样板代码:

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

...
mergef(&us.FirstName, input.FirstName)
mergef(&us.LastName, input.LastName)
...
Run Code Online (Sandbox Code Playgroud)

使用 Reflect 包来减少更多样板文件:

// 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())
    }
}
Run Code Online (Sandbox Code Playgroud)

使用这样的函数:

merge(us, input)
Run Code Online (Sandbox Code Playgroud)