copy icon indicating copy to clipboard operation
copy copied to clipboard

How to copy only not null fields?

Open frederikhors opened this issue 5 years ago • 1 comments

Is there a way to copy only the not null fields?

Example:

package main

import (
	"fmt"

	"github.com/gotidy/copy"
)

type Player struct {
	Firstname  string
	Username   *string
	UserDetail *UserDetail
}

type PlayerInput struct {
	Firstname  *string
	Username   *string
	UserDetail *UserDetail
}

type UserDetail struct {
	Email *string
}

func main() {
	inputFirstname := "Bob"

	input := PlayerInput{
		Firstname: &inputFirstname,
	}

	outputUsername := "BobTheBob"

	output := Player{
		Username: &outputUsername,
	}

	c := copy.New()
	c.Get(&output, &input).Copy(&output, &input)

	fmt.Printf("%#v \n", output)
}

I get:

main.Player{Firstname:"Bob", Username:(*string)(nil), UserDetail:(*main.UserDetail)(nil)}

I would:

main.Player{Firstname:"Bob", Username:"BobTheBob", UserDetail:(*main.UserDetail)(nil)}

Is this possible?

frederikhors avatar Nov 26 '20 00:11 frederikhors

Now there is no such possibility. I'll think about it.

yazver avatar Dec 28 '20 07:12 yazver