Skip to content

Generate methods on an existing type  #169

Open
@justenstall

Description

@justenstall

Describe the solution you'd like

I would like to generate methods on an existing type so the type implements an interface I am defining.

type MyObject struct {
	Name string
	Description string
	Values map[string]string
}

type MyObjectSlim struct {
	Name string
}

// goverter:converter
// goverter:skipCopySameType
// goverter:output:file @cwd/convert.gen.go
// goverter:output:format method *MyObject
type MyObjectConverter interface {
	ConvertToSlim() *MyObjectSlim
}

and have goverter generate the following:

func (source *MyObject) ConvertToSlim() *MyObjectSlim {
   var pMyObjectSlim *MyObjectSlim
   if source != nil {
      var myObjectSlim MyObjectSlim
      myObjectSlim.Name = (*source).Name
   }
   return pMyObjectSlim
}

Describe alternatives you've considered

I can define the methods manually and call the goverter-generated functions from them, but I would prefer to combine those steps to avoid an extra round of code generation.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions