IriMapping.mapper constructor

const IriMapping.mapper(
  1. Type mapperType
)

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 and it must have a no-argument default constructor.

It will only be used for the Resource Mapper whose property is annotated with this mapping, not automatically be registered globally.

This approach is useful when the mapper has a default constructor and doesn't require additional configuration parameters.

Example:

class Book {
  // Using a custom mapper for an ISBN object
  @RdfProperty(
    Dcterms.source,
    iri: IriMapping.mapper(IsbnMapper)
  )
  final ISBN isbn;
}

// The mapper implementation must be accessible to the generator:
class IsbnMapper implements IriTermMapper<ISBN> {
  @override
  IriTerm toRdfTerm(ISBN isbn, SerializationContext context) {
    return context.createIriTerm('urn:isbn:${isbn.value}');
  }

  @override
  ISBN fromRdfTerm(IriTerm term, DeserializationContext context) {
    final iri = term.value;
    if (!iri.startsWith('urn:isbn:')) {
      throw ArgumentError('Invalid ISBN IRI: $iri');
    }
    return ISBN(iri.substring(9));
  }
}

Implementation

const IriMapping.mapper(Type mapperType)
    : template = null,
      fragmentTemplate = null,
      super.mapper(mapperType);