Skip to content

Commit

Permalink
Merge pull request #84 from conneroisu/devit
Browse files Browse the repository at this point in the history
devit
  • Loading branch information
conneroisu authored Aug 5, 2024
2 parents 943cc17 + 386a886 commit 86110a4
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
83 changes: 83 additions & 0 deletions slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,86 @@ func NewFromBytesChFn[
}
return NewChFn(doc, ch, fn)
}

// NewPl parses a reader into a slice of structs with parallel processing.
func NewPl[T any](doc *goquery.Document) ([]T, error) {
dType := reflect.TypeOf((*T)(nil)).Elem()
if dType.Kind() != reflect.Struct && dType.Kind() != reflect.Ptr {
return nil, fmt.Errorf("expected struct, got %s", dType.Kind())
}
results := make([]T, 0)
var cfg *SelectorConfig
errCh := make(chan error)
for i := 0; i < dType.NumField(); i++ {
go func(i int) {
cfg = NewSelectorConfig(dType.Field(i).Tag)
if cfg.DataSelector == "" {
errCh <- ErrSelectorNotFound{
Typ: dType,
Field: dType.Field(i),
Cfg: cfg,
}
}
dataRows := doc.Find(cfg.DataSelector)
if dataRows.Length() <= 0 {
return
}
if cfg.HeadSelector != "" && cfg.HeadSelector != "-" {
_ = dataRows.RemoveFiltered(cfg.HeadSelector)
}
if len(results) < dataRows.Length() {
results = make([]T, dataRows.Length())
}
for j := 0; j < dataRows.Length(); j++ {
err := SetStructField(
&results[j],
dType.Field(i), // name of the field to set
dataRows.Eq(j), // goquery selection for cell
&selector{
control: cfg.ControlTag,
query: cfg.QuerySelector,
}, // selector for the inner cell
)
if err != nil {
errCh <- fmt.Errorf(
"failed to set field %s: %s",
dType.Field(i).Name,
err,
)
}
}
}(i)
}
if len(results) < 1 {
return nil, fmt.Errorf("no data found")
}
return results, nil
}

// NewFromReaderPl parses a reader into a slice of structs with parallel processing.
func NewFromReaderPl[T any](r io.Reader) ([]T, error) {
doc, err := goquery.NewDocumentFromReader(r)
if err != nil {
return nil, fmt.Errorf("failed to parse html: %w", err)
}
return NewPl[T](doc)
}

// NewFromStringPl parses a string into a slice of structs with parallel processing.
func NewFromStringPl[T any](htmlInput string) ([]T, error) {
reader := strings.NewReader(htmlInput)
doc, err := goquery.NewDocumentFromReader(reader)
if err != nil {
return nil, fmt.Errorf("failed to parse html: %w", err)
}
return NewPl[T](doc)
}

// NewFromBytesPl parses a byte slice into a slice of structs with parallel processing.
func NewFromBytesPl[T any](b []byte) ([]T, error) {
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(b))
if err != nil {
return nil, fmt.Errorf("failed to parse html: %w", err)
}
return NewPl[T](doc)
}
1 change: 0 additions & 1 deletion tools/seltabls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,3 @@ Manually, one can run the tests with:
```sh
go test ./...
```

0 comments on commit 86110a4

Please sign in to comment.