/
table.go
45 lines (39 loc) · 1.33 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package trv
import (
"context"
"fmt"
"strings"
"github.com/google/go-github/github"
)
type Column struct {
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 `json:"name"`
Description string `json:"comment"`
Columns []Column `json:"columns"`
}
type Repositories interface {
GetContents(ctx context.Context, owner string, repo string, path string, opt *github.RepositoryContentGetOptions) (fileContent *github.RepositoryContent, directoryContent []*github.RepositoryContent, resp *github.Response, err error)
}
// return table_name.column_name
func (t Table) getFullName(i int) string {
return t.Name + "." + t.Columns[i].Name
}
// Get the schema information output in markdown from GitHub.
func (t *Table) fetchTableInfoInMarkdownFromGitHub(repositories Repositories, ctx context.Context, owner, repo, path string) (schema, error) {
content, _, _, err := repositories.GetContents(ctx, owner, repo, path, nil)
if err != nil {
return "", fmt.Errorf("fetch table info fail:%w", err)
}
t.Name = strings.Replace(content.GetName(), ".md", "", -1)
text, err := content.GetContent()
if err != nil {
return "", fmt.Errorf("fetch table info fail:%w", err)
}
return schema(text), nil
}