Skip to content

Commit

Permalink
Merge pull request #10 from harakeishi/support_schemaJson
Browse files Browse the repository at this point in the history
schema.jsonに対応します
  • Loading branch information
harakeishi authored Feb 10, 2023
2 parents 88df30c + f5d230b commit 96d85b3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 33 deletions.
54 changes: 37 additions & 17 deletions trv/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
)

type DB struct {
tables []Table
Tables []Table `json:"tables"`
Name string `json:"name"`
Desc string `json:"desc"`
}

// If there is DB data locally, load it and return it.
Expand All @@ -23,7 +25,7 @@ func (d *DB) loadData(repo, path string) {
log.Printf("loadData fail:%s", err)
}
raw, _ := ioutil.ReadFile(fmt.Sprintf("%s/.trv/%s-%s.json", home, repo, strings.Replace(path, "/", "-", -1)))
json.Unmarshal(raw, &d.tables)
json.Unmarshal(raw, &d.Tables)
}

// Store DB data locally.
Expand All @@ -37,7 +39,7 @@ func (d *DB) saveData(repo, path string) {
log.Printf("saveData fail:%s", err)
}
}
file, err := json.MarshalIndent(d.tables, "", " ")
file, err := json.MarshalIndent(d.Tables, "", " ")
if err != nil {
log.Printf("saveData fail:%s", err)
}
Expand All @@ -47,21 +49,39 @@ func (d *DB) saveData(repo, path string) {
}

func (d *DB) fetchDBInfo(client *github.Client, ctx context.Context, source Source) error {
_, contents, _, err := client.Repositories.GetContents(ctx, source.Owner, source.Repo, source.Path, nil)
if err != nil {
return fmt.Errorf("fech DB info fail:%w", err)
}
for _, v := range contents {
path := v.GetPath()
if strings.Contains(path, ".md") {
if strings.Contains(path, "README.md") {
continue
}
var table Table
if err := table.fetchTableInfo(client, ctx, source.Owner, source.Repo, path); err != nil {
return fmt.Errorf("fech DB info fail:%w", err)
content, _, _, _ := client.Repositories.GetContents(ctx, source.Owner, source.Repo, fmt.Sprintf("%s/schema.json", source.Path), nil)
if content != nil {
text, err := content.GetContent()
if err != nil {
return fmt.Errorf("fetch table info fail:%w", err)
}
var table DB
err = json.Unmarshal([]byte(text), &table)
if err != nil {
return fmt.Errorf("fetch table info fail:%w", err)
}
d.Tables = table.Tables
} else {
if len(d.Tables) != 0 {
return nil
}
// Processing in the absence of schema.json
_, contents, _, err := client.Repositories.GetContents(ctx, source.Owner, source.Repo, source.Path, nil)
if err != nil {
return fmt.Errorf("fech DB info fail:%w", err)
}
for _, v := range contents {
path := v.GetPath()
if strings.Contains(path, ".md") {
if strings.Contains(path, "README.md") {
continue
}
var table Table
if err := table.fetchTableInfoFromMarkdown(client, ctx, source.Owner, source.Repo, path); err != nil {
return fmt.Errorf("fech DB info fail:%w", err)
}
d.Tables = append(d.Tables, table)
}
d.tables = append(d.tables, table)
}
}
return nil
Expand Down
4 changes: 0 additions & 4 deletions trv/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ func (s Source) setDbData() (DB, error) {
return DB{}, err
}

if len(db.tables) != 0 {
return db, nil
}

if err := db.fetchDBInfo(client, ctx, s); err != nil {
return DB{}, fmt.Errorf("set DB data fail:%w", err)
}
Expand Down
19 changes: 9 additions & 10 deletions trv/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,29 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/google/go-github/github"
)

type Column struct {
Name string
Type string
Defaul bool
Comment string
Name string `json:"name"`
Type string `json:"type"`
Nullable bool `json:"nullable"`
Defaul bool `json:"default"`
Comment string `json:"comment"`
}
type Table struct {
Name string
Description string
Columns []Column
UpdateDate time.Time
Name string `json:"name"`
Description string `json:"comment"`
Columns []Column `json:"columns"`
}

// return table_name.column_name
func (t Table) getFullName(i int) string {
return t.Name + "." + t.Columns[i].Name
}

func (t *Table) fetchTableInfo(client *github.Client, ctx context.Context, owner, repo, path string) error {
func (t *Table) fetchTableInfoFromMarkdown(client *github.Client, ctx context.Context, owner, repo, path string) error {
content, _, _, err := client.Repositories.GetContents(ctx, owner, repo, path, nil)
if err != nil {
return fmt.Errorf("fetch table info fail:%w", err)
Expand Down
4 changes: 2 additions & 2 deletions trv/trv.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (t *Trv) setSourceSelecter() error {
t.Pages.ShowPage("error")
return
}
t.Tables = t.DB.tables
t.Tables = t.DB.Tables
t.filterList()
t.App.SetFocus(t.Searcher)
})
Expand All @@ -125,7 +125,7 @@ func (t *Trv) addDropdownOption() error {
t.SourceSelecter.RemoveOption(currentOptionCount - 1)
t.SourceSelecter.AddOption(t.Source[lastOptionIndex], func() {
t.DB, err = t.Config.Source[lastOptionIndex].setDbData()
t.Tables = t.DB.tables
t.Tables = t.DB.Tables
t.filterList()
t.App.SetFocus(t.Searcher)
})
Expand Down

0 comments on commit 96d85b3

Please sign in to comment.