def construct_resource(base_url, path_parts, entry, fields):
    path_str = '/'.join(path_parts)
    attributes = {}
    if (models.EntryFields.metadata in fields):
        attributes['metadata'] = entry.metadata
    if (models.EntryFields.specs in fields):
        attributes['specs'] = getattr(entry, 'specs', None)
    if isinstance(entry, DuckTree):
        if (models.EntryFields.count in fields):
            attributes['count'] = len_or_approx(entry)
            if hasattr(entry, 'sorting'):
                attributes['sorting'] = entry.sorting
        resource = models.TreeResource(**{'id': (path_parts[(- 1)] if path_parts else ''), 'attributes': models.TreeAttributes(**attributes), 'type': models.EntryType.tree, 'links': {'self': f'{base_url}metadata/{path_str}', 'search': f'{base_url}search/{path_str}'}})
    else:
        links = {'self': f'{base_url}metadata/{path_str}'}
        structure = {}
        if (entry is not None):
            links.update({link: template.format(base_url=base_url, path=path_str) for (link, template) in FULL_LINKS[entry.structure_family].items()})
            if (models.EntryFields.structure_family in fields):
                attributes['structure_family'] = entry.structure_family
            if (models.EntryFields.macrostructure in fields):
                macrostructure = entry.macrostructure()
                if (macrostructure is not None):
                    structure['macro'] = dataclasses.asdict(macrostructure)
            if (models.EntryFields.microstructure in fields):
                if (entry.structure_family == 'dataframe'):
                    structure['micro'] = {'links': {'meta': f'{base_url}dataframe/meta/{path_str}', 'divisions': f'{base_url}dataframe/divisions/{path_str}'}}
                else:
                    microstructure = entry.microstructure()
                    if (microstructure is not None):
                        structure['micro'] = dataclasses.asdict(microstructure)
                if (entry.structure_family == 'array'):
                    block_template = ','.join((f'{{index_{index}}}' for index in range(len(structure['macro']['shape']))))
                    links['block'] = f'{base_url}array/block/{path_str}?block={block_template}'
                elif (entry.structure_family == 'dataframe'):
                    links['partition'] = f'{base_url}dataframe/partition/{path_str}?partition={{index}}'
                elif (entry.structure_family == 'variable'):
                    block_template = ','.join((f'{{index_{index}}}' for index in range(len(structure['macro']['data']['macro']['shape']))))
                    links['block'] = f'{base_url}variable/block/{path_str}?block={block_template}'
                elif (entry.structure_family == 'data_array'):
                    block_template = ','.join((f'{{index_{index}}}' for index in range(len(structure['macro']['variable']['macro']['data']))))
                    links['block'] = f'{base_url}data_array/block/{path_str}?block={block_template}'
                elif (entry.structure_family == 'dataset'):
                    links['block'] = f'{base_url}dataset/block/{path_str}?variable={{variable}}&block={{block_indexes}}'
                    microstructure = entry.microstructure()
            attributes['structure'] = structure
        resource = models.ReaderResource(**{'id': path_parts[(- 1)], 'attributes': models.ReaderAttributes(**attributes), 'type': models.EntryType.reader, 'links': links})
    return resource
def construct_resource(base_url, path_parts, entry, fields):
    path_str = '/'.join(path_parts)
    attributes = {}
    if (models.EntryFields.metadata in fields):
        attributes['metadata'] = entry.metadata
    if (models.EntryFields.specs in fields):
        attributes['specs'] = getattr(entry, 'specs', None)
    if isinstance(entry, DuckTree):
        if (models.EntryFields.count in fields):
            attributes['count'] = len_or_approx(entry)
            if hasattr(entry, 'sorting'):
                attributes['sorting'] = entry.sorting
        resource = models.TreeResource(**{'id': (path_parts[(- 1)] if path_parts else ), 'attributes': models.TreeAttributes(**attributes), 'type': models.EntryType.tree, 'links': {'self': f'{base_url}metadata/{path_str}', 'search': f'{base_url}search/{path_str}'}})
    else:
        links = {'self': f'{base_url}metadata/{path_str}'}
        structure = {}
        if (entry is not None):
            links.update({link: template.format(base_url=base_url, path=path_str) for (link, template) in FULL_LINKS[entry.structure_family].items()})
            if (models.EntryFields.structure_family in fields):
                attributes['structure_family'] = entry.structure_family
            if (models.EntryFields.macrostructure in fields):
                macrostructure = entry.macrostructure()
                if (macrostructure is not None):
                    structure['macro'] = dataclasses.asdict(macrostructure)
            if (models.EntryFields.microstructure in fields):
                if (entry.structure_family == 'dataframe'):
                    structure['micro'] = {'links': {'meta': f'{base_url}dataframe/meta/{path_str}', 'divisions': f'{base_url}dataframe/divisions/{path_str}'}}
                else:
                    microstructure = entry.microstructure()
                    if (microstructure is not None):
                        structure['micro'] = dataclasses.asdict(microstructure)
                if (entry.structure_family == 'array'):
                    block_template = ','.join((f'{{index_{index}}}' for index in range(len(structure['macro']['shape']))))
                    links['block'] = f'{base_url}array/block/{path_str}?block={block_template}'
                elif (entry.structure_family == 'dataframe'):
                    links['partition'] = f'{base_url}dataframe/partition/{path_str}?partition={{index}}'
                elif (entry.structure_family == 'variable'):
                    block_template = ','.join((f'{{index_{index}}}' for index in range(len(structure['macro']['data']['macro']['shape']))))
                    links['block'] = f'{base_url}variable/block/{path_str}?block={block_template}'
                elif (entry.structure_family == 'data_array'):
                    block_template = ','.join((f'{{index_{index}}}' for index in range(len(structure['macro']['variable']['macro']['data']))))
                    links['block'] = f'{base_url}data_array/block/{path_str}?block={block_template}'
                elif (entry.structure_family == 'dataset'):
                    links['block'] = f'{base_url}dataset/block/{path_str}?variable={{variable}}&block={{block_indexes}}'
                    microstructure = entry.microstructure()
            attributes['structure'] = structure
        resource = models.ReaderResource(**{'id': path_parts[(- 1)], 'attributes': models.ReaderAttributes(**attributes), 'type': models.EntryType.reader, 'links': links})
    return resource