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

Improve struct code-generation for pointer fields #97

Closed
jmattheis opened this issue Nov 4, 2023 · 1 comment · Fixed by #148
Closed

Improve struct code-generation for pointer fields #97

jmattheis opened this issue Nov 4, 2023 · 1 comment · Fixed by #148
Labels
feature New feature or request

Comments

@jmattheis
Copy link
Owner

jmattheis commented Nov 4, 2023

For this converter interface

// goverter:converter
type Converter interface {
	Convert(*Input) *Output
}

type Input struct {
	Age  int
	Name *string
}
type Output struct {
	Age  int
	Name *string
}

goverter will generate this code

func (c *ConverterImpl) Convert(source *example.Input) *example.Output {
	var pExampleOutput *example.Output
	if source != nil {
		var exampleOutput example.Output
		exampleOutput.Age = (*source).Age
		var pString *string
		if (*source).Name != nil {
			xstring := *(*source).Name
			pString = &xstring
		}
		exampleOutput.Name = pString
		pExampleOutput = &exampleOutput
	}
	return pExampleOutput
}

This could be simplified to:

func (c *ConverterImpl) Convert(source *example.Input) *example.Output {
	var pExampleOutput *example.Output
	if source != nil {
		var exampleOutput example.Output
		exampleOutput.Age = (*source).Age
		if (*source).Name != nil {
			xstring := *(*source).Name
			exampleOutput.Name = &xstring
		}
		pExampleOutput = &exampleOutput
	}
	return pExampleOutput
}

This is useful for the default feature. See #96 (comment) & #93

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

@jmattheis jmattheis added the feature New feature or request label Nov 4, 2023
@gtchiflidjanov
Copy link

gtchiflidjanov commented Mar 18, 2024

This could be simplified more:

func (c *ConverterImpl) Convert(source *example.Input) *example.Output {
  var pExampleOutput *example.Output
  if source != nil {
    pExampleOutput = & example.Output{
      Age: source.Age,
      Name: source.Name,
    }
  }
  return pExampleOutput
}

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>
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

Successfully merging a pull request may close this issue.

2 participants