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

Support -propagateInitFuncReturns CLI option #16

Merged
merged 1 commit into from
Aug 22, 2022

Conversation

moznion
Copy link
Owner

@moznion moznion commented Aug 21, 2022

This option is the specifier whether to propagate the returned values come from the init function that is specified through the -init option by the generated constructor.

For example, when it generates the constructor for the following code:

//go:generate gonstructor --type=Struct --constructorTypes=allArgs,builder --init validate --propagateInitFuncReturns
type Struct struct {
	foo string
}

func (s *Struct) validate() error {
	// do something with the created `Struct` value.
	return err
}

then, the generated code becomes like the following:

func NewStruct(foo string) (*Struct, error) {
	r := &Struct{
		foo: foo,
	}

	ret_validate0 := r.validate()

	return r, ret_validate0
}

type Structstruct {
	foo string
}

func NewStruct() *Struct{
	return &Struct{}
}

func (b *Struct) Foo(foo string) *Struct{
	b.foo = foo
	return b
}

func (b *Struct) Build() (*Struct, error) {
	r := &Struct{
		foo: b.foo,
	}
	ret_validate0 := r.validate()
	return r, ret_validate0
}

Ref: #13


Known Issue

If this option is used with the multiple --type options, probably it won't be the expected result.

@moznion moznion added the enhancement New feature or request label Aug 21, 2022
@moznion moznion self-assigned this Aug 21, 2022
This option is the specifier whether to propagate the returned values come from the init
function that is specified through the `-init` option by the generated constructor

For example, when it generates the constructor for the following code:

```go
//go:generate gonstructor --type=Struct --constructorTypes=allArgs,builder --init validate --propagateInitFuncReturns
type Struct struct {
	foo string
}

func (s *Struct) validate() error {
	// do something with the created `Struct` value.
	return err
}
```

then, the generated code becomes like the following:

```go
func NewStruct(foo string) (*Struct, error) {
	r := &Struct{
		foo: foo,
	}

	ret_validate0 := r.validate()

	return r, ret_validate0
}

type Structstruct {
	foo string
}

func NewStruct() *Struct{
	return &Struct{}
}

func (b *Struct) Foo(foo string) *Struct{
	b.foo = foo
	return b
}

func (b *Struct) Build() (*Struct, error) {
	r := &Struct{
		foo: b.foo,
	}
	ret_validate0 := r.validate()
	return r, ret_validate0
}
```

Ref: #13

Signed-off-by: moznion <moznion@mail.moznion.net>
@moznion moznion force-pushed the propagate_init_return_types branch from 9a50821 to 3d44054 Compare August 21, 2022 22:45
@moznion moznion merged commit d6c5c28 into master Aug 22, 2022
@moznion moznion deleted the propagate_init_return_types branch August 22, 2022 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant