Home > Software design >  What is the right way to conditionally assign multiple properties to a struct
What is the right way to conditionally assign multiple properties to a struct

Time:01-26

I'm working on a resolver function for a GraphQL query for a BE I'm writing in Go. In the resolver, I have user data that I want to update, using an input value containing several possible update properties.

In JavaScript, this can be done quickly through destructuring (pseudo):

const mergedObj = {...oldProps, ...newProps}

For now, my resolver function looks like this (using gqlgen for GraphQL Go resolvers):

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
}

This feels quite boilerplatey. Would it make sense in this situation to iterate through struct keys? Or is there another pattern I'm missing?

CodePudding user response:

Use a function to reduce the boilerplate:

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

...
mergef(&us.FirstName, input.FirstName)
mergef(&us.LastName, input.LastName)
...

Use the reflect package to reduce more boilerplate:

// 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())
    }
}

Employ the function like this:

merge(us, input)
  • Related