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

Go implementation of Piranha #112

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
19 changes: 19 additions & 0 deletions go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# PiranhaGo
PiranhaGo is runnable now.
Instructions:-
1. To build the package run `go build -o piranha`. Dependencies will install automatically and they are given in `go.mod` file.
2. Below are the instructions for running for the single file.
```
Inputs from args
Usage: ./piranha [-h] -p PROPERTIES -s SOURCE_FILE -f STALE_FLAG [-treated] [-o OUTPUT]
Required arguments:
-s SOURCE_FILE: Path of the file to be refactored
-p PROPERTIES: Configuration file (json format) for Piranha.
-f STALE_FLAG: Name of the stale flag
Optional arguments:
-h: Show the options and exit.
-treated: If this is given, then flag is treated,
otherwise it is control.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want control to be implicit? I believe most other Piranha versions take an explicit string, which is either treatment or control (and, for some languages, and probably for Go in the future, they can also take a specific named treatment group).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make it different. But I thought making it implicit is still fine. I think it's generally less pain to first-time users as they have to set fewer attributes initially when they are trying out. (I will answer other comments slowly and steadily:)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking here is that, since we are deleting code in place, we do want the user to be somewhat explicit about which logic ought to be deleted, even if it's a bit more friction. It's also not clear that one default would be more common than the other.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I agree with you. I suggest we can ask for as like:
-mode treated or -mode control. Is it fine?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. That makes sense to me :)

-o OUTPUT: Destination of the refactored output from piranha. If -o is not provided, then the source file is updated in place.
```
3. To do a test run, run piranha on `example/testExample.go`. Run `./piranha -p properties.json -s ./example/testExample.go -o ./example/treatedExample.go -f staleFlag` command in root directory. You will get your refracted file as `/example/treatedExample.go`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"refactored", not "refracted", I assume.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change that.

69 changes: 69 additions & 0 deletions go/example/testExample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright (c) 2021 Uber Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file

except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the

License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing permissions and
limitations under the License.
*/
package testfiles

import "fmt"

func testExpressions(ge GoExamples) {
var x, y bool = false, false

if ge.flagMthds.treatedBehaviour(staleFlag) || x {
fmt.Println("treated || of treatedBehaviour")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, when reading treatedExample.go below, I am finding the text of this print statements hard to interpret without referring back to this file (testExample.go).

This is a very minor nit, but I think the idea is that the prints ought to be descriptive enough for the developer to know which branch of the conditional they are referring to.

If that's the case, we can make this clear by writing print messages like:

	if ge.flagMthds.treatedBehaviour(staleFlag) || x {
		fmt.Println("then-branch of `ge.flagMthds.treatedBehaviour(staleFlag) || x`")
	} else {
		fmt.Println("else-branch of `ge.flagMthds.treatedBehaviour(staleFlag) || x`")
	}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's a great idea. I will change statements.

} else {
fmt.Println("control || of treatedBehaviour")
}

if ge.flagMthds.treatedBehaviour(staleFlag) && x {
fmt.Println("treated && of treatedBehaviour")
} else {
fmt.Println("control && of treatedBehaviour")
}

if ge.flagMthds.treatedBehaviour(staleFlag) && (x || y) {
fmt.Println("treated && of || of treatedBehaviour")
} else {
fmt.Println("control && of || of treatedBehaviour")
}

if ge.flagMthds.treatedBehaviour(staleFlag) && (x && y) {
fmt.Println("treated && of && of treatedBehaviour")
} else {
fmt.Println("control && of && of treatedBehaviour")
}

if ge.flagMthds.treatedBehaviour(staleFlag) && y == x {
fmt.Println("treated && equals of treatedBehaviour")
} else {
fmt.Println("control && equals of treatedBehaviour")
}

if ge.flagMthds.controlBehaviour(staleFlag) || y == x {
fmt.Println("treated || equals of controlBehaviour")
} else {
fmt.Println("control || equals of controlBehaviour")
}

if ge.flagMthds.controlBehaviour(staleFlag) && y && x {
fmt.Println("treated && and && of controlBehaviour")
} else {
fmt.Println("control && and && of controlBehaviour")
}

if ge.flagMthds.controlBehaviour(staleFlag) || y || x {
fmt.Println("treated || && || of controlBehaviour")
} else {
fmt.Println("control || && || of controlBehaviour")
}

}
40 changes: 40 additions & 0 deletions go/example/treatedExample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright (c) 2021 Uber Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file

except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the

License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing permissions and
limitations under the License.
*/
package testfiles

