Skip to content

Commit

Permalink
Added NewSliceFromPage factory (#7)
Browse files Browse the repository at this point in the history
This PR adds convenience function to get a slice of annotated entities from `htmltable.Page` instance.

Closes #6
  • Loading branch information
nfx committed Dec 27, 2022
1 parent 4470783 commit 5f367df
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
12 changes: 6 additions & 6 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ func ExampleNewSliceFromURL_rowspansAndColspans() {
AMDStoreMI bool `header:"Storage features AMD StoreMI"`
Overclocking string `header:"Processoroverclocking"`
TDP string `header:"TDP"`
SupportExcavator string `header:"CPU support[14] Excavator"`
SupportZen string `header:"CPU support[14] Zen"`
SupportZenPlus string `header:"CPU support[14] Zen+"`
SupportZen2 string `header:"CPU support[14] Zen 2"`
SupportZen3 string `header:"CPU support[14] Zen 3"`
SupportExcavator string `header:"CPU support Excavator"`
SupportZen string `header:"CPU support Zen"`
SupportZenPlus string `header:"CPU support Zen+"`
SupportZen2 string `header:"CPU support Zen 2"`
SupportZen3 string `header:"CPU support Zen 3"`
Architecture string `header:"Architecture"`
}
am4Chipsets, _ := htmltable.NewSliceFromURL[AM4]("https://en.wikipedia.org/wiki/List_of_AMD_chipsets")
Expand Down Expand Up @@ -97,5 +97,5 @@ func ExampleLogger() {

// Output:
// [INFO] found table [columns [Symbol Security SEC filings GICSSector GICS Sub-Industry Headquarters Location Date first added CIK Founded] count 503]
// [INFO] found table [columns [Date Added Ticker Added Security Removed Ticker Removed Security Reason] count 312]
// [INFO] found table [columns [Date Added Ticker Added Security Removed Ticker Removed Security Reason] count 316]
}
7 changes: 7 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ func NewSlice[T any](ctx context.Context, r io.Reader) ([]T, error) {
return f.slice()
}

// NewSliceFromPage finds a table matching the slice and returns the slice
func NewSliceFromPage[T any](p *Page) ([]T, error) {
return (&feeder[T]{
Page: *p,
}).slice()
}

// NewSliceFromString is same as NewSlice(context.Context, io.Reader),
// but takes just a string.
func NewSliceFromString[T any](in string) ([]T, error) {
Expand Down
9 changes: 9 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ func TestNewSliceFromUrl(t *testing.T) {
assertGreaterOrEqual(t, len(out), 500)
}

func TestNewSliceFromPage(t *testing.T) {
url := "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
p, err := NewFromURL(url)
assertNoError(t, err)
out, err := NewSliceFromPage[Ticker](p)
assertNoError(t, err)
assertGreaterOrEqual(t, len(out), 500)
}

func TestNewSliceFromUrl_Fails(t *testing.T) {
_, err := NewSliceFromURL[Ticker]("https://127.0.0.1")
assertEqualError(t, err, "Get \"https://127.0.0.1\": dial tcp 127.0.0.1:443: connect: connection refused")
Expand Down

0 comments on commit 5f367df

Please sign in to comment.