From b57cc1799d1a8e801361c30dc6a07d3503ad941e Mon Sep 17 00:00:00 2001 From: Aaron Tye Date: Sun, 7 Jul 2024 11:56:55 -0400 Subject: [PATCH] use table options (#99) Co-authored-by: atye --- examples/main.go | 10 +-- internal/entrypoint/entrypoint_test.go | 7 ++ .../entrypoint/testdata/goldenDouble.html | 83 +++++++++++++++++++ internal/entrypoint/types_test.go | 11 +++ internal/server/server.go | 15 +++- pkg/client/client.go | 79 +++++++++++++----- 6 files changed, 176 insertions(+), 29 deletions(-) create mode 100644 internal/entrypoint/testdata/goldenDouble.html diff --git a/examples/main.go b/examples/main.go index f7e699a..c861600 100644 --- a/examples/main.go +++ b/examples/main.go @@ -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) } diff --git a/internal/entrypoint/entrypoint_test.go b/internal/entrypoint/entrypoint_test.go index 08961b5..f62c1be 100644 --- a/internal/entrypoint/entrypoint_test.go +++ b/internal/entrypoint/entrypoint_test.go @@ -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": @@ -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), diff --git a/internal/entrypoint/testdata/goldenDouble.html b/internal/entrypoint/testdata/goldenDouble.html new file mode 100644 index 0000000..43fa835 --- /dev/null +++ b/internal/entrypoint/testdata/goldenDouble.html @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
test
Column 1Column 2Column 3 +
A + B +
C + D +
E + F +
G +
H +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
test
Column 1Column 2Column 3 +
A + B +
C + D +
E + F +
G +
H +
+ + + \ No newline at end of file diff --git a/internal/entrypoint/types_test.go b/internal/entrypoint/types_test.go index 59c9e71..9b37412 100644 --- a/internal/entrypoint/types_test.go +++ b/internal/entrypoint/types_test.go @@ -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{ { { diff --git a/internal/server/server.go b/internal/server/server.go index 7d78070..2f49477 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -36,18 +36,25 @@ 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 @@ -55,13 +62,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } 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 diff --git a/pkg/client/client.go b/pkg/client/client.go index 654b3f7..ae550bf 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -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) } @@ -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, @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) }