import "fmt"

func testExpressions(ge GoExamples) {
var x, y bool = false, false

if x {
fmt.Println("treated || of treatedBehaviour")
} else {
fmt.Println("control || of treatedBehaviour")
}

fmt.Println("control && of treatedBehaviour")
fmt.Println("control && of || of treatedBehaviour")
fmt.Println("control && of && of treatedBehaviour")
fmt.Println("control && equals of treatedBehaviour")
fmt.Println("treated || equals of controlBehaviour")

if y && x {
fmt.Println("treated && and && of controlBehaviour")
} else {
fmt.Println("control && and && of controlBehaviour")
}

fmt.Println("treated || && || of controlBehaviour")
}
5 changes: 5 additions & 0 deletions go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/PiranhaGo

go 1.16

require github.com/dave/dst v0.26.2
37 changes: 37 additions & 0 deletions go/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
github.com/dave/dst v0.26.2 h1:lnxLAKI3tx7MgLNVDirFCsDTlTG9nKTk7GcptKcWSwY=
github.com/dave/dst v0.26.2/go.mod h1:UMDJuIRPfyUCC78eFuB+SV/WI8oDeyFDvM/JR6NI3IU=
github.com/dave/gopackages v0.0.0-20170318123100-46e7023ec56e/go.mod h1:i00+b/gKdIDIxuLDFob7ustLAVqhsZRk2qVZrArELGQ=
github.com/dave/jennifer v1.2.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg=
github.com/dave/kerr v0.0.0-20170318121727-bc25dd6abe8e/go.mod h1:qZqlPyPvfsDJt+3wHJ1EvSXDuVjFTK0j2p/ca+gtsb8=
github.com/dave/rebecca v0.9.1/go.mod h1:N6XYdMD/OKw3lkF3ywh8Z6wPGuwNFDNtWYEMFWEmXBA=
github.com/google/pprof v0.0.0-20181127221834-b4f47329b966/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/arch v0.0.0-20180920145803-b19384d3c130/go.mod h1:cYlCBUl1MsqxdiKgmc4uh7TxZfWSFLOGSRR090WDxt8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200509030707-2212a7e161a5 h1:MeC2gMlMdkd67dn17MEby3rGXRxZtWeiRXOnISfTQ74=
golang.org/x/tools v0.0.0-20200509030707-2212a7e161a5/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/src-d/go-billy.v4 v4.3.0/go.mod h1:tm33zBoOwxjYHZIE+OV8bxTWFMJLrconzFMd38aARFk=
24 changes: 24 additions & 0 deletions go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright (c) 2021 Uber Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file

except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the

License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing permissions and
limitations under the License.
*/
package main

import (
"os"

"github.com/PiranhaGo/src"
)

func main() {
src.RunPiranha(os.Args)
}
19 changes: 19 additions & 0 deletions go/properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"methodProperties": [
{
"methodName": "treatedBehaviour",
"flagType": "treated",
"argumentIndex": 0
},
{
"methodName": "controlBehaviour",
"flagType": "control",
"argumentIndex": 0
},
{
"methodName": "commonBehaviour",
"flagType": "treated",
"argumentIndex": 1
}
]
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets have files end in a newline unless we have a really good reason not to.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will do that. But like any specific reason for it? Just curious.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ending files with new line is considered best practice. One reason I know of is because command line tools might otherwise merge the last and first line of two files when processing a list of files. Also, some programs will fail to parse the last line entirely if it doesn't end in newline (not that bizarre behavior, actually, since POSIX does define a line as "A sequence of zero or more non- characters plus a terminating character.")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing.

137 changes: 137 additions & 0 deletions go/src/piranha.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
Copyright (c) 2021 Uber Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file

except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the

License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing permissions and
limitations under the License.
*/
package src

import (
"fmt"
"go/parser"
"go/token"
"log"
"os"
"strings"

"github.com/dave/dst"
"github.com/dave/dst/decorator"
)

