Skip to content

Convert dicts to metadatavalues helper function #40

@nsa-brant

Description

@nsa-brant

Recommend adding a helper function for converting regular dicts to metadatavalues, thoughts?

def convert_dict_to_metadata_values(metadata_dict: dict) -> MetadataValues:
    """Convert a dictionary to Iconik MetadataValues format.
    
    Args:
        metadata_dict: Dictionary containing metadata key-value pairs
        
    Returns:
        MetadataValues object with properly formatted fields
    """
    formatted_dict = {}
    
    def _should_iterate(value: Any) -> bool:
        """Check if value should be treated as iterable."""
        return (
            isinstance(value, Iterable) 
            and not isinstance(value, (str, bytes))
            and not isinstance(value, dict)
        )
    
    for key, value in metadata_dict.items():
        if _should_iterate(value):
            # Create multiple FieldValue objects for iterables
            field_values = [FieldValue(value=v) for v in value]
        else:
            # Single value case
            field_values = [FieldValue(value=value)]
            
        formatted_dict[key] = FieldValues(field_values=field_values)
    
    return MetadataValues(root=formatted_dict)



Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions