예제 #1
0
def value_node_from_pyvalue(val: object):
    if isinstance(val, str):
        val = val.replace('\\', '\\\\')
        value = eql_quote.quote_literal(val)
        return gql_ast.StringValue(value=value[1:-1])
    elif isinstance(val, bool):
        return gql_ast.BooleanValue(value=bool(val))
    elif isinstance(val, int):
        return gql_ast.IntValue(value=str(val))
    elif isinstance(val, float):
        return gql_ast.FloatValue(value=str(val))
    elif isinstance(val, list):
        return gql_ast.ListValue(
            values=[value_node_from_pyvalue(v) for v in val])
    elif isinstance(val, dict):
        return gql_ast.ObjectValue(fields=[
            gql_ast.ObjectField(name=n, value=value_node_from_pyvalue(v))
            for n, v in val.items()
        ])
    else:
        raise ValueError(f'unexpected constant type: {type(val)!r}')
예제 #2
0
    def get_schema_reflection_default(
        self,
        schema: s_schema.Schema,
    ) -> Optional[str]:
        """Return the default expression if this is a reflection of a
           schema class field and the field has a defined default value.
        """
        ptr = self.get_nearest_non_derived_parent(schema)

        src = ptr.get_source(schema)
        if src is None:
            # This is an abstract pointer
            return None

        ptr_name = ptr.get_name(schema)
        if ptr_name.module not in {'schema', 'sys', 'cfg'}:
            # This isn't a reflection type
            return None

        if isinstance(src, Pointer):
            # This is a link property
            tgt = src.get_target(schema)
            assert tgt is not None
            schema_objtype = tgt
        else:
            assert isinstance(src, s_types.Type)
            schema_objtype = src

        assert isinstance(schema_objtype, so.QualifiedObject)
        src_name = schema_objtype.get_name(schema)
        mcls = so.ObjectMeta.get_schema_metaclass(src_name.name)
        if mcls is None:
            # This schema class is not (publicly) reflected.
            return None
        fname = ptr.get_shortname(schema).name
        if not mcls.has_field(fname):
            # This pointer is not a schema field.
            return None
        field = mcls.get_field(fname)
        if not isinstance(field, so.SchemaField):
            # Not a schema field, no default possible.
            return None
        f_default = field.default
        if (f_default is None or f_default is so.NoDefault):
            # No explicit default value.
            return None

        tgt = ptr.get_target(schema)

        if f_default is so.DEFAULT_CONSTRUCTOR:
            if (issubclass(
                    field.type,
                (collections.abc.Set, collections.abc.Sequence),
            ) and not issubclass(field.type, (str, bytes))):
                return f'<{tgt.get_displayname(schema)}>[]'
            else:
                return None

        default = qlquote.quote_literal(json.dumps(f_default))

        if tgt.is_enum(schema):
            return f'<{tgt.get_displayname(schema)}><str>to_json({default})'
        else:
            return f'<{tgt.get_displayname(schema)}>to_json({default})'