fjuniorr / flowmapper

Mappings between elementary flows
MIT License
0 stars 1 forks source link

Make Context class generic for dealing with multiple segments #53

Closed fjuniorr closed 6 months ago

fjuniorr commented 6 months ago

Currently Context.from_dict only handles contexts with two segments and the glom spec for the field mapping must specify each segment separately. For example:

{
    "context": [
        ["categories.0", "categories.1"], 
        ["compartment.compartment.#text", "compartment.subcompartment.#text"]
    ],
}

The idea is to require a glom spec that returns a list of segments, and change Context.from_dict accordingly. Here are some examples of specs that return ["water", "ocean"] for different shapes source flows:

from glom import glom

flow = {
    "uuid": "74a0aabb-e11b-4f3b-8921-45e447b33393",
    "context": ["water", "ocean"]
}

glom(flow, 'context')

flow = {
    "uuid": "74a0aabb-e11b-4f3b-8921-45e447b33393",
    "context": {
      "compartment": {
        "name": "water"
      },
      "subcompartment": {
        "name": "ocean"
      }
    }
}

glom(flow, 'context.*.name')

flow = {
    "uuid": "74a0aabb-e11b-4f3b-8921-45e447b33393",
    "context": [
      {"name": "water"},
      {"name": "ocean"}
    ]
}

glom(flow, ('context', ['name']))
fjuniorr commented 6 months ago

This is done in https://github.com/fjuniorr/flowmapper/commit/abc58f735895a6462f0fc8304e3d6706d7cb3c79 but there is one possible shape for context data that I don't yet know how to handle with glom (ie. a spec that returns ["water", "ocean"]):

flow = {
    "uuid": "74a0aabb-e11b-4f3b-8921-45e447b33393",
    "compartment": {
        "name": "water"
      },
    "subcompartment": {
        "name": "ocean"
      },
    "unit": {
        "name": "kg"
    }
}

It would likely involve glom(data, '*.name') with some sort of filtering.

I'm closing this and will reopen as needed.