Skip to content

Commit

Permalink
add linter
Browse files Browse the repository at this point in the history
  • Loading branch information
coryb committed Nov 8, 2020
1 parent 87b81de commit 7138767
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 29 deletions.
30 changes: 30 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
linters:
enable-all: false
disable-all: true
enable:
- deadcode
- depguard
- gocritic
- gofmt
- goimports
- golint
- gosimple
- govet
- ineffassign
- misspell
- scopelint
- structcheck
- typecheck
- unconvert
- unparam
- unused
- varcheck

issues:
exclude-use-default: false
exclude:
- ST1005
- "should be [^ ]*(SSH|JSON|XML|UID|ID|API|URL|URI|HTTP|HTML|IP)"
- 'result .* is always (false|nil|`nil`)'
- 'result .* is never used'
- "and that stutters"
37 changes: 20 additions & 17 deletions slipscheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,22 @@ func (s *SchemaType) MarshalJSON() ([]byte, error) {
}

func main() {
exitCode := Main(os.Args, Stdio{
exitCode := runMain(os.Args, Stdio{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
})
os.Exit(exitCode)
}

// Stdio holds common io readers/writers
type Stdio struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}

func Main(arguments []string, io Stdio) int {
func runMain(arguments []string, io Stdio) int {
flags := flag.NewFlagSet(arguments[0], flag.ExitOnError)
outputDir := flags.String("dir", ".", "output directory for go files")
pkgName := flags.String("pkg", "main", "package namespace for go files")
Expand Down Expand Up @@ -246,17 +247,18 @@ func setRoot(root, schema *Schema) {
var ctx interface{}
ctx = schema
for _, part := range schemaPath {
if part == "#" {
switch part {
case "#":
ctx = root
} else if part == "definitions" {
case "definitions":
ctx = ctx.(*Schema).Definitions
} else if part == "properties" {
case "properties":
ctx = ctx.(*Schema).Properties
} else if part == "patternProperties" {
case "patternProperties":
ctx = ctx.(*Schema).PatternProperties
} else if part == "items" {
case "items":
ctx = ctx.(*Schema).Items
} else {
default:
if cast, ok := ctx.(map[string]*Schema); ok {
ctx = cast[part]
}
Expand All @@ -277,7 +279,7 @@ func camelCase(name string) string {
return ' '
}, name),
)
caseName = strings.Replace(caseName, " ", "", -1)
caseName = strings.ReplaceAll(caseName, " ", "")

for _, suffix := range []string{"Id", "Url", "Json", "Xml"} {
if strings.HasSuffix(caseName, suffix) {
Expand All @@ -303,7 +305,8 @@ func (s *SchemaProcessor) structComment(schema *Schema, typeName string) string
}

func (s *SchemaProcessor) processSchema(schema *Schema) (typeName string, err error) {
if schema.Type == OBJECT {
switch schema.Type {
case OBJECT:
typeName = camelCase(schema.Name())
if schema.Properties != nil {
typeData := fmt.Sprintf("%stype %s struct {\n", s.structComment(schema, typeName), typeName)
Expand Down Expand Up @@ -350,7 +353,7 @@ func (s *SchemaProcessor) processSchema(schema *Schema) (typeName string, err er
}
}
}
} else if schema.Type == ARRAY {
case ARRAY:
subTypeName, err := s.processSchema(schema.Items)
if err != nil {
return "", err
Expand All @@ -375,15 +378,15 @@ func (s *SchemaProcessor) processSchema(schema *Schema) (typeName string, err er
} else {
typeName = fmt.Sprintf("[]%s", subTypeName)
}
} else if schema.Type == BOOLEAN {
case BOOLEAN:
typeName = "bool"
} else if schema.Type == INTEGER {
case INTEGER:
typeName = "int"
} else if schema.Type == NUMBER {
case NUMBER:
typeName = "float64"
} else if schema.Type == NULL || schema.Type == ANY {
case NULL, ANY:
typeName = "interface{}"
} else if schema.Type == STRING {
case STRING:
typeName = "string"
}
return
Expand Down Expand Up @@ -427,7 +430,7 @@ func (s *SchemaProcessor) writeGoCode(typeName, code string) error {
if err != nil {
return err
}
//defer fh.Close()
defer fh.Close()
preamble := fmt.Sprintf("package %s\n", s.PackageName)
preamble += fmt.Sprintf(`
/////////////////////////////////////////////////////////////////////////
Expand Down
24 changes: 12 additions & 12 deletions slipscheme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func ExampleUsage() {
Main([]string{"slipscheme"}, Stdio{
runMain([]string{"slipscheme"}, Stdio{
Stdout: os.Stdout,
})
// Output:
Expand All @@ -31,7 +31,7 @@ func ExampleUsage() {
func TestOutputFiles(t *testing.T) {
tdir, err := ioutil.TempDir("", "slipscheme")
noError(t, err)
Main([]string{"slipscheme", "-dir", tdir, "sample.json"}, Stdio{
runMain([]string{"slipscheme", "-dir", tdir, "sample.json"}, Stdio{
Stdout: os.Stdout,
})
exists(t, filepath.Join(tdir, "Contact.go"))
Expand All @@ -40,15 +40,15 @@ func TestOutputFiles(t *testing.T) {
exists(t, filepath.Join(tdir, "Phones.go"))
}

func ExampleStdoutSlice() {
func Example_stdoutSlice() {
input := `{
"title": "stuff",
"type": "array",
"items": {
"type": "string"
}
}`
Main([]string{"slipscheme", "-stdout=true", "-"}, Stdio{
runMain([]string{"slipscheme", "-stdout=true", "-"}, Stdio{
Stdin: strings.NewReader(input),
Stdout: os.Stdout,
})
Expand All @@ -65,15 +65,15 @@ func ExampleStdoutSlice() {
// type Stuff []string
}

func ExampleNoComments() {
func Example_noComments() {
input := `{
"title": "stuff",
"type": "array",
"items": {
"type": "string"
}
}`
Main([]string{"slipscheme", "-stdout=true", "-comments=false", "-"}, Stdio{
runMain([]string{"slipscheme", "-stdout=true", "-comments=false", "-"}, Stdio{
Stdin: strings.NewReader(input),
Stdout: os.Stdout,
})
Expand All @@ -82,7 +82,7 @@ func ExampleNoComments() {
// type Stuff []string
}

func ExampleStruct() {
func Example_struct() {
input := `{
"title": "thing",
"type": "object",
Expand All @@ -95,7 +95,7 @@ func ExampleStruct() {
}
}
}`
Main([]string{"slipscheme", "-stdout=true", "-comments=false", "-"}, Stdio{
runMain([]string{"slipscheme", "-stdout=true", "-comments=false", "-"}, Stdio{
Stdin: strings.NewReader(input),
Stdout: os.Stdout,
})
Expand All @@ -107,7 +107,7 @@ func ExampleStruct() {
// }
}

func ExampleNoFmt() {
func Example_noFmt() {
input := `{
"title": "thing",
"type": "object",
Expand All @@ -120,7 +120,7 @@ func ExampleNoFmt() {
}
}
}`
Main([]string{"slipscheme", "-stdout=true", "-comments=false", "-fmt=false", "-"}, Stdio{
runMain([]string{"slipscheme", "-stdout=true", "-comments=false", "-fmt=false", "-"}, Stdio{
Stdin: strings.NewReader(input),
Stdout: os.Stdout,
})
Expand All @@ -132,7 +132,7 @@ func ExampleNoFmt() {
// }
}

func ExampleStructSlice() {
func Example_structSlice() {
input := `{
"title": "stuff",
"type": "array",
Expand All @@ -149,7 +149,7 @@ func ExampleStructSlice() {
}
}
}`
Main([]string{"slipscheme", "-stdout=true", "-comments=false", "-"}, Stdio{
runMain([]string{"slipscheme", "-stdout=true", "-comments=false", "-"}, Stdio{
Stdin: strings.NewReader(input),
Stdout: os.Stdout,
})
Expand Down

0 comments on commit 7138767

Please sign in to comment.