/*
Inputs from args
Usage: ./piranha [-h] -p PROPERTIES -s SOURCE_FILE -f STALE_FLAG [-treated] [-o OUTPUT]
Required arguments:
-s SOURCE_FILE: Path of the file to be refactored
-p PROPERTIES: Configuration file (json format) for Piranha.
-f STALE_FLAG: Name of the stale flag
Optional arguments:
-h: Show the options and exit.
-treated: If this is given, then flag is treated,
otherwise it is control.
-o OUTPUT: Destination of the refactored output from piranha. If -o is not provided, then the source file is updated in place.
*/
func help() {
//Help message
fmt.Println(
"Usage: ./piranha [-h] -p PROPERTIES -s SOURCE_FILE -f STALE_FLAG [-treated] [-o OUTPUT]",
"\nRequired arguments:",
"\n\t\t\t-s SOURCE_FILE: Path of the file to be refactored.",
"\n\t\t\t-p PROPERTIES: Configuration file (json format) for Piranha.",
"\n\t\t\t-f STALE_FLAG: Name of the stale flag.",
"\nOptional arguments:",
"\n\t\t\t-h: Show the options and exit.",
"\n\t\t\t-treated: If this is given, then flag is treated,",
"\n\t\t\totherwise it is control.",
"\n\t\t\t-o OUTPUT: Destination of the refactored output from piranha.",
"\n\t\t\tIf -o is not provided, then the source file is updated in place.")
}

// RunPiranha : the main function for the piranha tool
func RunPiranha(inArgs []string) {
var sourceFile, configFile, flagName, outputFileName string = "", "", "", ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I believe it's idiomatic in Go to not explicitly initialize local variables to the default initialization for their type. In which case this should just be:

var sourceFile, configFile, flagName, outputFileName string

without the equals (strings default to "", not nil, in Go)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

var isTreated = false
sizeOfArgs := len(inArgs)
if len(inArgs) < 2 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 2? Looks to me like there are 6 mandatory arguments. That said, we probably just want to check that they are provided below, rather than use the number of arguments to distinguish this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this just to tell the people instructions when they just run ./piranha in the command line. Mandatory arguments are checked below in the file.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why you parse the arguments by hand. Unless there is a really good reason, please use the flag package (or a different one) that standardizes it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will use flag one.

help()
return
}
for index, arg := range inArgs {
switch arg {
case "-h":
help()
return
case "-p":
if index+1 < sizeOfArgs {
configFile = inArgs[index+1]
if !strings.HasSuffix(configFile, ".json") {
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of just returning, which will silently end execution (and I think it will do so with a success exit code), each of this checks, when not met, should:

  • Print a specific error message about the issue
  • Briefly remind the user to use the -h option for more info
  • Exit with a non-zero exit code.

I recommend abstracting it all under a reportArgumentError(message string) method.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

}
}
case "-s":
if index+1 < sizeOfArgs {
sourceFile = inArgs[index+1]
if !strings.HasSuffix(sourceFile, ".go") {
return
}
}
case "-f":
if index+1 < sizeOfArgs {
flagName = inArgs[index+1]
}
case "-treated":
isTreated = true
case "-o":
if index+1 < sizeOfArgs {
outputFileName = inArgs[index+1]
}
default:
break
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this break on the second argument for a command starting with:

./piranha -p properties.json [...]

since: properties.json is not one of the cases, and nothing in the processing of -p advances index?

Maybe I am missing something here? Because it looks to me like this command wouldn't run if the above is true...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will work. The index is an increment in for loop. The switch cases just check the argument and extract the value.

}
}

if flagName == "" {
fmt.Println("Please provide a flag.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, worth creating an calling a reportArgumentError method/function that prints this string and also stops the execution with a non-zero error code. You can also add the "For more info, run ./piranha -h." bit there.

Same for the following two checks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
if sourceFile == "" {
fmt.Println("Please provide a source file that is to be refactored.")
}
if configFile == "" {
fmt.Println("Please provide a config file. See README for more instructions.")
}
if flagName == "" || sourceFile == "" || configFile == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a generic argument issue reporting function that exits the program, this redundant check becomes unnecessary.

fmt.Println("For more info, run ./piranha -h.")
return
}

fs := token.NewFileSet()
parsed, err := decorator.ParseFile(fs, sourceFile, nil, parser.ParseComments)
if err != nil {
log.Fatal(err)
}

var cleaner staleFlagCleaner
cleaner.init(configFile, flagName, isTreated)
newRoot := cleaner.run(parsed)
////////////////////////
// For debugging purpose. It prints out the ast.
// spew.Dump(newRoot)
///////////////////////
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove (not just comment out) before landing PR, or else (possibly) guard under some --debug flag for the tool.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing it.


if outputFileName == "" {
outputFileName = sourceFile
}
outputFile, err := os.Create(outputFileName)
/*
Here we are typecasting newRoot to dst.File.It is safe because the root of AST
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space before "It". Or, possibly better in this case: new line.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

always starts with the dst.File type.
*/
decorator.Fprint(outputFile, newRoot.(*dst.File))
}
Loading