Beispiel #1
0
def parse_optional(p: Parser[A]) -> Parser[Sum2[None, A]]:
    return Parser(
        p.path(), Fn[JsonType, PreParsed[Sum2[None, A]]]
        (lambda j: fold2[PreParseError, A, PreParsed[Sum2[None, A]]]
         ((lambda x: fold2[PreParseError, None, PreParsed[Sum2[None, A]]]
           ((lambda _: F1(x), lambda x: F2(F1(x))))(parse_none(p.path(
           )).parse_fn()(j)), lambda x: F2(F2(x))))(p.parse_fn()(j))))
Beispiel #2
0
 def __call__(self, a: PreParsed[A]) -> Parsed[A]:
     return fold2[PreParseError, A, Parsed[A]](
         (fold2[List[ParseError], ParseException,
                Parsed[A]]((lambda x: F1(
                    list(
                        map(
                            Fn[ParseError, ParseError]
                            (lambda y: y._replace(path=self.sg.append(
                                self.path, y.path))), x))),
                            lambda x: F1([ParseError(self.path, x)]))),
          lambda x: F2(x)))(a)
Beispiel #3
0
def safe_parse(y: JsonDict, k: str, t: Type[A]) -> Sum2[Exception, A]:
    try:
        x = y[k]
        if isinstance(x, t):
            return F2(x)
        else:
            return F1(
                TypeError('Could not parse value of type ' + t.__name__ +
                          ' from key ' + k))
    except Exception as e:
        return F1(e)
Beispiel #4
0
 def map_deserialize(self, j: JsonType) -> Parsed[Exception, Foo]:
     if not isinstance(j, JsonDict):
         return F1(TypeError('Expecting JSON object'))
     c = cast(JsonDict, j)
     return map2(
         append2(safe_parse(c, 'baz', int), safe_parse(c, 'bar', str)),
         lambda t: Foo(t[1], t[0]))
Beispiel #5
0
 def x(j: JsonType) -> Parsed[JsonType]:
     if len(k) == 0:
         return F2(j)
     else:
         d = parse_dict()(j)
         try:
             aj = map2(d, lambda y: y.get(k[0]))  # type: ignore
             ntk = k if (len(tk) == 0) else tk
             pj = fold2[List[ParseError], JsonType, Parsed[JsonType]](
                 (lambda x: F1(
                     list(
                         map(
                             Fn[ParseError, ParseError]
                             (lambda y: y._replace(error=TraverseError(
                                 k[0], y.error))), x))),
                  lambda x: F2(x)))(ToParsed[JsonType](ntk)(bind2(
                      aj, parse_json)))  # type: ignore
             return bind2(pj, traverse(k[1:], ntk))
         except Exception as e:
             return F1([ParseError(tk, UnknownParseError(e))])
Beispiel #6
0
def parse_json(j: str) -> Parsed[Exception, JsonType]:
    try:
        x = json.loads(j)  # type: ignore
        if isinstance(x, dict):  # type: ignore
            return F2(JsonDict(x))
        elif isinstance(x, list):  # type: ignore
            return F2(JsonList(x))
        else:
            return F2(cast(JsonPrimitive, x))
    except Exception as e:
        return F1(e)
Beispiel #7
0
def parse_json(x: any) -> PreParsed[JsonType]:  # type: ignore
    try:
        if isinstance(x, list):  # type: ignore
            return F2(F2(JsonList(x)))
        elif isinstance(x, dict):  # type: ignore
            return F2(F3(JsonDict(x)))
        else:
            if isinstance(x, str):  # type: ignore
                return F2(F1(F1(x)))
            elif isinstance(x, int):  # type: ignore
                return F2(F1(F2(x)))
            elif isinstance(x, bool):  # type: ignore
                return F2(F1(F3(x)))
            elif isinstance(x, type(None)):  # type: ignore
                return F2(F1(F4(x)))
            else:
                return F1(F2(ParseException))
    except Exception as e:
        return F1(F2(UnknownParseError(e)))
Beispiel #8
0
 def __init__(self, fn: SerializeFn[A]) -> None:
     self._fn = Fn[List[A], JsonType](lambda x: F1(F4(None)))
     self._list_fn = fn
     self._path: List[str] = []
Beispiel #9
0
 def __call__(self, j: JsonType) -> PreParsed[B]:
     return bind2(
         self._p(j),
         Compose(
             fold2[CustomParseError, B, PreParsed[B]](
                 (lambda x: F1(F2(x)), lambda x: F2(x))), self._c))
Beispiel #10
0
 def deserialize(self, b: str) -> Parsed[Exception, JsonType]:
     try:
         return parse_json(b)
     except Exception as e:
         return F1(e)
Beispiel #11
0
def serialize_bool_fn(b: bool) -> JsonType:
    return F1(F3(b))
Beispiel #12
0
def serialize_str_fn(s: str) -> JsonType:
    return F1(F1(s))
Beispiel #13
0
def parse_list() -> ParseFn[JsonList]:
    err = error(JsonList)
    return fold3[JsonPrimitive, 'JsonList', 'JsonDict', PreParsed[JsonList]](
        (lambda _: F1(err('JsonPrimitive')), lambda x: F2(x),
         lambda _: F1(err('Dict'))))
Beispiel #14
0
 def bind(self, r: ListResult[A]) -> Parsed[ListResult[A]]:
     return F1(r[0]) if (len(r[1]) == 0
                         and not (len(r[0]) == 0)) else F2(r)
Beispiel #15
0
 def __call__(self, a: Parsed[A]) -> PreParsed[A]:
     return fold2[List[ParseError], A,
                  PreParsed[A]]((lambda x: F1(F1(x)), lambda x: F2(x)))(a)
Beispiel #16
0
def load_json(j: str) -> PreParsed[any]:  # type: ignore
    try:
        return F2(json.loads(j))  # type: ignore
    except Exception as e:
        return F1(F2(UnknownParseError(e)))
Beispiel #17
0
def prims(t: Type[A]) -> fold4[str, int, bool, None, PreParsed[A]]:
    err = error(t)
    return fold4[str, int, bool, None, PreParsed[A]](
        (lambda x: F1(err('str')), lambda x: F1(err('int')),
         lambda x: F1(err('bool')), lambda x: F1(err('None'))))
Beispiel #18
0
def parse_prim(t: Type[A], f: fold4[str, int, bool, None,
                                    PreParsed[A]]) -> ParseFn[A]:
    err = error(t)
    return fold3[JsonPrimitive, 'JsonList', 'JsonDict', PreParsed[B]](
        (f, lambda x: F1(err('List')), lambda x: F1(err('Dict'))))
Beispiel #19
0
def serialize_int_fn(i: int) -> JsonType:
    return F1(F2(i))
Beispiel #20
0
 def __init__(self) -> None:
     self._run = Fn[JsonType,
                    PreParsed[H]](lambda x: F1(F2(ParseException())))
     self._path = []
Beispiel #21
0
def serialize_none_fn(n: None) -> JsonType:
    return F1(F4(n))
Beispiel #22
0
 def map_error(self, e: E) -> Parsed[E, A]:
     return F1(e)