IriStrategy.mapper constructor

const IriStrategy.mapper(
  1. Type mapperType, {
  2. String? providedAs,
})

Creates a reference to a mapper that will be instantiated from the given type.

The generator will create an instance of mapperType to handle IRI mapping for this class. The type must implement IriTermMapper for a record type composed of the @RdfIriPart property values.

Note that - unlike similar constructors like RdfIri.namedMapper - the mapper will not be registered globally, it will only be used for the class annotated with @RdfGlobalResource.

When implementing the mapper for multiple IRI parts, use @RdfIriPart.position(index) to define the position of each property in the record that will be passed to your mapper.

Example:

@RdfGlobalResource(Product.classIri, IriStrategy.mapper(ProductIdMapper))
class Product {
  @RdfIriPart.position(1) // First field in record
  final String category;

  @RdfIriPart.position(2) // Second field in record
  final String id;
  // ...
}

// Mapper must work with the record type defined by the RdfIriPart positions:
class ProductIdMapper implements IriTermMapper<(String, String)> {
  @override
  IriTerm toRdfTerm((String, String) record, SerializationContext context) {
    final (category, id) = record;
    return context.createIriTerm('https://example.org/products/$category/$id');
  }

  @override
  (String, String) fromRdfTerm(IriTerm term, DeserializationContext context) {
    final parts = term.value.split('/').takeLast(2).toList();
    return (parts[0], parts[1]);
  }
}

The optional providedAs parameter allows this resource's IRI to be provided to dependent mappers under the specified name, enabling hierarchical IRI patterns.

Implementation

const IriStrategy.mapper(Type mapperType, {this.providedAs})
    : template = null,
      fragmentTemplate = null,
      super.mapper(mapperType);