Improved plugin structure based on @adamcharnock suggestions

This commit is contained in:
Syrus Akbary 2015-12-06 16:34:47 -08:00
parent 730de3f289
commit cd5d9b8eea
6 changed files with 24 additions and 35 deletions

View File

@ -50,10 +50,12 @@ class Schema(object):
plugin.contribute_to_schema(self) plugin.contribute_to_schema(self)
self.plugins.append(plugin) self.plugins.append(plugin)
def get_internal_type(self, objecttype): def get_default_namedtype_name(self, value):
for plugin in self.plugins: for plugin in self.plugins:
objecttype = plugin.transform_type(objecttype) if not hasattr(plugin, 'get_default_namedtype_name'):
return objecttype.internal_type(self) continue
value = plugin.get_default_namedtype_name(value)
return value
def T(self, _type): def T(self, _type):
if not _type: if not _type:
@ -62,7 +64,7 @@ class Schema(object):
is_instancetype = isinstance(_type, InstanceType) is_instancetype = isinstance(_type, InstanceType)
if is_classtype or is_instancetype: if is_classtype or is_instancetype:
if _type not in self._types: if _type not in self._types:
internal_type = self.get_internal_type(_type) internal_type = _type.internal_type(self)
self._types[_type] = internal_type self._types[_type] = internal_type
if is_classtype: if is_classtype:
self.register(_type) self.register(_type)

View File

@ -11,9 +11,7 @@ class Argument(NamedType, OrderedType):
def __init__(self, type, description=None, default=None, def __init__(self, type, description=None, default=None,
name=None, _creation_counter=None): name=None, _creation_counter=None):
super(Argument, self).__init__(_creation_counter=_creation_counter) super(Argument, self).__init__(name=name, _creation_counter=_creation_counter)
self.name = name
self.attname = None
self.type = type self.type = type
self.description = description self.description = description
self.default = default self.default = default
@ -38,18 +36,18 @@ def to_arguments(*args, **kwargs):
arguments = {} arguments = {}
iter_arguments = chain(kwargs.items(), [(None, a) for a in args]) iter_arguments = chain(kwargs.items(), [(None, a) for a in args])
for attname, arg in iter_arguments: for default_name, arg in iter_arguments:
if isinstance(arg, Argument): if isinstance(arg, Argument):
argument = arg argument = arg
elif isinstance(arg, ArgumentType): elif isinstance(arg, ArgumentType):
argument = arg.as_argument() argument = arg.as_argument()
else: else:
raise ValueError('Unknown argument %s=%r' % (attname, arg)) raise ValueError('Unknown argument %s=%r' % (default_name, arg))
if attname: if default_name:
argument.attname = attname argument.default_name = default_name
name = argument.name or argument.attname name = argument.name or argument.default_name
assert name, 'Argument in field must have a name' assert name, 'Argument in field must have a name'
assert name not in arguments, 'Found more than one Argument with same name {}'.format(name) assert name not in arguments, 'Found more than one Argument with same name {}'.format(name)
arguments[name] = argument arguments[name] = argument

View File

@ -129,7 +129,10 @@ class MountedType(FieldType, ArgumentType):
class NamedType(InstanceType): class NamedType(InstanceType):
pass def __init__(self, name=None, default_name=None, *args, **kwargs):
self.name = name
self.default_name = None
super(NamedType, self).__init__(*args, **kwargs)
class GroupNamedType(InstanceType): class GroupNamedType(InstanceType):
@ -138,7 +141,7 @@ class GroupNamedType(InstanceType):
self.types = types self.types = types
def get_named_type(self, schema, type): def get_named_type(self, schema, type):
name = type.name or type.attname name = type.name or schema.get_default_namedtype_name(type.default_name)
return name, schema.T(type) return name, schema.T(type)
def iter_types(self, schema): def iter_types(self, schema):

View File

@ -19,8 +19,7 @@ class Field(NamedType, OrderedType):
self, type, description=None, args=None, name=None, resolver=None, self, type, description=None, args=None, name=None, resolver=None,
required=False, default=None, *args_list, **kwargs): required=False, default=None, *args_list, **kwargs):
_creation_counter = kwargs.pop('_creation_counter', None) _creation_counter = kwargs.pop('_creation_counter', None)
super(Field, self).__init__(_creation_counter=_creation_counter) super(Field, self).__init__(name=name, _creation_counter=_creation_counter)
self.name = name
if isinstance(type, six.string_types): if isinstance(type, six.string_types):
type = LazyType(type) type = LazyType(type)
self.required = required self.required = required
@ -37,6 +36,7 @@ class Field(NamedType, OrderedType):
cls, (FieldsClassType)), 'Field {} cannot be mounted in {}'.format( cls, (FieldsClassType)), 'Field {} cannot be mounted in {}'.format(
self, cls) self, cls)
self.attname = attname self.attname = attname
self.default_name = attname
self.object_type = cls self.object_type = cls
self.mount(cls) self.mount(cls)
if isinstance(self.type, MountType): if isinstance(self.type, MountType):
@ -120,7 +120,6 @@ class InputField(NamedType, OrderedType):
def __init__(self, type, description=None, default=None, def __init__(self, type, description=None, default=None,
name=None, _creation_counter=None, required=False): name=None, _creation_counter=None, required=False):
super(InputField, self).__init__(_creation_counter=_creation_counter) super(InputField, self).__init__(_creation_counter=_creation_counter)
self.name = name
if required: if required:
type = NonNull(type) type = NonNull(type)
self.type = type self.type = type
@ -132,6 +131,7 @@ class InputField(NamedType, OrderedType):
cls, (InputObjectType)), 'InputField {} cannot be mounted in {}'.format( cls, (InputObjectType)), 'InputField {} cannot be mounted in {}'.format(
self, cls) self, cls)
self.attname = attname self.attname = attname
self.default_name = attname
self.object_type = cls self.object_type = cls
self.mount(cls) self.mount(cls)
if isinstance(self.type, MountType): if isinstance(self.type, MountType):

View File

@ -27,7 +27,7 @@ def test_to_arguments():
other_kwarg=String(), other_kwarg=String(),
) )
assert [a.name or a.attname for a in arguments] == [ assert [a.name or a.default_name for a in arguments] == [
'myArg', 'otherArg', 'my_kwarg', 'other_kwarg'] 'myArg', 'otherArg', 'my_kwarg', 'other_kwarg']

View File

@ -1,22 +1,8 @@
from ..core.types.base import GroupNamedType from ..utils import to_camel_case
from ..utils import memoize, to_camel_case
from .base import Plugin from .base import Plugin
def camelcase_named_type(schema, type):
name = type.name or to_camel_case(type.attname)
return name, schema.T(type)
class CamelCase(Plugin): class CamelCase(Plugin):
@memoize def get_default_namedtype_name(self, value):
def transform_group(self, _type): return to_camel_case(value)
new_type = _type.__class__(*_type.types)
setattr(new_type, 'get_named_type', camelcase_named_type)
return new_type
def transform_type(self, _type):
if isinstance(_type, GroupNamedType):
return self.transform_group(_type)
return _type