Exemple #1
0
def resolve_ptr(near_endpoint: s_sources.Source,
                pointer_name: str,
                *,
                direction: s_pointers.PointerDirection = (
                    s_pointers.PointerDirection.Outbound),
                source_context: typing.Optional[parsing.ParserContext] = None,
                ctx: context.ContextLevel) -> s_pointers.Pointer:

    if not isinstance(near_endpoint, s_sources.Source):
        # Reference to a property on non-object
        msg = 'invalid property reference on a primitive type expression'
        raise errors.InvalidReferenceError(msg, context=source_context)

    ctx.env.schema, ptr = near_endpoint.resolve_pointer(ctx.env.schema,
                                                        pointer_name,
                                                        direction=direction)

    if ptr is None:
        if isinstance(near_endpoint, s_links.Link):
            msg = (f'{near_endpoint.get_displayname(ctx.env.schema)} '
                   f'has no property {pointer_name!r}')

        elif direction == s_pointers.PointerDirection.Outbound:
            msg = (f'{near_endpoint.get_displayname(ctx.env.schema)} '
                   f'has no link or property {pointer_name!r}')

        else:
            nep_name = near_endpoint.get_displayname(ctx.env.schema)
            path = f'{nep_name}.{direction}{pointer_name}'
            msg = f'{path!r} does not resolve to any known path'

        err = errors.InvalidReferenceError(msg, context=source_context)

        if direction == s_pointers.PointerDirection.Outbound:
            near_enpoint_pointers = near_endpoint.get_pointers(ctx.env.schema)
            s_utils.enrich_schema_lookup_error(
                err,
                pointer_name,
                modaliases=ctx.modaliases,
                item_types=(s_pointers.Pointer, ),
                collection=near_enpoint_pointers.objects(ctx.env.schema),
                schema=ctx.env.schema)

        raise err

    return ptr
Exemple #2
0
def resolve_ptr(
        near_endpoint: s_sources.Source,
        pointer_name: str, *,
        direction: s_pointers.PointerDirection=(
            s_pointers.PointerDirection.Outbound
        ),
        source_context: typing.Optional[parsing.ParserContext]=None,
        ctx: context.ContextLevel) -> s_pointers.Pointer:

    if not isinstance(near_endpoint, s_sources.Source):
        # Reference to a property on non-object
        msg = 'invalid property reference on a primitive type expression'
        raise errors.InvalidReferenceError(msg, context=source_context)

    if direction is s_pointers.PointerDirection.Outbound:
        ptr = near_endpoint.getptr(ctx.env.schema, pointer_name)
    else:
        ptrs = near_endpoint.getrptrs(ctx.env.schema, pointer_name)
        if not ptrs:
            ptr = None
        else:
            if len(ptrs) == 1:
                ptr = next(iter(ptrs))
            else:
                ctx.env.schema_refs.update(
                    p.get_nearest_non_derived_parent(ctx.env.schema)
                    for p in ptrs
                )
                ctx.env.schema, ptr = s_pointers.get_or_create_union_pointer(
                    ctx.env.schema,
                    ptrname=pointer_name,
                    source=near_endpoint,
                    direction=direction,
                    components=ptrs,
                    modname=ctx.derived_target_module)

    if ptr is None:
        if isinstance(near_endpoint, s_links.Link):
            msg = (f'{near_endpoint.get_verbosename(ctx.env.schema)} '
                   f'has no property {pointer_name!r}')

        elif direction == s_pointers.PointerDirection.Outbound:
            msg = (f'{near_endpoint.get_verbosename(ctx.env.schema)} '
                   f'has no link or property {pointer_name!r}')

        else:
            nep_name = near_endpoint.get_displayname(ctx.env.schema)
            path = f'{nep_name}.{direction}{pointer_name}'
            msg = f'{path!r} does not resolve to any known path'

        err = errors.InvalidReferenceError(msg, context=source_context)

        if direction == s_pointers.PointerDirection.Outbound:
            near_enpoint_pointers = near_endpoint.get_pointers(
                ctx.env.schema)
            s_utils.enrich_schema_lookup_error(
                err, pointer_name, modaliases=ctx.modaliases,
                item_types=(s_pointers.Pointer,),
                collection=near_enpoint_pointers.objects(ctx.env.schema),
                schema=ctx.env.schema
            )

        raise err

    ref = ptr.get_nearest_non_derived_parent(ctx.env.schema)
    ctx.env.schema_refs.add(ref)

    return ptr