def update_collections_with_deprecated_resources(gapic_v2,
                                                 pattern_resource_map,
                                                 collection_oneofs):
    for interface in gapic_v2.get('interfaces', ()):
        if 'deprecated_collections' not in interface:
            continue
        for deprecated_collection in interface['deprecated_collections']:
            if 'entity_name' not in deprecated_collection:
                raise ValueError('entity_name is required '
                                 'in a deprecated_collection.')
            if 'name_pattern' not in deprecated_collection:
                raise ValueError('name_pattern is required '
                                 'in a deprecated_collection.')

            name_pattern = deprecated_collection['name_pattern']
            if name_pattern not in pattern_resource_map:
                raise ValueError(
                    'deprecated collection has '
                    'an unknown name pattern: {}'.format(name_pattern))

            resource = _get_resource_for_deprecate_pattern(
                name_pattern, pattern_resource_map)
            oneof_name = to_snake(resource.type.split('/')[-1]) + '_oneof'
            if oneof_name not in collection_oneofs:
                raise ValueError(
                    'internal: multi-pattern resource not added to '
                    'collection_oneofs: {}'.format(oneof_name))
            # Note Gapic config does not actually have the
            # `has_deprecated_collections` field.
            # This field is here to turn off generating `parseList`
            # and `toStringList` methods to avoid naming collisions.
            collection_oneofs[oneof_name]['has_deprecated_collections'] = True
def update_collections_with_deprecated_resources(gapic_v2,
                                                 pattern_resource_map,
                                                 collections,
                                                 collection_oneofs):
    for interface in gapic_v2.get('interfaces', ()):
        if 'deprecated_collections' not in interface:
            continue
        for deprecated_collection in interface['deprecated_collections']:
            if 'entity_name' not in deprecated_collection:
                raise ValueError('entity_name is required '
                                 'in a deprecated_collection.')
            if 'name_pattern' not in deprecated_collection:
                raise ValueError('name_pattern is required '
                                 'in a deprecated_collection.')

            entity_name = deprecated_collection['entity_name']
            name_pattern = deprecated_collection['name_pattern']

            if entity_name not in collections:
                collections[entity_name] = deprecated_collection
            if name_pattern not in pattern_resource_map:
                raise ValueError(
                    'deprecated collection has '
                    'an unknown name pattern: {}'.format(name_pattern))
            resource = _get_resource_for_deprecate_pattern(
                name_pattern, pattern_resource_map)
            oneof_name = to_snake(resource.type.split('/')[-1]) + '_oneof'
            if oneof_name not in collection_oneofs:
                raise ValueError(
                    'internal: multi-pattern resource not added to '
                    'collection_oneofs: {}'.format(oneof_name))
            collection_oneofs[oneof_name]['collection_names'].append(
                entity_name)
def update_collections(res, collections, collection_oneofs):
    # Determine the name.
    name = to_snake(res.type.split('/')[-1])

    # pylint: disable=no-member
    if len(res.pattern) == 0:
        raise ValueError('patterns not found for resource {}'.format(res.type))
    elif len(res.pattern) == 1:
        if res.pattern[0] == "*":
            return
        # for a single-pattern resource name, the unqualified
        # name of the resource is the collection entity name in gapic v1
        collections.setdefault(name, {}).update({
            'entity_name': name,
            'name_pattern': res.pattern[0]
        })
    else:
        # for a multi-pattern resource name, the unqualified name of
        # the resource is the oneof name of a collection_oneof in gapic v1
        oneof_name = name + '_oneof'

        # Note Gapic config does not actually have the `pattern_strings` field.
        # This field is here to generate the self-sustained multi-pattern
        # Java resource class.
        collection_oneofs.setdefault(oneof_name, {}).update({
            'oneof_name':
            oneof_name,
            'collection_names': [],
            'pattern_strings': [p for p in res.pattern if p != "*"]
        })
def calculate_pattern_entity_name(ptn):

    if isFixedPattern(ptn):
        start_index = next(i for (i, c) in enumerate(list(ptn)) if c.isalpha())
        end_index = len(ptn) - next(
            i for (i, c) in enumerate(list(ptn)[::-1]) if c.isalpha())
        name_parts = re.split(r'[^a-zA-Z]', ptn[start_index:end_index])
        return '_'.join(name_parts)
    else:
        segs = []
        for seg in ptn.split('/'):
            if _is_variable_segment(seg):
                segs.append(to_snake(seg[1:-1]))
        return '_'.join(segs)
Exemple #5
0
def get_pattern_name(pattern):
    if is_fixed_pattern(pattern):
        start_index = \
            next(i for (i, c) in enumerate(list(pattern)) if c.isalpha())
        end_index = len(pattern) - next(
            i for (i, c) in enumerate(list(pattern)[::-1]) if c.isalpha())
        name_parts = re.split(r'[^a-zA-Z]', pattern[start_index:end_index])
        return '_'.join(name_parts)
    else:
        name = '_'.join(get_id_segments(pattern))
        last_segment = pattern.split('/')[-1]
        if last_segment.startswith('{') and last_segment.endswith('}'):
            return name
        elif last_segment.startswith('{') or last_segment.endswith('}'):
            raise ValueError(
                'segment {} of pattern {} has unmatching braces'.format(
                    last_segment, pattern))
        else:
            return name + '_' + casing_utils.to_snake(last_segment)
Exemple #6
0
def test_constant_to_snake():
    assert to_snake('CONSTANT_CASE_THING') == 'constant_case_thing'
Exemple #7
0
def test_camel_to_snake():
    assert to_snake('camelCaseThing') == 'camel_case_thing'
Exemple #8
0
def test_pascal_to_snake():
    assert to_snake('PascalCaseThing') == 'pascal_case_thing'