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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This repository contains four independent versions of Piranha, one for each of t

To use/build each version, look under the corresponding [lang]/ directory and follow instructions in the corresponding [lang]/README.md file. Make sure to cd into that directory to build any related code following the instructions in the README.

- [PiranhaGo](go/README.md)
- [PiranhaJava](java/README.md)
- [PiranhaJS](javascript/README.md)
- [PiranhaObjC](objc/README.md)
Expand Down
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 -mode MODE_NAME [-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.
-mode MODE_NAME: If MODE_NAME=treated, then flag is treated,
otherwise MODE_NAME=control, it is control.
Optional arguments:
-h: Show the options and exit.
-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 -mode control` command in root directory. You will get your refactored file as `/example/treatedExample.go`.
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("then-branch of `ge.flagMthds.treatedBehaviour(staleFlag) || x`")
} else {
fmt.Println("else-branch of `ge.flagMthds.treatedBehaviour(staleFlag) || x`")
}

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`")
}

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

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

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

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

if ge.flagMthds.controlBehaviour(staleFlag) && y && x {
fmt.Println("then-branch of `ge.flagMthds.controlBehaviour(staleFlag) && y && x`")
} else {
fmt.Println("else-branch of `ge.flagMthds.controlBehaviour(staleFlag) && y && x`")
}

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

}
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("then-branch of `ge.flagMthds.treatedBehaviour(staleFlag) || x`")
} else {
fmt.Println("else-branch of `ge.flagMthds.treatedBehaviour(staleFlag) || x`")
}

fmt.Println("else-branch of `ge.flagMthds.treatedBehaviour(staleFlag) && x`")
fmt.Println("else-branch of `ge.flagMthds.treatedBehaviour(staleFlag) && (x || y)`")
fmt.Println("else-branch of `ge.flagMthds.treatedBehaviour(staleFlag) && (x && y)`")
fmt.Println("else-branch of `ge.flagMthds.treatedBehaviour(staleFlag) && y == x`")
fmt.Println("then-braanch of `ge.flagMthds.controlBehaviour(staleFlag) || y == x`")

if y && x {
fmt.Println("then-branch of `ge.flagMthds.controlBehaviour(staleFlag) && y && x`")
} else {
fmt.Println("else-branch of `ge.flagMthds.controlBehaviour(staleFlag) && y && x`")
}

fmt.Println("then-braanch of `ge.flagMthds.controlBehaviour(staleFlag) || y || x`")
}
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=
44 changes: 44 additions & 0 deletions go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
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 (
"flag"

"github.com/PiranhaGo/src"
)

func main() {
var sourceFile, configFile, flagName, outputFileName, modeName string
var isTreated = false

// get configFile
flag.StringVar(&configFile, "p", "PROPERTIES", "Configuration file (json format) for Piranha.")
// get sourceFile
flag.StringVar(&sourceFile, "s", "SOURCE_FILE", "Path of the file to be refactored.")
// get flagName
flag.StringVar(&flagName, "f", "STALE_FLAG", "Name of the stale flag.")
// check treatedMode
flag.StringVar(&modeName, "mode", "MODE_NAME", "If MODE_NAME=treated, then flag is treated, otherwise MODE_NAME=control, it is control.")
if modeName == "treated" {

Choose a reason for hiding this comment

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

It should be excuted after L40 flag.Parse().

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for pointing it out. I changed it now.

isTreated = true
}
// get outputFileName
flag.StringVar(&outputFileName, "o", "OUTPUT", "Destination of the refactored output from piranha. If -o is not provided, then the source file is updated in place.")

flag.Parse()


src.RunPiranha(sourceFile, configFile, flagName, outputFileName, isTreated)
}
20 changes: 20 additions & 0 deletions go/properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"methodProperties": [
{
"methodName": "treatedBehaviour",
"flagType": "treated",
"argumentIndex": 0
},
{
"methodName": "controlBehaviour",
"flagType": "control",
"argumentIndex": 0
},
{
"methodName": "commonBehaviour",
"flagType": "treated",
"argumentIndex": 1
}
]
}

105 changes: 105 additions & 0 deletions go/src/piranha.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
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 -mode MODE_NAME [-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.
-mode MODE_NAME: If MODE_NAME=treated, then flag is treated,
otherwise MODE_NAME=control, it is control.
Optional arguments:
-h: Show the options and exit.
-o OUTPUT: Destination of the refactored output from piranha. If -o is not provided, then the source file is updated in place.
*/
func reportArgumentError(arg string) {
switch arg {
case "configFileSuffix":
fmt.Println("Please provide configuration file of json format.")
case "sourceFileSuffix":
fmt.Println("Please provide source file of go format.")
case "flagName":
fmt.Println("Please provide a flag.")
case "configFile":
fmt.Println("Please provide a config file. See README for more instructions.")
case "sourceFile":
fmt.Println("Please provide a source file that is to be refactored.")
}
fmt.Println("For more info, run ./piranha -h.")
}

// RunPiranha : the main function for the piranha tool
func RunPiranha(sourceFile string, configFile string, flagName string, outputFileName string, isTreated bool) {
if flagName == "STALE_FLAG" {
reportArgumentError("flagName")
return
}
if sourceFile == "SOURCE_FILE" {
reportArgumentError("sourceFile")
return
}
if configFile == "PROPERTIES" {
reportArgumentError("configFile")
return
}

if !strings.HasSuffix(configFile, ".json") {
reportArgumentError("sourceFileSuffix")
return
}

if !strings.HasSuffix(sourceFile, ".go") {
reportArgumentError("sourceFileSuffix")
return
}

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

var cleaner staleFlagCleaner
err = cleaner.init(configFile, flagName, isTreated)
if err != nil {
log.Fatal(err)
}

newRoot := cleaner.run(parsed)

if outputFileName == "" {
outputFileName = sourceFile
}
outputFile, _ := os.Create(outputFileName)
/*
Here we are typecasting newRoot to dst.File. It is safe because the root of AST
always starts with the dst.File type.
*/
decorator.Fprint(outputFile, newRoot.(*dst.File))
}
Loading