From 0a79682ee5a68f30ea691551dfd53a6face0be36 Mon Sep 17 00:00:00 2001 From: Alex Palaistras Date: Mon, 30 May 2016 22:50:25 +0100 Subject: [PATCH] Rename interface types and methods for clarity --- farsight.go | 6 +++--- parser/html/html.go | 4 ++-- parser/parser.go | 4 +++- source/source.go | 9 +++++---- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/farsight.go b/farsight.go index 6e28205..d90ea9d 100644 --- a/farsight.go +++ b/farsight.go @@ -125,10 +125,10 @@ func setField(doc parser.Document, field reflect.Value) error { } case reflect.Slice: // Decompose document into list and prepare destination slice. - list := doc.List() - slice := reflect.MakeSlice(field.Type(), len(list), cap(list)) + docs := doc.Slice() + slice := reflect.MakeSlice(field.Type(), len(docs), cap(docs)) - for i, d := range list { + for i, d := range docs { if err := setField(d, slice.Index(i)); err != nil { return err } diff --git a/parser/html/html.go b/parser/html/html.go index e2dcc02..183507c 100644 --- a/parser/html/html.go +++ b/parser/html/html.go @@ -89,9 +89,9 @@ func (h *HTMLDocument) Filter(sel string) (parser.Document, error) { return sub, nil } -// List decomposes the target HTMLDocument into a slice of HTMLDocument types, +// Slice decomposes the target HTMLDocument into a slice of HTMLDocument types, // each containing a single node from the parent's list of nodes. -func (h *HTMLDocument) List() []parser.Document { +func (h *HTMLDocument) Slice() []parser.Document { var docs []parser.Document for _, n := range h.nodes { diff --git a/parser/parser.go b/parser/parser.go index 1a17534..fd708ee 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -10,13 +10,15 @@ import ( "io" ) +// Parser is an interface that wraps the Parse method. type Parser interface { Parse(io.Reader) (Document, error) } +// Document is an interface that represents a generic, introspect-able document. type Document interface { Filter(attr string) (Document, error) - List() []Document + Slice() []Document String() string } diff --git a/source/source.go b/source/source.go index e34af98..2781433 100644 --- a/source/source.go +++ b/source/source.go @@ -11,15 +11,16 @@ import ( "strings" ) -type Source interface { +// Fetcher is an interface that wraps the Fetch method. +type Fetcher interface { Fetch(src string) (io.Reader, error) } // A map of all registered sources. -var sources map[string]Source +var sources map[string]Fetcher // Register a source under a unique name. -func Register(name string, rcvr Source) error { +func Register(name string, rcvr Fetcher) error { if _, exists := sources[name]; exists { return fmt.Errorf("Source '%s' already registered, refusing to overwrite", name) } @@ -43,5 +44,5 @@ func Fetch(src string) (io.Reader, error) { } func init() { - sources = make(map[string]Source) + sources = make(map[string]Fetcher) }