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

Handle zero values as absent values #160

Open
jmattheis opened this issue Sep 6, 2024 · 0 comments
Open

Handle zero values as absent values #160

jmattheis opened this issue Sep 6, 2024 · 0 comments
Labels
feature New feature or request

Comments

@jmattheis
Copy link
Owner

When using default or the upcoming copy to an existing instance #147, a user may want to skip setting fields on a target struct when the source field has a zero value.

// goverter:converter
type Converter interface {
	// goverter:default DefaultOutput
	Convert(source Input) Output
}

func DefaultOutput() Output {
	return Output{Name: "jmattheis", Age: 42}
}

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

func use() {
	var c Converter
	source := Input{Name: "override", Age: 0}
	output := c.Convert(source) 
	// current behavior
	// output.Name = override, output.Age = 0
}

In Go the zero value can describe an absent value, goverter should support skipping the absent value so that the output would be actually

// output.Name = override, output.Age = 42
// 42 is from the DefaultOutput

Similarly, when converting a non nilable primitive type to a nilable type, the target should be set/stay nil when the source value is absent, e.g.

// goverter:converter
type Converter interface {
	// goverter:default DefaultOutput
	Convert(source Input) Output
}

func DefaultOutput() Output {
	return Output{Name: "jmattheis", Age: nil}
}

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

func use() {
	var c Converter
	source := Input{Name: "override", Age: 0}
	output := c.Convert(source)
	// wanted behavior
	// output.Name = override, output.Age = nil
}

I don't think this should be the default, so this feature should be behind a feature config flag.

Similar #97
Request #147 (comment)
Request #152

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

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

1 participant