Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert/copy to existing instance #147

Closed
jmattheis opened this issue Jun 11, 2024 · 5 comments
Closed

Convert/copy to existing instance #147

jmattheis opened this issue Jun 11, 2024 · 5 comments
Labels
feature New feature or request

Comments

@jmattheis
Copy link
Owner

Currently goverter always instantiates the target type itself. Sometimes there is already an instance of the target type and you just want to copy the source fields to the target field. Example:

// goverter:converter
type Converter interface {
	CopyTo(source Input, target *Output)
}

type Input struct {
	Age  int
}

type Output struct {
	Name string
	Age  int
}

func use() {
	c := ConverterImpl{}

	source := Input{Age: 42}
	output := Output{Name: "jmattheis"}

	c.CopyTo(source, &output)

	// output.Name = "jmattheis"
	// output.Age = 42
}

Mapstruct has similar functionality:

@Mapper
public interface CarMapper {
    void updateCarFromDto(CarDto carDto, @MappingTarget Car car);
}

https://mapstruct.org/documentation/1.5/reference/html/#updating-bean-instances

See #146 (comment) for another use-case.

Please 👍 this issue if you want this functionality. If you have a specific use-case in mind, feel free to comment it.

@dhirajsb
Copy link

Is there also a general use case for supporting factory functions to provide the target instance?

@jmattheis
Copy link
Owner Author

@dhirajsb Could you create a new ticket with a use case? I don't want to fill this ticket with not closely related requests.

isinyaaa added a commit to isinyaaa/model-registry that referenced this issue Jun 21, 2024
As that fixed a nil assignment override[1][2] which prevented us from
emulating copy constructors[3].
The update also allows us to use generics.

[1]: jmattheis/goverter#146 (comment)
[2]: jmattheis/goverter#97
[3]: jmattheis/goverter#147

Signed-off-by: Isabella Basso do Amaral <idoamara@redhat.com>
isinyaaa added a commit to isinyaaa/model-registry that referenced this issue Jun 21, 2024
As that fixed a nil assignment override[1][2] which prevented us from
emulating copy constructors[3].
The update also allows us to use generics.

[1]: jmattheis/goverter#146 (comment)
[2]: jmattheis/goverter#97
[3]: jmattheis/goverter#147

Signed-off-by: Isabella Basso do Amaral <idoamara@redhat.com>
Signed-off-by: Isabella do Amaral <idoamara@redhat.com>
isinyaaa added a commit to isinyaaa/model-registry that referenced this issue Jun 25, 2024
As that fixed a nil assignment override[1][2] which prevented us from
emulating copy constructors[3].
The update also allows us to use generics.

[1]: jmattheis/goverter#146 (comment)
[2]: jmattheis/goverter#97
[3]: jmattheis/goverter#147

Signed-off-by: Isabella Basso do Amaral <idoamara@redhat.com>
Signed-off-by: Isabella do Amaral <idoamara@redhat.com>
google-oss-prow bot pushed a commit to kubeflow/model-registry that referenced this issue Jun 26, 2024
* update goverter to 1.4.1

As that fixed a nil assignment override[1][2] which prevented us from
emulating copy constructors[3].
The update also allows us to use generics.

[1]: jmattheis/goverter#146 (comment)
[2]: jmattheis/goverter#97
[3]: jmattheis/goverter#147

Signed-off-by: Isabella Basso do Amaral <idoamara@redhat.com>
Signed-off-by: Isabella do Amaral <idoamara@redhat.com>

* simplify converter utils using generics

Signed-off-by: Isabella do Amaral <idoamara@redhat.com>

* server: update existing objects on PATCH

Signed-off-by: Isabella do Amaral <idoamara@redhat.com>

---------

Signed-off-by: Isabella Basso do Amaral <idoamara@redhat.com>
Signed-off-by: Isabella do Amaral <idoamara@redhat.com>
mzhl1111 pushed a commit to mzhl1111/model-registry that referenced this issue Jul 1, 2024
* update goverter to 1.4.1

As that fixed a nil assignment override[1][2] which prevented us from
emulating copy constructors[3].
The update also allows us to use generics.

[1]: jmattheis/goverter#146 (comment)
[2]: jmattheis/goverter#97
[3]: jmattheis/goverter#147

Signed-off-by: Isabella Basso do Amaral <idoamara@redhat.com>
Signed-off-by: Isabella do Amaral <idoamara@redhat.com>

* simplify converter utils using generics

Signed-off-by: Isabella do Amaral <idoamara@redhat.com>

* server: update existing objects on PATCH

Signed-off-by: Isabella do Amaral <idoamara@redhat.com>

---------

Signed-off-by: Isabella Basso do Amaral <idoamara@redhat.com>
Signed-off-by: Isabella do Amaral <idoamara@redhat.com>
Signed-off-by: muzhouliu <sllzhlv77@gmail.com>
@heliosgo
Copy link

heliosgo commented Sep 5, 2024

It would be better if null/empty values ​​could be ignored.

// goverter:converter
type Converter interface {
	CopyTo(source Input, target *Output, ignoreEmpty ...bool)
}

type Input struct {
	Age  int
}

type Output struct {
	Name string
	Age  int
}

func use() {
	c := ConverterImpl{}

	source := Input{Age: 0}
	output := Output{Name: "jmattheis", Age: 42}

	c.CopyTo(source, &output, true)

	// output.Name = "jmattheis"
	// output.Age = 42
}

@jmattheis
Copy link
Owner Author

@heliosgo created #160 for this, as it's not unique to the the copy feature described in this issue.

@jmattheis
Copy link
Owner Author

Fixed with v1.6.0. See Guide: Update an existing instance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants