Skip to content
This repository has been archived by the owner on Jan 24, 2020. It is now read-only.

Commit

Permalink
Merge pull request #8 from ewgRa/ISSUE-3-group-stdlib-import
Browse files Browse the repository at this point in the history
group stdlib import (#3)
  • Loading branch information
ewgRa authored Jul 24, 2018
2 parents 1e2f92a + 57a3ba1 commit f12ed4b
Show file tree
Hide file tree
Showing 10 changed files with 986 additions and 73 deletions.
8 changes: 8 additions & 0 deletions cmd/gocsfixer/.gocsfixer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ fixers:
recommend: true
lint: true
fix: true

group_import:
recommend: true
lint: true
fix: true
options:
stdLib: true
lintText: Group stdLib imports
27 changes: 7 additions & 20 deletions fixers/file_header.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
package fixers

import (
"errors"
"strings"
)

func init() {
AddFixer("file_header", func(options FixerOptions) (CsFixer, error) {
headerOption, ok := options["header"]
header, err := options.extractRequiredString("header")

if !ok {
return nil, errors.New("Header option is required")
if err != nil {
return nil, err
}

header, ok := headerOption.(string)
lintText, err := options.extractString("lintText", "File header")

if !ok {
return nil, errors.New("Wrong header option")
}

lintText := "File header"

lintTextOption, ok := options["lintText"]

if ok {
lintText, ok = lintTextOption.(string)

if !ok {
return nil, errors.New("Wrong header option")
}
if err != nil {
return nil, err
}

return &FileHeaderCsFixer{header: header, lintText: lintText}, nil
Expand All @@ -41,7 +28,7 @@ type FileHeaderCsFixer struct {
}

func (l *FileHeaderCsFixer) Lint(content string) (Problems, error) {
var problems []*Problem
var problems Problems

if !strings.HasPrefix(content, l.header) {
lines := strings.Split(content, "\n")
Expand Down
23 changes: 12 additions & 11 deletions fixers/file_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,29 @@ func TestFileHeaderFix(t *testing.T) {
}

func fileHeaderTestTable() []fixerTestCase {
cases := []fixerTestCase{
return []fixerTestCase{
{
"\npackage main\n\n" +
"func main() {\n\n" +
"}",
"// Header\n\n" +
"package main\n\n" +
"func main() {\n\n" +
"}",
`package main
func main() {
}`,
`// Header
package main
func main() {
}`,
fixers.Problems{
&fixers.Problem{Position: &fixers.Position{Line: 1}, Text: "License header required"},
},
},
}

return cases
}

func createFileHeaderFixer() *fixers.FileHeaderCsFixer {
mapFixer, _ := fixers.CreateFixer(
"file_header",
fixers.FixerOptions{"header": "// Header\n", "lintText": "License header required"},
fixers.FixerOptions{"header": "// Header\n\n", "lintText": "License header required"},
)

return mapFixer.(*fixers.FileHeaderCsFixer)
Expand Down
48 changes: 47 additions & 1 deletion fixers/fixer.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,54 @@
package fixers

import "fmt"
import (
"errors"
"fmt"
)

type FixerOptions map[interface{}]interface{}

func (o FixerOptions) extractRequiredString(name string) (string, error) {
_, ok := o[name]

if !ok {
return "", errors.New(name + " option is required")
}

return o.extractString(name, "")
}

func (o FixerOptions) extractString(name string, defaultValue string) (string, error) {
v, ok := o[name]

if !ok {
return defaultValue, nil
}

value, ok := v.(string)

if !ok {
return "", errors.New("Wrong " + name + " option")
}

return value, nil
}

func (o FixerOptions) extractBool(name string, defaultValue bool) (bool, error) {
v, ok := o[name]

if !ok {
return defaultValue, nil
}

value, ok := v.(bool)

if !ok {
return false, errors.New("Wrong " + name + " option")
}

return value, nil
}

type FixerCreateFunc func(options FixerOptions) (CsFixer, error)

var FixersMap map[string]FixerCreateFunc
Expand Down
Loading

0 comments on commit f12ed4b

Please sign in to comment.