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

Merge generated file with existing instead of overwriting #83

Open
alenapetraki opened this issue Feb 28, 2024 · 0 comments
Open

Merge generated file with existing instead of overwriting #83

alenapetraki opened this issue Feb 28, 2024 · 0 comments

Comments

@alenapetraki
Copy link

alenapetraki commented Feb 28, 2024

I want to edit generated methods and I want them not to be overwritten by next generation. If new methods are added to interface I want them to be generated and added to the existing file.

So what I want is some kind of strategy. Invocation may look like:

gowrap gen -i Some -t mine.tmpl -o generated.go -strategy merge|overwrite

How woild this work:

Template:

type {{$decorator}} struct {
  logger *zap.Logger
  next {{.Interface.Type}}
}

{{range $method := .Interface.Methods}}
func (m *{{$decorator}}) {{$method.Declaration}} {
    m.logger.Info("{{ $method.Name }}")
    {{ $method.Pass "m.next." }}
}
{{end}}

Initial interface:

type Some interface {
    A()
}

Initial generated file:

type decorator struct {
    logger *zap.Logger
    next Some
}

func (d *decorator) A() {
    m.logger.Info("A") 
    m.next.A()
}

Updated file:

type decorator struct {
    logger *zap.Logger
    next Some
}

func (d *decorator) A() {
    m.logger.Info("A with updates") // updated generated file
    m.next.A()
}

Updated interface:

type Some interface {
    A()
    B() // New added method
}

Generated with stratedy 'merge' file:

type decorator struct {
    logger *zap.Logger
    next Some
}

func (d *decorator) A() {
    m.logger.Info("A with updates") // updates are not overwritten
    m.next.A()
}

func (d *decorator) B() {
    m.logger.Info("B") 
    m.next.A()
}

If you believe there is a place for such a feature in this library I might be able to implement it myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant