diff --git a/conservancy_beancount/plugin/meta_rt_links.py b/conservancy_beancount/plugin/meta_rt_links.py index 591e7c0342196d21c0d1c1fd68a8ac54f3b4e5a2..b91f4ec45a4a70d18fa149c08fc4a9044dd9cc57 100644 --- a/conservancy_beancount/plugin/meta_rt_links.py +++ b/conservancy_beancount/plugin/meta_rt_links.py @@ -21,11 +21,13 @@ from .. import errors as errormod from ..beancount_types import ( MetaKey, MetaValue, + Posting, Transaction, ) from typing import ( - Mapping, + MutableMapping, + Optional, ) class MetaRTLinks(core.TransactionHook): @@ -39,19 +41,26 @@ class MetaRTLinks(core.TransactionHook): self.rt = rt_wrapper def _check_links(self, + meta: MutableMapping[MetaKey, MetaValue], txn: Transaction, - meta: Mapping[MetaKey, MetaValue], + post: Optional[Posting]=None, ) -> errormod.Iter: - for key in self.LINK_METADATA.intersection(meta): - for link in str(meta[key]).split(): - if not link.startswith('rt:'): - continue - parsed = self.rt.parse(link) - if parsed is None or not self.rt.exists(*parsed): - yield errormod.BrokenRTLinkError(txn, key, link, parsed) + metadata = data.Metadata(meta) + for key in self.LINK_METADATA: + try: + links = metadata.get_links(key) + except TypeError: + yield errormod.InvalidMetadataError(txn, key, meta[key], post) + else: + for link in links: + if not link.startswith('rt:'): + continue + parsed = self.rt.parse(link) + if parsed is None or not self.rt.exists(*parsed): + yield errormod.BrokenRTLinkError(txn, key, link, parsed) def run(self, txn: Transaction) -> errormod.Iter: - yield from self._check_links(txn, txn.meta) + yield from self._check_links(txn.meta, txn) for post in txn.postings: if post.meta is not None: - yield from self._check_links(txn, post.meta) + yield from self._check_links(post.meta, txn, post)