|
| 1 | +# Copyright (c) 2015, Facebook, Inc. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. An additional grant |
| 6 | +# of patent rights can be found in the PATENTS file in the same directory. |
| 7 | + |
| 8 | +from casing import snake |
| 9 | +import ast_cython_c |
| 10 | +import ast_cython |
| 11 | + |
| 12 | +SIMPLE_RETURN_CASTS = { |
| 13 | + 'boolean': 'bool', |
| 14 | + 'int': '', |
| 15 | + 'string': '', # XXX decode? |
| 16 | + 'OperationKind': '' |
| 17 | +} |
| 18 | + |
| 19 | +SOURCE_TYPE_CASTS = { |
| 20 | + 'IntValue': 'int', |
| 21 | + 'FloatValue': 'float', |
| 22 | + 'StringValue': '', # XXX decode? |
| 23 | + 'BooleanValue': 'bool', |
| 24 | + 'EnumValue': '' # XXX decode? |
| 25 | +} |
| 26 | + |
| 27 | +def field_prototype(owning_type, type, name, nullable, plural): |
| 28 | + _map = {'cmodule': ast_cython_c.CMODULE_NAME, |
| 29 | + 'owning_st': ast_cython_c.struct_name(owning_type), |
| 30 | + 'snake': snake(name), |
| 31 | + 'return_st': ast_cython_c.struct_name(type)} |
| 32 | + if plural: |
| 33 | + return ''' |
| 34 | + def get_%(snake)s_size(self): |
| 35 | + return int(%(cmodule)s.%(owning_st)s_get_%(snake)s_size(self._wrapped)) |
| 36 | +''' % _map |
| 37 | + if type in SIMPLE_RETURN_CASTS: |
| 38 | + # TODO: convert string to unicode |
| 39 | + if owning_type in SOURCE_TYPE_CASTS: |
| 40 | + _map['cast'] = SOURCE_TYPE_CASTS[owning_type] |
| 41 | + else: |
| 42 | + _map['cast'] = SIMPLE_RETURN_CASTS[type] |
| 43 | + return ''' |
| 44 | + def get_%(snake)s(self): |
| 45 | + val = %(cmodule)s.%(owning_st)s_get_%(snake)s(self._wrapped) |
| 46 | + if val is None: |
| 47 | + return None |
| 48 | + return %(cast)s(val) |
| 49 | +''' % _map |
| 50 | + elif type in ['Type', 'Value']: |
| 51 | + # XXX this types have no functions... |
| 52 | + return ''' |
| 53 | +''' |
| 54 | + else: |
| 55 | + # python object return type |
| 56 | + return ''' |
| 57 | + def get_%(snake)s(self): |
| 58 | + cdef %(cmodule)s.%(return_st)s *next |
| 59 | + next = %(cmodule)s.%(owning_st)s_get_%(snake)s(self._wrapped) |
| 60 | + if next is NULL: |
| 61 | + return None |
| 62 | + return %(return_st)s.create(next) |
| 63 | +''' % _map |
| 64 | + |
| 65 | +class Printer(object): |
| 66 | + '''Printer for a visitor in cython |
| 67 | + ''' |
| 68 | + |
| 69 | + def __init__(self): |
| 70 | + self._types = [] |
| 71 | + |
| 72 | + def start_file(self): |
| 73 | + print ast_cython_c.license_comment() + ''' |
| 74 | +
|
| 75 | +cimport %s |
| 76 | +
|
| 77 | +cdef class GraphQLAst: |
| 78 | + """Base class for all Ast pieces""" |
| 79 | + pass |
| 80 | +
|
| 81 | +''' % ast_cython_c.CMODULE_NAME |
| 82 | + |
| 83 | + def start_type(self, name): |
| 84 | + self._current_type = name |
| 85 | + _map = {'snake': snake(name), 'name': name} |
| 86 | + print ''' |
| 87 | +
|
| 88 | +cdef class %(name)s(GraphQLAst): |
| 89 | +
|
| 90 | + @staticmethod |
| 91 | + cdef create(%(cmodule)s.%(name)s *thing): |
| 92 | + node = %(name)s() |
| 93 | + node._wrapped = thing |
| 94 | + return node |
| 95 | +
|
| 96 | +''' % {'name': ast_cython_c.struct_name(name), |
| 97 | + 'cmodule': ast_cython_c.CMODULE_NAME} |
| 98 | + |
| 99 | + def field(self, type, name, nullable, plural): |
| 100 | + print field_prototype(self._current_type, type, name, nullable, plural) |
| 101 | + |
| 102 | + def end_type(self, name): |
| 103 | + print |
| 104 | + print |
| 105 | + |
| 106 | + def end_file(self): |
| 107 | + pass |
| 108 | + |
| 109 | + def start_union(self, name): |
| 110 | + pass |
| 111 | + |
| 112 | + def union_option(self, option): |
| 113 | + pass |
| 114 | + |
| 115 | + def end_union(self, name): |
| 116 | + pass |
0 commit comments