Files
zenml/venv/lib/python3.9/site-packages/graphene/utils/crunch.py
Christian Mantha 2ca0b9ef7c star
2026-03-02 19:10:52 -05:00

36 lines
756 B
Python

import json
from collections.abc import Mapping
def to_key(value):
return json.dumps(value)
def insert(value, index, values):
key = to_key(value)
if key not in index:
index[key] = len(values)
values.append(value)
return len(values) - 1
return index.get(key)
def flatten(data, index, values):
if isinstance(data, (list, tuple)):
flattened = [flatten(child, index, values) for child in data]
elif isinstance(data, Mapping):
flattened = {key: flatten(child, index, values) for key, child in data.items()}
else:
flattened = data
return insert(flattened, index, values)
def crunch(data):
index = {}
values = []
flatten(data, index, values)
return values