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

Generate methods on an existing type #169

Open
justenstall opened this issue Nov 18, 2024 · 3 comments
Open

Generate methods on an existing type #169

justenstall opened this issue Nov 18, 2024 · 3 comments
Labels
feature New feature or request

Comments

@justenstall
Copy link

justenstall commented Nov 18, 2024

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.

@jmattheis
Copy link
Owner

Could you give an example, I don't understand what you mean with extra round of code generation.

@jmattheis jmattheis added the question Further information is requested label Nov 19, 2024
@justenstall
Copy link
Author

justenstall commented Nov 20, 2024

Sure, this is what I would like to do.

I want to write this source:

Example moved to ticket description.

@jmattheis
Copy link
Owner

Okay. I'm not sure if that's worth the dev effort, as the alternative using functions directly e.g:

var input MyObject = ...

slim := conv.ConvertToSlim(input)

isn't that different as the proposed format

slim := input.ConvertToSlim()

If many users want this feature, I'm cool with changing my mind about this.


This project https://github.com/reedom/convergen does support having the source as receiver function, maybe it fits your need.

type Convergen interface {
    // :recv u
    ToStorage(*User) *storage.User  
}

generates:

type User struct {
    ID   int
    Name string
}

func (u *User) ToStorage() (dst *storage.User) {
    dst = &storage.User{}
    dst.ID = int64(u.ID)  
    dst.Name = u.Name

    return
}

https://github.com/reedom/convergen?tab=readme-ov-file#recv-var

@jmattheis jmattheis added feature New feature or request and removed question Further information is requested labels Nov 21, 2024
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

2 participants