Skip to content

Commit

Permalink
refactor packages
Browse files Browse the repository at this point in the history
  • Loading branch information
marhaupe committed Aug 10, 2019
1 parent e691f02 commit ab4d73c
Show file tree
Hide file tree
Showing 60 changed files with 102 additions and 99 deletions.
21 changes: 2 additions & 19 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
Expand All @@ -11,7 +10,6 @@ import (

"github.com/marhaupe/json2struct/internal/editor"
"github.com/marhaupe/json2struct/internal/generator"
"github.com/marhaupe/json2struct/internal/parse"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -58,12 +56,12 @@ func rootFunc(cmd *cobra.Command, args []string) {
defer benchmark()()
}

generatedCode, err := generate(userInput)
output, err := generator.GenerateOutputFromString(userInput)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(generatedCode)
fmt.Println(output)
}

func readFromFile() string {
Expand Down Expand Up @@ -110,18 +108,3 @@ func benchmark() func() {
fmt.Printf("generating took %v\n", time.Since(start))
}
}

func generate(json string) (string, error) {
node, err := parse.ParseFromString("json2struct", json)
if err != nil {
return "", err
}

file, err := generator.GenerateFile(node)
if err != nil {
return "", err
}
buf := &bytes.Buffer{}
err = file.Render(buf)
return buf.String(), err
}
78 changes: 0 additions & 78 deletions cmd/root_test.go

This file was deleted.

25 changes: 24 additions & 1 deletion internal/generator/generator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generator

import (
"bytes"
"errors"
"fmt"
"sort"
Expand All @@ -12,7 +13,29 @@ import (
"github.com/dave/jennifer/jen"
)

func GenerateFile(tree parse.Node) (*jen.File, error) {
func GenerateOutputFromString(s string) (string, error) {
generatedFile, err := GenerateFileFromString(s)
if err != nil {
return "", err
}

buf := &bytes.Buffer{}
err = generatedFile.Render(buf)
if err != nil {
return "", fmt.Errorf("error rendering file: %v", err)
}
return buf.String(), nil
}

func GenerateFileFromString(s string) (*jen.File, error) {
node, err := parse.ParseFromString("json2struct", s)
if err != nil {
return nil, err
}
return GenerateFileFromAST(node)
}

func GenerateFileFromAST(tree parse.Node) (*jen.File, error) {
g := Generator{
Tree: tree,
currentNode: tree,
Expand Down
77 changes: 76 additions & 1 deletion internal/generator/generator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package generator

import "testing"
import (
"go/format"
"io/ioutil"
"path"
"strings"
"testing"
)

func Test_isNumber(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -45,3 +51,72 @@ func Test_isNumber(t *testing.T) {
})
}
}

const dirName = "testdata"
const expectedSuffix = "_expected"

func TestFiles(t *testing.T) {
inputFiles, err := listValidInputFiles()
if err != nil {
t.Fatal("Error reading input files", err)
}

for _, filename := range inputFiles {

input := readFile(filename)
expected := readFile(filename + expectedSuffix)
actual, err := GenerateOutputFromString(input)
if err != nil {
t.Errorf("Test resulted in error. Filename: %v, Error: %v", filename, err)
}

formatExpectedBytes, _ := format.Source([]byte(expected))
formatActualBytes, _ := format.Source([]byte(actual))

expected = string(formatExpectedBytes)
actual = string(formatActualBytes)

if actual != expected {
t.Errorf("Test failed. Filename: %v\nActual: %v\nActualLen: %v\nExpected: %v\nExpectedLen: %v\n",
filename, actual, len(actual), expected, len(expected))
}
}
}

func listValidInputFiles() ([]string, error) {
dirFiles, err := ioutil.ReadDir(dirName)
if err != nil {
return nil, err
}

var inputFiles []string
inputFileHasExpectedFile := make(map[string]bool, 2)

for _, f := range dirFiles {
fileName := f.Name()
isExpectedFile := strings.HasSuffix(fileName, expectedSuffix)
if isExpectedFile {
inputFileNameLength := strings.Index(fileName, expectedSuffix)
inputFileName := fileName[:inputFileNameLength]
inputFileHasExpectedFile[inputFileName] = true
} else {
inputFiles = append(inputFiles, fileName)
}
}

var validInputFiles []string
for _, f := range inputFiles {
if inputFileHasExpectedFile[f] {
validInputFiles = append(validInputFiles, path.Join(dirName, f))
}
}
return validInputFiles, nil
}

func readFile(filename string) string {
content, err := ioutil.ReadFile(filename)
if err != nil {
panic("error reading file " + filename)
}
return string(content)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit ab4d73c

Please sign in to comment.