uber-go / gopatch

Refactoring and code transformation tool for Go.
MIT License
917 stars 33 forks source link

Appending new variable declaration in a var(...) block #134

Open rosroble opened 1 year ago

rosroble commented 1 year ago

Hello!

I have a global var block as follows:

var (
    SystemSubsystem                   = "system"
    SystemLabelNames                  = []string{"hostname", "resource", "system_id"}
    SystemMemoryLabelNames            = []string{"hostname", "resource", "memory", "memory_id"}
// and so on
)

Let's say I want to insert a new variable SystemProcessorLabelNames after the SystemMemoryLabelNames

This doesn't work:

@@
@@
SystemMemoryLabelNames            = []string{"hostname", "resource", "memory", "memory_id"}
+SystemProcessorLabelNames         = []string{"hostname", "resource", "processor", "processor_id"}

And this doesn't work:

@@
@@
var (
...
SystemMemoryLabelNames            = []string{"hostname", "resource", "memory", "memory_id"}
+SystemProcessorLabelNames         = []string{"hostname", "resource", "processor", "processor_id"}
)

Although replacing works fine like that:

@@
@@
-[]string{"hostname", "resource", "memory", "memory_id"}
+[]string{"hostname", "resource", "processor", "processor_id"}

But I need to append a new line. Appending works fine inside functions, just like that:

@@
@@
a := 5
+b:=6

But inside var block it either fails or skips the file. Is this a bug or my mistake? Thanks.

lverma14 commented 1 year ago

Hi, gopatch doesn't yet support elision in value declarations #6 . But it should work against the format specified in the patch. Ref The below patch should work & add the new line.

@@
@@
 var (
  SystemSubsystem        = "system"
  SystemLabelNames       = []string{"hostname", "resource", "system_id"}
  SystemMemoryLabelNames = []string{"hostname", "resource", "memory", "memory_id"}
+ SystemProcessorLabelNames = []string{"hostname", "resource", "processor", "processor_id"}
 )

Thank you for reporting this! We plan to fix this in the future versions.