Skip to content

Commit

Permalink
Merge pull request #52 from xushiwei/q
Browse files Browse the repository at this point in the history
fetcher: return ErrUnknownPageType
  • Loading branch information
xushiwei committed May 18, 2024
2 parents e3d3902 + dffe215 commit 5b4333c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
10 changes: 7 additions & 3 deletions chore/pysigfetch/pysigfetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ func main() {
b, _ := io.ReadAll(os.Stdin)
names = strings.Split(strings.TrimSpace(string(b)), " ")
}
docs := make([]any, len(names))
for i, name := range names {
var docs = make([]any, 0, len(names))
for _, name := range names {
log.Println("==> Fetch", name)
docs[i] = fetcher.FromInput(moduleName, name)
doc, err := fetcher.FromInput(moduleName, name)
if err == fetcher.ErrUnknownPageType {
break
}
docs = append(docs, doc)
}
json.NewEncoder(os.Stdout).Encode(module{moduleName, docs})
}
17 changes: 11 additions & 6 deletions fetcher/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package fetcher

import (
"errors"
"reflect"

"github.com/goplus/hdq"
Expand All @@ -35,23 +36,27 @@ func Convert(conv reflect.Value, input, source any) any {

// -----------------------------------------------------------------------------

var (
ErrUnknownPageType = errors.New("unknown page type")
)

// New creates a new object from a html source by a registered converter.
func New(pageType string, input, source any) any {
func New(pageType string, input, source any) (any, error) {
page, ok := convs[pageType]
if !ok {
panic("fetcher: unknown pageType - " + pageType)
return nil, ErrUnknownPageType
}
return Convert(page.Conv, input, source)
return Convert(page.Conv, input, source), nil
}

// FromInput creates a new object from the html source with the specified input.
func FromInput(pageType string, input any) any {
func FromInput(pageType string, input any) (any, error) {
page, ok := convs[pageType]
if !ok {
panic("fetcher: unknown pageType - " + pageType)
return nil, ErrUnknownPageType
}
url := page.URL(input)
return Convert(page.Conv, input, url)
return Convert(page.Conv, input, url), nil
}

// sitePageType represents a site page type.
Expand Down

0 comments on commit 5b4333c

Please sign in to comment.