Example #1
0
    def parse(self, serviceName: str, rawService: dict = None):
        if rawService is None:
            return Service(serviceName,
                           self.__typeResolver.resolve(serviceName))

        arguments = self.__parseArguments(rawService)
        tags = rawService['tags'] if 'tags' in rawService else []
        class_ = self.__typeResolver.resolve(
            rawService['class'] if 'class' in rawService else
            serviceName)  # pylint: disable = invalid-name

        service = Service(serviceName, class_, arguments, tags)

        if 'autowire' in rawService:
            service.setAutowire(rawService['autowire'] is True)

        if 'factory' in rawService:
            if rawService['factory'][0][0:1] != '@':
                raise Exception(
                    'Factory service name must be prefixed with @ (service {})'
                    .format(serviceName))

            service.setFactory(ServiceArgument(rawService['factory'][0][1:]),
                               rawService['factory'][1])

        return service
    def test_factory(self):
        result = self.__serviceParser.parse('injecta.api.ApiClient', {
            'factory': ['@injecta.api.ApiClientFactory.ApiClientFactory', 'create'],
            'arguments': [
                'Jirka',
            ]
        })
        expected = Service(
            'injecta.api.ApiClient',
            DType('injecta.api', 'ApiClient'),
            [
                PrimitiveArgument('Jirka'),
            ]
        )
        expected.setFactory(ServiceArgument('injecta.api.ApiClientFactory.ApiClientFactory'), 'create')

        self.assertEqual(expected, result)