Skip to content

Convert dicts to metadatavalues helper function #40

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)



Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions