Skip to content

Commit

Permalink
Make better checks and fix ordering Structs
Browse files Browse the repository at this point in the history
- See #6 - in progress to resolve
- Zee #4 - in propose to resolve
  • Loading branch information
metal3d committed Jun 22, 2023
1 parent 69d87de commit 55fb023
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 26 deletions.
60 changes: 36 additions & 24 deletions ordering/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"crypto/sha256"
"errors"
"fmt"
"go/format"
"io/ioutil"
"log"
"os"
"os/exec"
"sort"
Expand Down Expand Up @@ -62,13 +62,8 @@ func ReorderSource(opt ReorderConfig) (string, error) {
})
}

structNames := make([]string, 0, len(info.Methods))
for _, s := range info.Structs {
log.Println("s.Name", s.Name)
structNames = append(structNames, s.Name)
}
if opt.ReorderStructs {
sort.Strings(structNames)
info.StructNames.Sort()
}

// Get the source code signature - we will use this to mark the lines to remove later
Expand All @@ -82,7 +77,7 @@ func ReorderSource(opt ReorderConfig) (string, error) {

lineNumberWhereInject := 0
removedLines := 0
for _, typename := range structNames {
for _, typename := range *info.StructNames {
if removedLines == 0 {
lineNumberWhereInject = info.Structs[typename].OpeningLine
}
Expand Down Expand Up @@ -129,33 +124,50 @@ func ReorderSource(opt ReorderConfig) (string, error) {
output := strings.Join(originalContent, "\n")

// write in a temporary file and use "gofmt" to format it
newcontent := []byte(output)
switch opt.FormatCommand {
case "gofmt":
// format the temporary file
newcontent, err = format.Source([]byte(output))
if err != nil {
return string(content), errors.New("Failed to format source: " + err.Error())
}
default:
if newcontent, err = formatWithCommand(content, output, opt); err != nil {
return string(content), errors.New("Failed to format source: " + err.Error())
}
}

if opt.Diff {
return doDiff(content, newcontent, opt.Filename)
}
return string(newcontent), nil
}

func formatWithCommand(content []byte, output string, opt ReorderConfig) (newcontent []byte, err error) {
// we use the format command given by the user
// on a temporary file we need to create and remove
tmpfile, err := ioutil.TempFile("", "")
if err != nil {
return string(content), errors.New("Failed to create temp file: " + err.Error())
return content, errors.New("Failed to create temp file: " + err.Error())
}
defer func() {
// close and remove the temporary file
tmpfile.Close()
os.Remove(tmpfile.Name())
}()
defer os.Remove(tmpfile.Name())

// write the temporary file
if _, err := tmpfile.Write([]byte(output)); err != nil {
return string(content), errors.New("Failed to write to temporary file: " + err.Error())
return content, errors.New("Failed to write temp file: " + err.Error())
}
tmpfile.Close()

// format the temporary file
cmd := exec.Command(opt.FormatCommand, "-w", tmpfile.Name())
if err := cmd.Run(); err != nil {
return string(content), err
return content, err
}

// read the temporary file
newcontent, err := ioutil.ReadFile(tmpfile.Name())
newcontent, err = ioutil.ReadFile(tmpfile.Name())
if err != nil {
return string(content), errors.New("Read Temporary File error: " + err.Error())
return content, errors.New("Read Temporary File error: " + err.Error())
}

if opt.Diff {
return doDiff(content, newcontent, opt.Filename)
}
return string(newcontent), nil
return newcontent, nil
}
126 changes: 126 additions & 0 deletions ordering/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,129 @@ func (f *Foo) FooMethod1() {
t.Error(err)
}
}

func TestNoOrderStructs(t *testing.T) {
const source = `package main
type grault struct {}
type xyzzy struct {}
type bar struct {}
type qux struct {}
type quux struct {}
type corge struct {}
type garply struct {}
type baz struct {}
type waldo struct {}
type fred struct {}
type plugh struct {}
type foo struct {}
`
const expected = `package main
type grault struct{}
type xyzzy struct{}
type bar struct{}
type qux struct{}
type quux struct{}
type corge struct{}
type garply struct{}
type baz struct{}
type waldo struct{}
type fred struct{}
type plugh struct{}
type foo struct{}
`

const orderedSource = `package main
type bar struct{}
type baz struct{}
type corge struct{}
type foo struct{}
type fred struct{}
type garply struct{}
type grault struct{}
type plugh struct{}
type quux struct{}
type qux struct{}
type waldo struct{}
type xyzzy struct{}
`

content, err := ReorderSource(ReorderConfig{
Filename: "foo.go",
FormatCommand: "gofmt",
ReorderStructs: false,
Src: []byte(source),
Diff: false,
})
if err != nil {
t.Error(err)
}
if content != expected {
t.Errorf("Expected UNORDERED:\n%s\nGot:\n%s\n", expected, content)
}

content, err = ReorderSource(ReorderConfig{
Filename: "foo.go",
FormatCommand: "gofmt",
ReorderStructs: true,
Src: []byte(source),
Diff: false,
})
if err != nil {
t.Error(err)
}
if content != orderedSource {
t.Errorf("Expected ORDERED:\n%s\nGot:\n%s\n", orderedSource, content)
}

}

func TestBadFormatCommand(t *testing.T) {
const source = `package main
import (
"os"
"fmt"
)
type grault struct {}
type xyzzy struct {}
type bar struct {}
`
content, err := ReorderSource(ReorderConfig{
Filename: "foo.go",
FormatCommand: "wthcommand",
ReorderStructs: false,
Src: []byte(source),
Diff: false,
})

if err == nil {
t.Error("Expected error, got nil")
}
if content != source {
t.Errorf("Expected:\n%s\nGot:\n%s\n", source, content)
}
}
8 changes: 6 additions & 2 deletions ordering/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ParsedInfo struct {
Structs map[string]*GoType
Constants map[string]*GoType
Variables map[string]*GoType
StructNames *StingList
}

// GetMethodComments returns the comments for the given method.
Expand Down Expand Up @@ -66,6 +67,7 @@ func Parse(filename string, src interface{}) (*ParsedInfo, error) {
methods = make(map[string][]*GoType)
constructors = make(map[string][]*GoType)
structTypes = make(map[string]*GoType)
structNames = &StingList{}
varTypes = make(map[string]*GoType)
constTypes = make(map[string]*GoType)
sourceCode []byte
Expand All @@ -87,7 +89,7 @@ func Parse(filename string, src interface{}) (*ParsedInfo, error) {
findMethods(d, fset, sourceLines, methods)
// find struct declarations
case *ast.GenDecl:
findStructs(d, fset, sourceLines, structTypes)
findStructs(d, fset, sourceLines, structNames, structTypes)
findGlobalVarsAndConsts(d, fset, sourceLines, varTypes, constTypes)
}
}
Expand All @@ -102,14 +104,15 @@ func Parse(filename string, src interface{}) (*ParsedInfo, error) {

return &ParsedInfo{
Structs: structTypes,
StructNames: structNames,
Methods: methods,
Constructors: constructors,
Variables: varTypes,
Constants: constTypes,
}, nil
}

func findStructs(d *ast.GenDecl, fset *token.FileSet, sourceLines []string, structTypes map[string]*GoType) {
func findStructs(d *ast.GenDecl, fset *token.FileSet, sourceLines []string, stuctNames *StingList, structTypes map[string]*GoType) {
if d.Tok != token.TYPE {
return
}
Expand All @@ -132,6 +135,7 @@ func findStructs(d *ast.GenDecl, fset *token.FileSet, sourceLines []string, stru
typeDef.OpeningLine -= len(comments)

structTypes[s.Name.Name] = typeDef
stuctNames.Add(s.Name.Name)
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions ordering/sortString.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ordering

import "sort"

var _ sort.Interface = (*StingList)(nil)

// StingList is a list of strings that *can* be sorted.
//
// Implement sort.Interface
type StingList []string

// Len returns the length of the list.
func (s *StingList) Len() int {
return len(*s)
}

// Swap swaps the elements with indexes i and j.
func (s StingList) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

// Less reports whether the element with index i should sort before the element with index j.
func (s StingList) Less(i, j int) bool {
return s[i] < s[j]
}

// Sort sorts the list.
func (s *StingList) Sort() {
sort.Sort(s)
}

// Add adds a string to the list.
func (s *StingList) Add(str string) {
*s = append(*s, str)
}

0 comments on commit 55fb023

Please sign in to comment.