Skip to content

Commit

Permalink
use table options (#99)
Browse files Browse the repository at this point in the history
Co-authored-by: atye <aaron.tye@gmail.com>
  • Loading branch information
atye and atye committed Jul 7, 2024
1 parent 0b287e9 commit b57cc17
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 29 deletions.
10 changes: 5 additions & 5 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,35 @@ func main() {
// You can also create a client with a cache (capacity, page expiration, interval to check expiration of each page)
// tg = client.NewTableGetter("github.com/atye/wikitable2json/examples", client.WithCache(5, 5*time.Second, 5*time.Second))

matrix, err := tg.GetMatrix(context.Background(), "Arhaan_Khan", "en", false)
matrix, err := tg.GetMatrix(context.Background(), "Arhaan_Khan", "en")
if err != nil {
log.Fatal(err)
}

fmt.Println(matrix)

matrix, err = tg.GetMatrix(context.Background(), "Arhaan_Khan", "en", false, 0)
matrix, err = tg.GetMatrix(context.Background(), "Arhaan_Khan", "en", client.WithTables(0))
if err != nil {
log.Fatal(err)
}

fmt.Println(matrix)

matrixVerbose, err := tg.GetMatrixVerbose(context.Background(), "Arhaan_Khan", "en", false)
matrixVerbose, err := tg.GetMatrixVerbose(context.Background(), "Arhaan_Khan", "en")
if err != nil {
log.Fatal(err)
}

fmt.Println(matrixVerbose)

keyValue, err := tg.GetKeyValue(context.Background(), "Arhaan_Khan", "en", false, 1, 1)
keyValue, err := tg.GetKeyValue(context.Background(), "Arhaan_Khan", "en", 1, client.WithTables(1))
if err != nil {
log.Fatal(err)
}

fmt.Println(keyValue)

keyValueVerbose, err := tg.GetKeyValueVerbose(context.Background(), "Arhaan_Khan", "en", false, 1, 1)
keyValueVerbose, err := tg.GetKeyValueVerbose(context.Background(), "Arhaan_Khan", "en", 1, client.WithTables(1))
if err != nil {
log.Fatal(err)
}
Expand Down
7 changes: 7 additions & 0 deletions internal/entrypoint/entrypoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func TestAPI(t *testing.T) {
switch r.URL.Path {
case "/api/rest_v1/page/html/golden":
w.Write(getPageBytes(t, "golden"))
case "/api/rest_v1/page/html/goldenDouble":
w.Write(getPageBytes(t, "goldenDouble"))
case "/api/rest_v1/page/html/issueOne":
w.Write(getPageBytes(t, "issueOne"))
case "/api/rest_v1/page/html/dataSortValue":
Expand Down Expand Up @@ -103,6 +105,11 @@ func TestAPI(t *testing.T) {
fmt.Sprintf("http://localhost:%s/api/golden", PORT),
GoldenMatrix,
},
{
"GoldenSecondTable",
fmt.Sprintf("http://localhost:%s/api/goldenDouble?table=1", PORT),
GoldenMatrixDouble,
},
{
"GoldenWithParameters",
fmt.Sprintf("http://localhost:%s/api/golden?lang=sp&format=matrix&table=0", PORT),
Expand Down
83 changes: 83 additions & 0 deletions internal/entrypoint/testdata/goldenDouble.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<!DOCTYPE html>
<html>

<body>
<table class="wikitable">
<caption id="mwAek">test</caption>
<tbody>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3
</th>
</tr>
<tr>
<td rowspan="2">A
</td>
<td colspan="2">B
</td>
</tr>
<tr>
<td>C
</td>
<td>D
</td>
</tr>
<tr class="mw-empty-elt"></tr>
<tr>
<td>E
</td>
<td rowspan="2" colspan="2">F
</td>
</tr>
<tr>
<td>G
</td>
</tr>
<tr>
<td colspan="3">H
</td>
</tr>
</tbody>
</table>
<table class="wikitable">
<caption id="mwAek">test</caption>
<tbody>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3
</th>
</tr>
<tr>
<td rowspan="2">A
</td>
<td colspan="2">B
</td>
</tr>
<tr>
<td>C
</td>
<td>D
</td>
</tr>
<tr class="mw-empty-elt"></tr>
<tr>
<td>E
</td>
<td rowspan="2" colspan="2">F
</td>
</tr>
<tr>
<td>G
</td>
</tr>
<tr>
<td colspan="3">H
</td>
</tr>
</tbody>
</table>
</body>

</html>
11 changes: 11 additions & 0 deletions internal/entrypoint/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ var (
},
}

GoldenMatrixDouble = [][][]string{
{
{"Column 1", "Column 2", "Column 3"},
{"A", "B", "B"},
{"A", "C", "D"},
{"E", "F", "F"},
{"G", "F", "F"},
{"H", "H", "H"},
},
}

GoldenKeyValue = [][]map[string]string{
{
{
Expand Down
15 changes: 11 additions & 4 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,39 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

opts := []client.TableOption{
client.WithTables(qv.tables...),
}
if qv.cleanRef {
opts = append(opts, client.WithCleanReferences())
}

s.client.SetUserAgent(r.Header.Get("User-Agent"))

var resp interface{}
if qv.keyRows >= 1 {
if qv.verbose {
resp, err = s.client.GetKeyValueVerbose(ctx, page, qv.lang, qv.cleanRef, qv.keyRows, qv.tables...)
resp, err = s.client.GetKeyValueVerbose(ctx, page, qv.lang, qv.keyRows, opts...)
if err != nil {
writeError(w, err)
return
}
} else {
resp, err = s.client.GetKeyValue(ctx, page, qv.lang, qv.cleanRef, qv.keyRows, qv.tables...)
resp, err = s.client.GetKeyValue(ctx, page, qv.lang, qv.keyRows, opts...)
if err != nil {
writeError(w, err)
return
}
}
} else {
if qv.verbose {
resp, err = s.client.GetMatrixVerbose(ctx, page, qv.lang, qv.cleanRef, qv.tables...)
resp, err = s.client.GetMatrixVerbose(ctx, page, qv.lang, opts...)
if err != nil {
writeError(w, err)
return
}
} else {
resp, err = s.client.GetMatrix(ctx, page, qv.lang, qv.cleanRef, qv.tables...)
resp, err = s.client.GetMatrix(ctx, page, qv.lang, opts...)
if err != nil {
writeError(w, err)
return
Expand Down
79 changes: 59 additions & 20 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,46 @@ var (
)

type TableGetter interface {
GetMatrix(ctx context.Context, page string, lang string, cleanRef bool, tables ...int) ([][][]string, error)
GetMatrixVerbose(ctx context.Context, page string, lang string, cleanRef bool, tables ...int) ([][][]Verbose, error)
GetKeyValue(ctx context.Context, page string, lang string, cleanRef bool, keyRows int, tables ...int) ([][]map[string]string, error)
GetKeyValueVerbose(ctx context.Context, page string, lang string, cleanRef bool, keyRows int, tables ...int) ([][]map[string]Verbose, error)
GetMatrix(ctx context.Context, page string, lang string, options ...TableOption) ([][][]string, error)
GetMatrixVerbose(ctx context.Context, page string, lang string, options ...TableOption) ([][][]Verbose, error)
GetKeyValue(ctx context.Context, page string, lang string, keyRows int, options ...TableOption) ([][]map[string]string, error)
GetKeyValueVerbose(ctx context.Context, page string, lang string, keyRows int, options ...TableOption) ([][]map[string]Verbose, error)
SetUserAgent(string)
}

type Option func(*client)
type ClientOption func(*client)

func WithCache(capacity int, itemExpiration time.Duration, purgeEvery time.Duration) Option {
func WithCache(capacity int, itemExpiration time.Duration, purgeEvery time.Duration) ClientOption {
return func(c *client) {
c.cache = cache.NewCache(capacity, itemExpiration, purgeEvery)
}
}

func WithHTTPClient(c *http.Client) Option {
func WithHTTPClient(c *http.Client) ClientOption {
return func(tg *client) {
tg.wikiAPI = api.NewWikiClient(api.BaseURL, api.WithHTTPClient(c))
}
}

type TableOption func(*tableOptions)

func WithCleanReferences() TableOption {
return func(to *tableOptions) {
to.cleanRef = true
}
}

func WithTables(tables ...int) TableOption {
return func(to *tableOptions) {
to.tables = tables
}
}

type tableOptions struct {
cleanRef bool
tables []int
}

type wikiAPI interface {
GetPageBytes(ctx context.Context, page, lang, userAgent string) ([]byte, error)
}
Expand All @@ -60,7 +79,7 @@ type client struct {
cache *cache.Cache
}

func NewTableGetter(userAgent string, options ...Option) TableGetter {
func NewTableGetter(userAgent string, options ...ClientOption) TableGetter {
c := &client{
wikiAPI: api.NewWikiClient(api.BaseURL),
userAgent: userAgent,
Expand All @@ -72,17 +91,22 @@ func NewTableGetter(userAgent string, options ...Option) TableGetter {
return c
}

func (c *client) GetMatrix(ctx context.Context, page string, lang string, cleanRef bool, tables ...int) ([][][]string, error) {
func (c *client) GetMatrix(ctx context.Context, page string, lang string, options ...TableOption) ([][][]string, error) {
tableSelection, err := c.getTableSelection(ctx, page, lang)
if err != nil {
return nil, handleErr(err)
}

if cleanRef {
to := new(tableOptions)
for _, o := range options {
o(to)
}

if to.cleanRef {
cleanReferences(tableSelection)
}

matrix, err := parse(tableSelection, 0, false, tables...)
matrix, err := parse(tableSelection, 0, false, to.tables...)
if err != nil {
return nil, handleErr(err)
}
Expand All @@ -99,17 +123,22 @@ func (c *client) GetMatrix(ctx context.Context, page string, lang string, cleanR
return ret, nil
}

func (c *client) GetMatrixVerbose(ctx context.Context, page string, lang string, cleanRef bool, tables ...int) ([][][]Verbose, error) {
func (c *client) GetMatrixVerbose(ctx context.Context, page string, lang string, options ...TableOption) ([][][]Verbose, error) {
tableSelection, err := c.getTableSelection(ctx, page, lang)
if err != nil {
return nil, handleErr(err)
}

if cleanRef {
to := new(tableOptions)
for _, o := range options {
o(to)
}

if to.cleanRef {
cleanReferences(tableSelection)
}

matrix, err := parse(tableSelection, 0, true, tables...)
matrix, err := parse(tableSelection, 0, true, to.tables...)
if err != nil {
return nil, handleErr(err)
}
Expand All @@ -126,7 +155,7 @@ func (c *client) GetMatrixVerbose(ctx context.Context, page string, lang string,
return ret, nil
}

func (c *client) GetKeyValue(ctx context.Context, page string, lang string, cleanRef bool, keyRows int, tables ...int) ([][]map[string]string, error) {
func (c *client) GetKeyValue(ctx context.Context, page string, lang string, keyRows int, options ...TableOption) ([][]map[string]string, error) {
if keyRows < 1 {
return nil, status.NewStatus("keyRows must be at least 1", http.StatusBadRequest)
}
Expand All @@ -136,11 +165,16 @@ func (c *client) GetKeyValue(ctx context.Context, page string, lang string, clea
return nil, handleErr(err)
}

if cleanRef {
to := new(tableOptions)
for _, o := range options {
o(to)
}

if to.cleanRef {
cleanReferences(tableSelection)
}

keyValue, err := parse(tableSelection, keyRows, false, tables...)
keyValue, err := parse(tableSelection, keyRows, false, to.tables...)
if err != nil {
return nil, handleErr(err)
}
Expand All @@ -157,7 +191,7 @@ func (c *client) GetKeyValue(ctx context.Context, page string, lang string, clea
return ret, nil
}

func (c *client) GetKeyValueVerbose(ctx context.Context, page string, lang string, cleanRef bool, keyRows int, tables ...int) ([][]map[string]Verbose, error) {
func (c *client) GetKeyValueVerbose(ctx context.Context, page string, lang string, keyRows int, options ...TableOption) ([][]map[string]Verbose, error) {
if keyRows < 1 {
return nil, status.NewStatus("keyRows must be at least 1", http.StatusBadRequest)
}
Expand All @@ -167,11 +201,16 @@ func (c *client) GetKeyValueVerbose(ctx context.Context, page string, lang strin
return nil, handleErr(err)
}

if cleanRef {
to := new(tableOptions)
for _, o := range options {
o(to)
}

if to.cleanRef {
cleanReferences(tableSelection)
}

keyValue, err := parse(tableSelection, keyRows, true, tables...)
keyValue, err := parse(tableSelection, keyRows, true, to.tables...)
if err != nil {
return nil, handleErr(err)
}
Expand Down

0 comments on commit b57cc17

Please sign in to comment.