Class SchemaMapper

Nested Relationships

Class Documentation

class SchemaMapper

A mapping between the keys of two Schemas, used to copy data between them.

SchemaMapper is initialized with its input Schema, and contains member functions to add mapped or unmapped fields to the output Schema.

Public Functions

Schema const getInputSchema() const

Return the input schema (copy-on-write).

Schema const getOutputSchema() const

Return the output schema (copy-on-write).

Schema &editOutputSchema()

Return a reference to the output schema that allows it to be modified in place.

template<typename T>
Key<T> addOutputField(Field<T> const &newField, bool doReplace = false)

Add a new field to the output Schema that is not connected to the input Schema.

template<typename T>
Key<T> addMapping(Key<T> const &inputKey, bool doReplace = false)

Add a new field to the output Schema that is a copy of a field in the input Schema.

If the input Key has already been mapped, the existing output Key will be reused but the associated Field in the output Schema will be reset to a copy of the input Field.

If doReplace=True and a field with same name already exists in the output schema, that field will be mapped instead of adding a new field to the output schema. If doReplace=false and a name conflict occurs, an exception will be thrown.

template<typename T>
Key<T> addMapping(Key<T> const &inputKey, Field<T> const &outputField, bool doReplace = false)

Add a new mapped field to the output Schema with new descriptions.

If the input Key has already been mapped, the existing output Key will be reused but the associated Field will be replaced with the given one.

If doReplace=True and a field with same name already exists in the output schema, that field will be mapped instead of adding a new field to the output schema. If doReplace=false and a name conflict occurs, an exception will be thrown.

template<typename T>
Key<T> addMapping(Key<T> const &inputKey, std::string const &outputName, bool doReplace = true)

Add a new mapped field to the output Schema with a new name.

If the input Key has already been mapped, the existing output Key will be reused but the associated Field will be replaced with one with the given name.

If doReplace=True and a field with same name already exists in the output schema, that field will be mapped instead of adding a new field to the output schema. If doReplace=false and a name conflict occurs, an exception will be thrown.

template<typename T>
Key<T> addMapping(Key<T> const &inputKey, char const *outputName, bool doReplace = true)

This deliberately deleted overload ensures we don’t accidentally cast string literals to bool.

See DM-13787 for more information.

template<typename Predicate>
void addMappingsWhere(Predicate predicate, bool doReplace = true)

Add mappings for all fields that match criteria defined by a predicate.

A mapping in the output Schema will be created for each SchemaItem ‘i’ in the input Schema such that ‘predicate(i)’ is true. Note that the predicate must have a templated and/or sufficiently overloaded operator() to match all supported field types, not just those present in the input Schema.

If doReplace=True and a field with same name already exists in the output schema, that field will be mapped instead of adding a new field to the output schema. If doReplace=false and a name conflict occurs, an exception will be thrown.

void addMinimalSchema(Schema const &minimal, bool doMap = true)

Add the given minimal schema to the output schema.

This is intended to be used to ensure the output schema starts with some minimal schema. It must be called before any other fields are added to the output schema.

Parameters
  • [in] minimal: Minimal schema to be added to the beginning of the output schema.

  • [in] doMap: Whether to map minimal schema fields that are also present in the input schema.

void invert()

Swap the input and output schemas in-place.

template<typename T>
bool isMapped(Key<T> const &inputKey) const

Return true if the given input Key is mapped to an output Key.

template<typename T>
Key<T> getMapping(Key<T> const &inputKey) const

Return the output Key corresponding to the given input Key, or raise NotFoundError.

template<typename F>
void forEach(F &&func) const

Call the given functor for each key pair in the mapper.

Function objects should have a template and/or overloaded operator() that takes two Key objects with the same type:

struct Functor {
    template <typename T>
    void operator()(Key<T> const & input, Key<T> const & output) const;
};

The order of iteration is the same as the order in which mappings were added.

SchemaMapper()

Construct an empty mapper; useless unless you assign a fully-constructed one to it.

SchemaMapper(Schema const &input, Schema const &output)

Construct a mapper from the given input Schema and initial output Schema

Note that the

addMapping() methods will not connect input schema fields to existing output schema fields unless doReplace=true; instead, these will by default append new fields to the output schema. So most often you’ll want to start with an empty output schema and construct it as fields are mapped from the input schema, or be sure to always pass doReplace=true to addMapping.
Parameters
  • [in] input: The Schema that fields will be mapped from.

  • [in] output: The starting point for the Schema that fields will be mapped to (no mappings will be created automaticaly). Use addMapping() with doReplace=true to connect input fields to preexisting fields in the output schema.

SchemaMapper(Schema const &input, bool shareAliasMap = false)

Construct a mapper from the given input Schema

Note that the

addMapping() methods will not connect input schema fields to existing output schema fields unless doReplace=true; instead, these will by default append new fields to the output schema. So most often you’ll want to start with an empty output schema and construct it as fields are mapped from the input schema, or be sure to always pass doReplace=true to addMapping.
Parameters
  • [in] input: The Schema that fields will be mapped from.

  • [in] shareAliasMap: If true, install the input Schema’s AliasMap in the output Schema.

The initial (empty) output schema will have the same version as the input schema, and they will share the same AliasMap (use editOutputSchema().disconnectAliases() to use a copy of the AliasMap).

SchemaMapper(SchemaMapper const &other)

Copy construct (copy-on-write).

SchemaMapper(SchemaMapper &&other)
SchemaMapper &operator=(SchemaMapper const &other)

Assignment (copy-on-write).

SchemaMapper &operator=(SchemaMapper &&other)
~SchemaMapper()

Public Static Functions

static SchemaMapper removeMinimalSchema(Schema const &input, Schema const &minimal)

Create a mapper by removing fields from the front of a schema.

The returned mapper maps all fields in the input schema to all fields that are not in the minimal schema (compared by keys, so the overlap must appear at the beginning of the input schema).

Parameters
  • [in] input: Input schema for the mapper.

  • [in] minimal: The minimal schema the input schema starts with.

static std::vector<SchemaMapper> join(std::vector<Schema> const &inputs, std::vector<std::string> const &prefixes = std::vector<std::string>())

Combine a sequence of schemas into one, creating a SchemaMapper for each.

Each of the returned SchemaMappers has the same output schema.

Parameters
  • [in] inputs: A vector of input schemas to merge.

  • [in] prefixes: An optional vector of prefixes for the output field names, either empty or of the same size as the inputs vector.