parser composer checks for either-type

This commit is contained in:
Matthias Bisping 2022-06-03 16:34:37 +02:00
parent 730bdfb220
commit f718b2f7ef
2 changed files with 13 additions and 1 deletions

View File

@ -46,7 +46,11 @@ class EitherParserComposer:
self.parser = rcompose(*map(EitherParserWrapper, parsers))
def parse(self, item):
return self.parser(item).item
result = self.parser(item)
if isinstance(result, Right):
return result.bind()
else:
raise ParsingError("All parsers failed.")
def __call__(self, item):
return self.parse(item)

View File

@ -1,5 +1,8 @@
import json
import pytest
from pyinfra.parser.blob_parser import ParsingError
from pyinfra.parser.parser_composer import EitherParserComposer
from pyinfra.parser.parsers.identity import IdentityBlobParser
from pyinfra.parser.parsers.json import JsonBlobParser
@ -32,3 +35,8 @@ def test_either_parser_composer():
a = 1
assert parser(a) == a
parser = EitherParserComposer(JsonBlobParser(), StringBlobParser())
with pytest.raises(ParsingError):
parser(1)