Skip to content

Commit

Permalink
Adding support to json #34
Browse files Browse the repository at this point in the history
To allow users to extract data to another applications we are putting support to json output.
  • Loading branch information
filhodanuvem authored Dec 17, 2016
2 parents 0b3ac2f + e379a64 commit 9d47f3b
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ Notes:
* The limit default is 10 rows
* It's inspired by [textql](https://github.com/dinedal/textql)
* But, why gitql is a compiler/interpreter instead of just read a sqlite database with all commits, tags and etc? Answer: Because we would need to sync the tables every time before run sql and we would have sqlite bases for each repository. :neutral_face:

8 changes: 5 additions & 3 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

var path *string
var query string
var typeFormat *string

func init() {
parseCommandLine()
Expand All @@ -32,17 +33,18 @@ func printTables() {
if i + 1 < len(fields) {
comma = ", "
}
fmt.Printf("%s%s", field, comma)
fmt.Printf("%s%s", field, comma)
}
fmt.Println()
}

}

func parseCommandLine() {
path = flag.String("p", ".", "The (optional) path to run gitql")
version := flag.Bool("v", false, "The version of gitql")
showTables := flag.Bool("show-tables", false, "Show all tables")
typeFormat = flag.String("type", "table", "The output type format {table|json}")
flag.Usage = usage
flag.Parse()

Expand All @@ -63,4 +65,4 @@ func parseCommandLine() {
flag.Usage()
os.Exit(1)
}
}
}
2 changes: 1 addition & 1 deletion gitql.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ func main() {
log.Fatalln(errGit)
}

runtime.Run(ast)
runtime.Run(ast, typeFormat)
}
9 changes: 6 additions & 3 deletions runtime/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
)

func walkCommits(n *parser.NodeProgram, visitor *RuntimeVisitor) {
func walkCommits(n *parser.NodeProgram, visitor *RuntimeVisitor) *TableData{
builder.walk, _ = repo.Walk()
builder.walk.PushHead()
builder.walk.Sorting(git.SortTime)
Expand Down Expand Up @@ -57,7 +57,10 @@ func walkCommits(n *parser.NodeProgram, visitor *RuntimeVisitor) {
}
rowsSliced = rowsSliced[0:counter]
}
printTable(rowsSliced, fields)
tableData := new(TableData)
tableData.rows = rowsSliced
tableData.fields = fields
return tableData
}

func metadataCommit(identifier string, object *git.Commit) string {
Expand Down Expand Up @@ -99,4 +102,4 @@ func metadataCommit(identifier string, object *git.Commit) string {
log.Fatalf("Field %s not implemented yet \n", identifier)

return ""
}
}
11 changes: 7 additions & 4 deletions runtime/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"
)

func walkReferences(n *parser.NodeProgram, visitor *RuntimeVisitor) {
func walkReferences(n *parser.NodeProgram, visitor *RuntimeVisitor) *TableData{
s := n.Child.(*parser.NodeSelect)
where := s.Where

Expand Down Expand Up @@ -54,8 +54,11 @@ func walkReferences(n *parser.NodeProgram, visitor *RuntimeVisitor) {
}
rowsSliced = rowsSliced[0:counter]
}
printTable(rowsSliced, fields)
}
tableData := new(TableData)
tableData.rows = rowsSliced
tableData.fields = fields
return tableData
}

func metadataReference(identifier string, object *git.Reference) string {
key := ""
Expand Down Expand Up @@ -96,4 +99,4 @@ func metadataReference(identifier string, object *git.Reference) string {
log.Fatalf("Field %s not implemented yet in reference\n", identifier)

return ""
}
}
11 changes: 7 additions & 4 deletions runtime/remotes.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package runtime
package runtime

import (
"github.com/cloudson/git2go"
"github.com/cloudson/gitql/parser"
"log"
)

