Skip to content

Commit

Permalink
remove parallel processing
Browse files Browse the repository at this point in the history
  • Loading branch information
conneroisu committed Aug 8, 2024
1 parent cdac4ce commit e94a2a8
Showing 1 changed file with 0 additions and 87 deletions.
87 changes: 0 additions & 87 deletions slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,90 +616,3 @@ 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,
)
}
errCh <- nil
}
}(i)
}
for i := 0; i < dType.NumField(); i++ {
<-errCh
}
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)
}

0 comments on commit e94a2a8

Please sign in to comment.