diff options
| author | JP Appel <jeanpierre.appel01@gmail.com> | 2025-06-13 12:55:34 -0400 |
|---|---|---|
| committer | JP Appel <jeanpierre.appel01@gmail.com> | 2025-06-13 12:55:34 -0400 |
| commit | e8de91eee099486200bac79406f17c149d1fcb5d (patch) | |
| tree | 4b163927f89ab6de662f4f7a50236aaa01e24978 /pkg/index | |
| parent | f85cfb2055551926ed4aeaa0550330d3e4da7569 (diff) | |
Add more flags to control document indexing
Added support for the following
* Ignore unpassable dates
* Ignore parsing non-standard header keys
* Ignore errors while parsing non-standard header keys
Diffstat (limited to 'pkg/index')
| -rw-r--r-- | pkg/index/index.go | 37 | ||||
| -rw-r--r-- | pkg/index/index_test.go | 3 |
2 files changed, 25 insertions, 15 deletions
diff --git a/pkg/index/index.go b/pkg/index/index.go index a96c227..419607c 100644 --- a/pkg/index/index.go +++ b/pkg/index/index.go @@ -27,6 +27,13 @@ type Document struct { Tags []string `yaml:"tags,omitempty" json:"tags"` Links []string `yaml:"-" json:"links"` OtherMeta string `yaml:"-" json:"meta"` + parseOpts ParseOpts +} + +type ParseOpts struct { + ParseMeta bool + IgnoreDateError bool + IgnoreMetaError bool } type infoPath struct { @@ -87,17 +94,21 @@ func (doc *Document) UnmarshalYAML(node ast.Node) error { } if keyPath == "$.date" { - if err := doc.parseDateNode(v); err != nil { + if err := doc.parseDateNode(v); err != nil && !doc.parseOpts.IgnoreDateError { return err } } else if keyPath == "$.author" { if err := doc.parseAuthor(v); err != nil { return err } - } else { + } else if doc.parseOpts.ParseMeta { field, err := kv.MarshalYAML() if err != nil { - return err + if doc.parseOpts.IgnoreMetaError { + continue + } else { + return err + } } buf.Write(field) buf.WriteByte('\n') @@ -314,9 +325,8 @@ func (idx Index) Filter(paths []string, numWorkers uint) []string { return fPaths } -func ParseDoc(path string) (*Document, error) { - doc := &Document{} - doc.Path = path +func ParseDoc(path string, opts ParseOpts) (*Document, error) { + doc := &Document{Path: path, parseOpts: opts} f, err := os.Open(path) if err != nil { @@ -336,7 +346,6 @@ func ParseDoc(path string) (*Document, error) { return nil, fmt.Errorf("Can't find YAML header in %s", path) } - // FIXME: decoder reads past yaml header into document if err := yaml.NewDecoder(io.LimitReader(f, pos)).Decode(doc); err != nil { return nil, errors.Join(ErrHeaderParse, err) } @@ -345,17 +354,17 @@ func ParseDoc(path string) (*Document, error) { return doc, nil } -func ParseDocs(paths []string, numWorkers uint) map[string]*Document { +func ParseDocs(paths []string, numWorkers uint, opts ParseOpts) map[string]*Document { jobs := make(chan string, numWorkers) - results := make(chan Document, numWorkers) + results := make(chan *Document, numWorkers) docs := make(map[string]*Document, len(paths)) wg := &sync.WaitGroup{} wg.Add(int(numWorkers)) for range numWorkers { - go func(jobs <-chan string, results chan<- Document, wg *sync.WaitGroup) { + go func(jobs <-chan string, results chan<- *Document, wg *sync.WaitGroup) { for path := range jobs { - doc, err := ParseDoc(path) + doc, err := ParseDoc(path, opts) if err != nil { // TODO: propagate error slog.Error("Error occured while parsing file", @@ -364,7 +373,7 @@ func ParseDocs(paths []string, numWorkers uint) map[string]*Document { continue } - results <- *doc + results <- doc } wg.Done() }(jobs, results, wg) @@ -377,13 +386,13 @@ func ParseDocs(paths []string, numWorkers uint) map[string]*Document { close(jobs) }(jobs, paths) - go func(results chan Document, wg *sync.WaitGroup) { + go func(results chan *Document, wg *sync.WaitGroup) { wg.Wait() close(results) }(results, wg) for doc := range results { - docs[doc.Path] = &doc + docs[doc.Path] = doc } return docs diff --git a/pkg/index/index_test.go b/pkg/index/index_test.go index 4e55325..3f9b900 100644 --- a/pkg/index/index_test.go +++ b/pkg/index/index_test.go @@ -262,7 +262,8 @@ func TestIndex_ParseOne(t *testing.T) { path := tt.pathMaker(t) tt.want.Path = path - got, gotErr := ParseDoc(path) + // TODO: add ParseOpts as test param + got, gotErr := ParseDoc(path, ParseOpts{ParseMeta: true}) if !errors.Is(gotErr, tt.wantErr) { t.Errorf("Recieved unexpected error: want %v got %v", tt.wantErr, gotErr) |
