Mappings

A mapping says how another system's file and one of your foundation's types correspond: a catalog feed's CSV from @/vehicle, a lead's XML into @/lead, a grader's JSON answer onto a submission. An integration moves entries through one. Each mapping is a file in the foundation's src/mappings/, named by the file, the way src/schemas/ holds your types:

# src/mappings/vehicle-to-meta.yml
label: Meta catalog
model: '@/vehicle'
direction: out
shape: list
format: { type: csv }
fields:
  - { title: listing.title }
  - { vin: listing.vin }
  - { price: { from: listing.price } }

uniweb register registers a foundation's mappings with it, and your Application Engine checks each against the type it names. A mapping that doesn't fit is refused there, with every problem listed as where: what, so you can fix them all in one pass. This page is the language that check holds a mapping to.

The one reading rule

A mapping goes one way. direction: in turns the other system's entries into yours; direction: out turns yours into its file. A case that goes both ways is two mappings.

In every rule, the left side is what is written, and the right side is where it is read from:

left (written)right (read)
direction: inyour field, section.fielda path in their file
direction: outa path in their fileyour field, section.field

The frame

Key
label, descriptionoptionalthe mapping's name and description, as the Solution Owner Console shows them
modelrequiredthe type it maps: @/name for one of your foundation's, or a type in full
directionrequiredin or out
shaperequireddocument (one file, one entry), list (one file, many entries) or set (one file, several linked types, inbound)
formatrequired{ type: csv, delimiter?: ',', header?: true }, { type: xml, instructions?: [...] } or { type: json }
pathlist onlywhere the entries are in the file; leave it out for the file's root (a CSV file, a JSON array)
keyinbound list: requiredthe path, within one entry, of the value that identifies it, so an entry sent again updates rather than duplicates
keys_fromoutbound onlyanother of your mappings, the inbound one that recorded the other system's keys, so they are written back
followoutbound onlyother entries to read from (below)
fieldsyour single Sections' fields
sectionsyour multi Sections
partsset onlythe linked parts (below)

Any other key is refused. So is a path on a document (path: is for the list shape; a document is one entity).

fields

fields is a list of one-entry maps, - target: rule, written in the order the other system expects: CSV columns and XML children are written in that order. A map in its place is refused, and so is a target named twice.

fields holds your single Sections only. Your side is section.field, split at its last ., and a nested Section is named by its path (media/photos.url). A multi Section goes under sections, and naming one here says so (it is a multi Section: map it under sections).

A rule's right side is a string, short for { from: <string> }, or a rule.

The other system's side: paths

One path language serves CSV, XML and JSON:

  • CSV: the file is its rows; a path is a column's name (Stock Number), or its position (1, 2, …) with header: false. Every value is text.
  • XML: a child is its name, an attribute is @name, and the text is #text. A path from the file's root starts with the root element (feed/vehicle).
  • JSON: the value as parsed.

Segments are separated by /, and each treats a single value as a list of one:

SegmentSelects
namethe child name, each one when there are several
name[*]the same, said explicitly
name[@type='main']the children name whose attribute type is main
@attran attribute (last segment only)
#textthe text (last segment only)
"name"a name taken literally, when it holds /, [, ] or . ("image[0].url")

Writing out, a path creates what it names: item[@id='x']/title writes an item element with id="x". An outbound XML name must be a valid XML name, and an outbound CSV target is one plain column name.

Rules

A rule has exactly one source:

SourceDirection
from: <path>a value read from the other sideboth
constant: <value>a fixed valueboth
template: "{make} {model}"text with {path} placeholders from the reading side; {{ and }} are literal braces. A template whose every placeholder is empty writes nothingboth
languages: { en: <path>, fr: <path> }one path per language, into a localized fieldin
uuid: self, or uuid: <alias>an entry's own id, or a followed entry'sout, under fields

Conversions

Conversions apply to a from source only, each at most once, in this order:

  1. locale: en: out only, one language of a localized field, with its fallbacks. Reading a localized field out without it is refused.
  2. split: "|", then first: true: one text into several values; keep the first.
  3. One of lookup, date or number, applied to each value:
    • lookup: { Y: true, N: false }: a value not listed fails its entry, unless the table has a "*" entry;
    • date: "MM/yyyy": a pattern of yyyy, MM, dd, HH, mm, ss and literal characters, in UTC both ways. Inbound, it must be at least as precise as the field;
    • number: { decimal?: ',', strip?: '$,' }: strip removes characters before reading, inbound.
  4. join: ",": several values into one text.
  5. default: <value>: the value when nothing is left.

json_text: true (inbound) keeps an object or list whole, written as JSON text into a string or text field; it takes no conversion but default.

A CSV or XML file's values are text. Reading one into an int or decimal field needs number: {} (or a lookup), and into a boolean a lookup. The refusal names the fix: the field is a boolean, and this file's values are text: add a lookup (e.g. {"yes": true, "no": false}). A JSON file's numbers need nothing.

Where one value is expected and several can arrive, add first or join: a split into a field that holds one value, or, out, a list into a CSV cell, an attribute or an element's text.

Inbound, a constant, a default and each lookup result must already fit the field, by its JSON type.

What a mapping never carries

