Skip to content

Add segment mappers to translate content segment aliases - #1312

Merged
KevinJump merged 1 commit into
v17/mainfrom
feature/segment-mapper
Nov 30, 2026
Merged

KevinJump merged 1 commit into
v17/mainfrom
feature/segment-mapper

Conversation

@KevinJump

Copy link
Copy Markdown
Member

Some packages name content segments after values that differ between sites. Umbraco Engage names each A/B test variant's segment engage_ab-testing_{variantId}, where the id is a database id. uSync writes segment aliases to files as they are, so they point at the wrong variant on any other site.

This adds an extension point so a package can translate those aliases when content is serialized and deserialized.

The interface

public interface ISyncSegmentMapper
{
    bool IsMapper(string segment);                      // called with site and portable aliases
    Task<string> GetExportSegmentAsync(string segment); // site alias -> alias in the file
    Task<string> GetImportSegmentAsync(string segment); // alias in the file -> site alias (unchanged if unresolved)
}

Register one in a composer:

builder.WithCollectionBuilder<SyncSegmentMapperCollectionBuilder>()
    .Add<MySegmentMapper>();

The first mapper whose IsMapper returns true handles a segment. Blank segments never reach a mapper, and a mapper that returns a blank value keeps the original segment, so a segment value can't turn into the default value.

Where it's used

ContentSerializerBase (content, media, blueprints):

  • Export: the Segment attribute on each Value is mapped. The segments filter from options is still checked against the site alias, because the setting holds this site's aliases.
  • Import: the Segment attribute is mapped before the value is read from the item, compared and set. Value mappers get the site alias in both directions.
  • Left alone: the Segments attribute on the root node. It holds the requested segment names from options, not stored values, and nothing reads it on import. The blank-value fill-in for missing cultures writes no segment, so it's untouched too.

Reports compare the serialized XML of the current item with the file, so both sides go through the mapper and stay comparable. The content tracker works on the same XML.

Because this is in the serializer, uSync.Complete push and pull pick it up too.

Compatibility

The collection is empty by default and registered in AdduSyncCore. With no mapper, GetExportSegmentAsync/GetImportSegmentAsync return the input, so export files are byte-identical to before.

ContentSerializerBase, ContentSerializer, MediaSerializer and ContentTemplateSerializer each get a constructor that takes SyncSegmentMapperCollection. The existing constructors are marked [Obsolete] (removal in v19) and pass SyncSegmentMapperCollection.Empty.

Tests

ContentSegmentMapperTests registers a fake mapper (test_1 ↔ test_key) and checks export, import (including an unmapped segment and the default value) and a no-change report. SyncSegmentMapperCollectionTests covers the empty collection, round trips, unhandled segments and blank results. Full suite: 386 passed.

Found along the way (not changed here)

Property values are sorted by culture only, so values for different segments come out in whatever order the item holds them. An item that hasn't been reloaded since saving can list segments in a different order from the database, and two sites can hold them in different orders, which shows as a change in reports. Sorting by segment as well would fix that, but it changes the order in existing files, so it belongs in its own PR.

🤖 Generated with Claude Code

Packages can register an ISyncSegmentMapper to turn site-specific
segment aliases (e.g. ones named after database ids) into portable
ones in uSync files, and back again on import. The content and media
serializers use the new SyncSegmentMapperCollection, which is empty by
default, so nothing changes unless a mapper is registered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release/17.6.4 uSync 17.6.4 release

Development

Successfully merging this pull request may close these issues.

1 participant