Replace reference path

136 views
Skip to first unread message

mr....@gmail.com

unread,
Oct 21, 2022, 11:58:57 AM10/21/22
to golang-nuts
I'm looking for a tool like this, I've searched google and github and can't find one, have you guys used such a tool?

I use a method like this in my code

k8s.io/apimachinery/pkg/util/diff.ObjectReflectDiff

k8s.io/apimachinery/pkg/util/diff is his import path, later I want to switch to another method.

github.com/google/go-cmp/cmp.Diff

github.com/google/go-cmp/cmp is his import path.

I would like to ask if there is a more convenient tool for replacing the whole project?

k8s.io/apimachinery/pkg/util/diff.ObjectReflectDiff -> github.com/google/go-cmp/cmp.Diff

Amnon

unread,
Oct 21, 2022, 6:20:53 PM10/21/22
to golang-nuts

mr....@gmail.com

unread,
Oct 30, 2022, 10:22:38 PM10/30/22
to golang-nuts
He doesn't get the job done.

Kurtis Rader

unread,
Oct 30, 2022, 10:52:45 PM10/30/22
to mr....@gmail.com, golang-nuts
I'm curious how often you perform such refactoring? In my experience such changes are extremely rare and usually require other changes due to differences in the API of the two third-party packages . Which means, in my experience, expending effort to automate such import rewrites typically requires more effort than just doing it "by hand". The gomvpkg tool is meant to solve a more common problem that does happen with some regularity. Namely, changing the paths of packages private to a particular project as that project evolves. Which is why that tool "doesn't get the job done" for your situation.  

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/695f32a0-009c-4415-b856-71bd4792385dn%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

mr....@gmail.com

unread,
Oct 31, 2022, 11:26:44 PM10/31/22
to golang-nuts
Yes, it is very rare, but it can be encountered, and once encountered his workload will be a lot of

ioutil.TempDir => os.MkdirTemp
ioutil.ReadAll  => io.ReadAll

Kurtis Rader

unread,
Nov 1, 2022, 12:41:22 AM11/1/22
to mr....@gmail.com, golang-nuts
On Mon, Oct 31, 2022 at 8:27 PM mr....@gmail.com <mr....@gmail.com> wrote:
Yes, it is very rare, but it can be encountered, and once encountered his workload will be a lot of

ioutil.TempDir => os.MkdirTemp
ioutil.ReadAll  => io.ReadAll

You seem to agree with me that such refactorings are extremely rare. Furthermore, your ioutil.TempDir to os.MkdirTemp example is a trivial substitution since the API didn't change -- only the preferred function name changed. How many programs include more than a single call to the now deprecated ioutil.TempDir function? And how much work is it for the people maintaining those projects to manually make the required substitutions? How much work would be saved even if those people were aware of such a tool? 

Your real question seems to be "Is there a tool that replaces a deprecated API with the preferred API?" I am not aware of such a tool. Furthermore, such a tool would have to be aware of the minimum Go version supported by a project and whether a particular dependency can be updated. The mechanical substitution of the API is trivial compared to determining whether the substitution is appropriate. Also, the substitution is only possible if the API is unchanged -- only the preferred symbols are changed.

I also note that none of the links in your initial message are valid. So I can't determine whether they are a trivial renaming of an API or the changes are more substantive.


在2022年10月31日星期一 UTC+8 10:52:45<kra...@skepticism.us> 写道:
I'm curious how often you perform such refactoring? In my experience such changes are extremely rare and usually require other changes due to differences in the API of the two third-party packages . Which means, in my experience, expending effort to automate such import rewrites typically requires more effort than just doing it "by hand". The gomvpkg tool is meant to solve a more common problem that does happen with some regularity. Namely, changing the paths of packages private to a particular project as that project evolves. Which is why that tool "doesn't get the job done" for your situation.  

On Sun, Oct 30, 2022 at 7:22 PM mr....@gmail.com <mr....@gmail.com> wrote:
He doesn't get the job done.

在2022年10月22日星期六 UTC+8 06:20:53<Amnon> 写道:
try https://pkg.go.dev/golang.org/x/tools/cmd/gomvpkg


On Friday, 21 October 2022 at 16:58:57 UTC+1 mr....@gmail.com wrote:
I'm looking for a tool like this, I've searched google and github and can't find one, have you guys used such a tool?

I use a method like this in my code

k8s.io/apimachinery/pkg/util/diff.ObjectReflectDiff

k8s.io/apimachinery/pkg/util/diff is his import path, later I want to switch to another method.

github.com/google/go-cmp/cmp.Diff

github.com/google/go-cmp/cmp is his import path.

I would like to ask if there is a more convenient tool for replacing the whole project?

k8s.io/apimachinery/pkg/util/diff.ObjectReflectDiff -> github.com/google/go-cmp/cmp.Diff
 
--

mr....@gmail.com

unread,
Nov 1, 2022, 1:51:07 AM11/1/22
to golang-nuts

mr....@gmail.com

unread,
Nov 1, 2022, 2:03:53 AM11/1/22
to golang-nuts
I think this tool just needs to be replaced from what to what.

For example this looks like this


goreplacepkg -from ioutil.ReadAll -to  io.ReadAll

Here we think his parameters are the same, then the tool just needs to parse the go file, find the path of the import in this go file, and the methods under this path. Then do the replacement,Finally a new file is generated, github.com/google/go-cmp/cmp is added to import, and the original k8s.io/apimachinery/pkg/util/diff can be removed if he is no longer used.

For example, the input file would look like this

package test


func A()string{
    var a = "test"
     var b = "test1"
     return diff.ObjectDiff(a,b)
}

func B()string{
      var a = "test"
      var b= 'test1"
      return diff.ObjectGoPrintSideBySide(a,b)
}



Execution of commands

The final file he generates may look like this

package test

import  (
)

func A()string{
    var a = "test"
     var b = "test1"
     return cmp.Diff(a,b)
}

func B()string{
      var a = "test"
      var b= 'test1"
      return diff.ObjectGoPrintSideBySide(a,b)
}


He doesn't need to know the go version, he just needs the user to pass in the input and output
Reply all
Reply to author
Forward
0 new messages