References (entity_ref, item_ref), file fields and secret fields. Out, read a referenced entry with follow instead. A secret is never written by a mapping.

Inbound, every required field of each Section the mapping writes must be written by some rule.

sections

One entry per multi Section:

Key
sectionrequiredyour Section's path (answers, media/photos)
pathrequiredwhere the repeated elements are, relative to the entry (or the parent's element, nested)
keyinbound: requiredthe path, within one element, of the value that identifies the item
fieldsthe Section's fields, named by their bare key (url, not photos.url)
sectionsmulti Sections nested under this one
ofoutbound, top levelthe follow alias whose Section this is
sections:
  - section: media/photos
    path: Photos
    key: '#text'
    fields:
      - { url: { from: '#text' } }

A CSV row holds one value per column, so an outbound CSV mapping writes a multi Section with a field rule and join instead: { Photos: { from: photos.url, join: '|' } }.

follow

An outbound mapping may read entries other than the one it writes, each reached along a reference field pinned to one type. follow is a list of one-entry maps, in order:

follow:
  - { quiz: { field: identity.quiz_ref } }                                      # forward: the entry a field names
  - { key:  { model: '@/quiz-key', field: identity.quiz, names: quiz } }       # reverse: the entry whose field names one above
fields:
  - { submission: { uuid: self } }
  - { quiz: 'quiz:identity.slug' }
  - { passing: 'quiz:identity.passing' }

A forward line takes field (a single Section's reference, pinned to one type) and may start from an alias above. A reverse line takes model and field, and may start from an alias above with names. An alias is a lowercase name other than self. Rules read a followed entry as alias:section.field, its id as { uuid: alias }, and its multi Section with of: alias.

Shapes

  • document: one file is one entry. key, when given, is the other system's id for it.
  • list: one file is many entries.
    • path is where they are. Leave it out for CSV. For outbound XML, name the root element and the entry element (feed/vehicle).
    • Inbound, key is required.
    • As an http integration's reply, a list mapping takes no path.
    • Outbound, key is where an entry's key is written back, with keys_from naming the inbound mapping that recorded it.
  • set: inbound, from JSON or XML, one file into several linked types. The mapping holds only parts, and each part has an id, a model, a path and a key, its own fields and sections, and optionally unique. Exactly one part is the root, and every other part lies below its path. A part's key and fields may read a parent with ../. A rule links parts: { ref: <part>, at: <path> } for a reference, or { uuid: <part>, at: <path> } for an id as text. The parts may not link in a circle.

An http reply

An http integration may read the other system's answer back through an inbound mapping:

  • a document mapping writes the answer onto the entry the integration sent, only the fields it names, so its model is the type the integration sends;
  • a list mapping writes one new entry about the one sent. The answer is JSON, and its entry is the answer's root: the mapping takes no path, and its key is one top-level name. The integration names, with reply_about, the section.field of the new entry that holds the one sent (a reference pinned to that type), and the engine fills it, so the mapping doesn't write it.

A set can't read a reply, and a push and a build refuse a reply mapping that goes out or is a set. The rest of these rules isn't caught by register or a push: the integration lists the problem and stays switched off until it's fixed.

What register checks, and what it doesn't

register checks everything above but a reply's rules against your types, and its refusal lists the problems, where: what, one per line. Your engine checks it again when it starts and when a mapping is used.

Two kinds of problems surface only later:

  • Per entry, when a run reads the data: a value missing from a lookup, a date that doesn't match its pattern, several values where one is held. That entry fails, and the run says why.
  • A reply's other rules, when your engine starts: whether a reply mapping keeps the rest of its reply's rules. The integration lists the problem and can't be switched on until it's fixed.

Whether a mapping fits its integration's channel is checked sooner: a push and a build refuse a line whose mapping doesn't fit, or whose reply isn't an inbound document or list (Integrations).

Two complete mappings

An inbound list from CSV:

model: '@/vehicle'
direction: in
shape: list
format: { type: csv }
key: VIN
fields:
  - { listing.vin: VIN }
  - { listing.year: { from: Year, number: {} } }
  - { listing.price: { from: Price, number: { strip: '$,' } } }
  - { listing.certified: { from: CPO, lookup: { Y: true, N: false } } }
  - { listing.listed: { from: Listed, date: MM/yyyy } }
  - { listing.title: { languages: { en: TitleEN, fr: TitleFR } } }
sections:
  - section: media/photos
    path: Photos
    key: '#text'
    fields:
      - { url: { from: '#text' } }

An outbound document to JSON, sending a submission to a grader with what it needs from the quiz and its answer key:

model: '@/submission'
direction: out
shape: document
format: { type: json }
follow:
  - { quiz: { field: identity.quiz_ref } }
  - { key:  { model: '@/quiz-key', field: identity.quiz, names: quiz } }
fields:
  - { submission: { uuid: self } }
  - { quiz: 'quiz:identity.slug' }
  - { attempt: identity.attempt }
  - { answers: identity.choice_answers }
  - { passing: 'quiz:identity.passing' }
sections:
  - section: answers
    of: key
    path: key
    fields:
      - { question: question }
      - { correct: { from: correct, join: ',' } }

See also: Blueprint § Integrations · Compute Functions · Libraries § Mappings in a library