def _build_stitch_directive(source_field_name, sink_field_name):
    """Build a Directive node for the stitch directive."""
    return ast_types.DirectiveNode(
        name=ast_types.NameNode(value="stitch"),
        arguments=[
            ast_types.ArgumentNode(
                name=ast_types.NameNode(value="source_field"),
                value=ast_types.StringValueNode(value=source_field_name),
            ),
            ast_types.ArgumentNode(
                name=ast_types.NameNode(value="sink_field"),
                value=ast_types.StringValueNode(value=sink_field_name),
            ),
        ],
    )
Beispiel #2
0
 def visit_deprecated_directive(self, obj):
     return ast.DirectiveNode(
         name=_name('deprecated'),
         arguments=[
             ast.ArgumentNode(
                 name=_name('reason'),
                 value=ast.StringValueNode(value=obj.reason),
             ),
         ],
     )
Beispiel #3
0
 def visit_requires_directive(self, obj):
     return ast.DirectiveNode(
         name=_name('requires'),
         arguments=[
             ast.ArgumentNode(
                 name=_name('fields'),
                 value=ast.StringValueNode(value=obj.fields),
             ),
         ],
     )
Beispiel #4
0
def get_ast_value(value):
    if isinstance(value, ast.Node):
        return value
    if isinstance(value, ast.ValueNode):
        return value
    if isinstance(value, str):
        return ast.StringValueNode(value=value)
    elif isinstance(value, bool):
        return ast.BooleanValueNode(value=value)
    elif isinstance(value, (float, decimal.Decimal)):
        return ast.FloatValueNode(value=value)
    elif isinstance(value, int):
        return ast.IntValueNode(value=value)
    elif isinstance(value, list):
        return ast.ListValueNode(values=[get_ast_value(v) for v in value])
    return None
Beispiel #5
0
def _encode(value):
    if value is None:
        return ast.NullValueNode()
    elif isinstance(value, bool):
        return ast.BooleanValueNode(value=value)
    elif isinstance(value, int):
        return ast.IntValueNode(value=str(value))
    elif isinstance(value, float):
        return ast.FloatValueNode(value=str(value))
    elif isinstance(value, str):
        return ast.StringValueNode(value=value)
    elif isinstance(value, list):
        return ast.ListValueNode(values=[_encode(v) for v in value])
    elif isinstance(value, dict):
        return ast.ObjectValueNode(fields=[
            ast.ObjectFieldNode(name=_name(key), value=_encode(val))
            for key, val in value.items()
        ])
    else:
        raise TypeError('Unsupported type: {!r}'.format(value))
Beispiel #6
0
def _encode_default_value(value) -> Optional[ast.ValueNode]:
    if value == Nothing:
        return None

    if value is None:
        return ast.NullValueNode()

    if isinstance(value, bool):
        return ast.BooleanValueNode(value=value)

    if isinstance(value, int):
        return ast.IntValueNode(value=f"{value:d}")
    if isinstance(value, float) and isfinite(value):
        return ast.FloatValueNode(value=f"{value:g}")

    if isinstance(value, str):
        return ast.StringValueNode(value=value)

    if isinstance(value, Iterable) and not isinstance(value, str):
        maybe_value_nodes = (_encode_default_value(item) for item in value)
        value_nodes = list(filter(None, maybe_value_nodes))
        return ast.ListValueNode(values=[value_nodes])

    raise TypeError(f"Cannot convert value to AST: {inspect(value)}.")
Beispiel #7
0
def serialize_string(value):
    return ast.StringValueNode(value=value)