func walkRemotes(n *parser.NodeProgram, visitor *RuntimeVisitor) {
func walkRemotes(n *parser.NodeProgram, visitor *RuntimeVisitor) *TableData{
s := n.Child.(*parser.NodeSelect)
where := s.Where

Expand Down Expand Up @@ -56,7 +56,10 @@ func walkRemotes(n *parser.NodeProgram, visitor *RuntimeVisitor) {
}
rowsSliced = rowsSliced[0:counter]
}
printTable(rowsSliced, fields)
tableData := new(TableData)
tableData.rows = rowsSliced
tableData.fields = fields
return tableData
}

func metadataRemote(identifier string, object *git.Remote) string {
Expand Down Expand Up @@ -85,4 +88,4 @@ func metadataRemote(identifier string, object *git.Remote) string {
log.Fatalf("Field %s not implemented yet \n", identifier)

return ""
}
}
39 changes: 32 additions & 7 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/crackcomm/go-clitable"
"github.com/cloudson/git2go"
"log"
"encoding/json"
)

const (
Expand Down Expand Up @@ -52,6 +53,11 @@ type RuntimeVisitor struct {
semantical.Visitor
}

type TableData struct {
rows []tableRow
fields []string
}

// =========================== Error

func (e *RuntimeError) Error() string {
Expand All @@ -67,24 +73,32 @@ func throwRuntimeError(message string, code uint8) *RuntimeError {
}

// =========================== Runtime
func Run(n *parser.NodeProgram) {
func Run(n *parser.NodeProgram, typeFormat *string) {
builder = GetGitBuilder(n.Path)
visitor := new(RuntimeVisitor)
err := visitor.Visit(n)
if err != nil {
log.Fatalln(err)
}
var tableData *TableData

switch findWalkType(n) {
case WALK_COMMITS:
walkCommits(n, visitor)
tableData = walkCommits(n, visitor)
break
case WALK_REFERENCES:
walkReferences(n, visitor)
tableData = walkReferences(n, visitor)
break
case WALK_REMOTES:
walkRemotes(n, visitor)
tableData = walkRemotes(n, visitor)
break
}

if *typeFormat == "json" {
printJson(tableData)
} else {
printTable(tableData)
}
}

func findWalkType(n *parser.NodeProgram) uint8 {
Expand All @@ -101,14 +115,25 @@ func findWalkType(n *parser.NodeProgram) uint8 {
return builder.currentWalkType
}

func printTable(rows []tableRow, fields []string) {
table := clitable.New(fields)
for _, r := range rows {
func printTable(tableData *TableData) {
table := clitable.New(tableData.fields)
for _, r := range tableData.rows {
table.AddRow(r)
}
table.Print()
}

func printJson(tableData *TableData) error {
res, err := json.Marshal(tableData.rows)
if err != nil {
log.Fatalln(err)
return throwRuntimeError(fmt.Sprintf("Json error:'%s'", err), 0)
} else {
fmt.Println(string(res))
}
return nil
}

func orderTable(rows []tableRow, order *parser.NodeOrder) []tableRow {
if order == nil {
return rows
Expand Down
26 changes: 26 additions & 0 deletions runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package runtime
import (
"path/filepath"
"testing"
"github.com/cloudson/gitql/parser"
"github.com/cloudson/gitql/semantical"
)

func TestErrorWithInvalidTables(t *testing.T) {
Expand Down Expand Up @@ -78,3 +80,27 @@ func TestFoundFieldsFromTable(t *testing.T) {
}
}
}

func TestCanConvertToTypeFormats(t *testing.T) {
folder, errFile := filepath.Abs("../")

if errFile != nil {
t.Errorf(errFile.Error())
}

query := "select author from commits"

parser.New(query)
ast, errGit := parser.AST()
if errGit != nil {
t.Errorf(errGit.Error())
}
ast.Path = &folder
errGit = semantical.Analysis(ast)
if errGit != nil {
t.Errorf(errGit.Error())
}

typeFormat := "json"
Run(ast, &typeFormat)
}

0 comments on commit 9d47f3b

Please sign in to comment.