conceptmap

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

README

haistack-conceptmap (pkg/conceptmap)

FHIR ConceptMap resolution and coding translation for StructureMap translate() and related tooling.

Library only — not a standalone terminology server.


What it does

Translator (translate.go):

type TranslateRequest struct {
    MapCanonical string
    Source       map[string]any // Coding-shaped map
    TargetSystem string         // optional group filter
}

Flow:

  1. Translate / TranslateResolved resolve Map via Resolver
  2. translateLocal matches groups (Source, Target, element.Code)
  3. Apply targets; filter by equivalence; handle NoMap and Unmapped modes
  4. If local not-found and Remote set → RemoteTranslateClient.Translate

Resolvers (resolver.go, chain.go):

Implementation Use
StoreResolver ConceptMap resources + DefinitionStore fallback, versioned canonicals
StaticResolver Tests and fixtures
ChainResolver Ordered fallback

Error helpers: IsNotFound, IsNoMap, IsNoTranslation.

Parsing: ParseMap, ResolveEnvelope, Canonical(Map).

Does not replace pkg/terminology for ValueSet expansion or validation bindings.


How it fits in the ecosystem

ConceptMap (FHIR in store)
        │
        ▼
Resolver ──► Map ──► Translator.Translate ──► []map[string]any (codings)
        │                      │
        │                      └──► structuremap.Engine (translate transform)
        └── optional RemoteTranslateClient

Runtime attaches a store-backed Translator to StructureMap extraction (wire.go).


When to use it

  • Test or implement StructureMap translate
  • Deterministic coding maps without hitting TX on every line
  • Custom StructureMap runners

Use pkg/terminology for $expand, $validate-code, CodeSystem lookup at validation time.

Most apps import this only indirectly through pkg/structuremap.


Usage modes

1. StructureMap engine (primary)
engine := structuremap.Engine{
    Translator: conceptmap.Translator{
        Resolver: &conceptmap.StoreResolver{Resources: rs, Registry: defs},
        Remote:   remoteClient, // optional
    },
}
2. Direct translation (tests)

From translate_test.go:

m := conceptmap.Map{
    URL: "http://example.org/maps/gender",
    Group: []conceptmap.Group{{
        Source: "http://example.org/source",
        Target: "http://example.org/target",
        Element: []conceptmap.Element{{
            Code: "M",
            Target: []conceptmap.Target{{Code: "male", Equivalence: "equivalent"}},
        }},
    }},
}
tr := conceptmap.Translator{Resolver: conceptmap.StaticResolver{m.URL: m}}
codings, err := tr.Translate(ctx, conceptmap.TranslateRequest{
    MapCanonical: m.URL,
    Source: map[string]any{"system": "http://example.org/source", "code": "M"},
})

TranslateResolved returns the Map used for provenance.

3. Store-backed production resolver

Install ConceptMap FHIR resources in the repository. StoreResolver lists ConceptMap ids on each resolve so new maps appear without restart.

4. Chain resolvers
conceptmap.ChainResolver{Resolvers: []conceptmap.Resolver{static, store}}
5. Remote fallback

Implement RemoteTranslateClient — see remote_translate.go. Local success never calls remote.


Examples (APIs from this repo)

No-map (TestTranslatorRejectsNoMap): NoMap: true → IsNoMap(err).

Unmapped (translate_test.go):

  • Mode: "provided" — pass source code/system
  • Mode: "use-source-code" — code with group target system
  • Mode: "fixed" — constant target

StructureMap (structuremap/transforms_test.go):

engine.applyTransform(ctx, "translate", []Parameter{{ValueString: m.URL}}, nil,
    map[string]any{"system": "...", "code": "F"})

Two-parameter translate — TestTranslateTransformTwoParameterForm.

Deduping — multiple targets dedupe by system/code/display.


Where it fits

ConceptMap ──► pkg/conceptmap ──► translate() in pkg/structuremap

Same ConceptMap resources may also feed terminology projections elsewhere.


Limits

  • Executes declared mappings only — no clinical inference
  • Remote optional; resolver required for local-first setups
  • Not TX server — no closure, subsumption, or expand
  • Store list cap 10k ConceptMap ids per index build
  • Disjoint/unmatched equivalences skipped; empty matches → IsNoTranslation
  • Ambiguous version duplicates → error from resolveFromIndex

Documentation

Overview

Package conceptmap resolves FHIR ConceptMap resources and translates source codings through map groups for StructureMap translate transforms.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Canonical

func Canonical(m Map) string

func ErrNotFound

func ErrNotFound(canonical string) error

ErrNotFound reports a missing ConceptMap canonical URL.

func IsNoMap

func IsNoMap(err error) bool

IsNoMap reports whether err is a ConceptMap no-map element.

func IsNoTranslation

func IsNoTranslation(err error) bool

IsNoTranslation reports whether err is a missing acceptable translation.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err indicates the ConceptMap canonical URL was not found.

Types

type ChainResolver

type ChainResolver struct {
	Resolvers []Resolver
}

ChainResolver tries resolvers in order until one succeeds.

func (ChainResolver) Resolve

func (c ChainResolver) Resolve(ctx context.Context, canonical string) (Map, error)

type Element

type Element struct {
	Code    string   `json:"code,omitempty"`
	Display string   `json:"display,omitempty"`
	NoMap   bool     `json:"noMap,omitempty"`
	Target  []Target `json:"target,omitempty"`
}

type Group

type Group struct {
	Source   string    `json:"source,omitempty"`
	Target   string    `json:"target,omitempty"`
	Element  []Element `json:"element,omitempty"`
	Unmapped *Unmapped `json:"unmapped,omitempty"`
}

type Map

type Map struct {
	ResourceType string  `json:"resourceType"`
	URL          string  `json:"url"`
	Version      string  `json:"version,omitempty"`
	Status       string  `json:"status,omitempty"`
	Name         string  `json:"name,omitempty"`
	SourceURI    string  `json:"sourceUri,omitempty"`
	Group        []Group `json:"group,omitempty"`
}

Map is the JSON representation of a FHIR ConceptMap resource.

func ParseMap

func ParseMap(raw []byte) (Map, error)

func ResolveEnvelope

func ResolveEnvelope(env *types.ResourceEnvelope) (Map, error)

ResolveEnvelope parses a ConceptMap envelope.

type RemoteHTTPClient

type RemoteHTTPClient struct {
	BaseURL    string
	HTTPClient *http.Client
	Headers    map[string]string
	Authorize  func(*http.Request) error
}

RemoteHTTPClient calls a FHIR server's ConceptMap/$translate operation.

func (RemoteHTTPClient) Translate

func (c RemoteHTTPClient) Translate(ctx context.Context, req TranslateRequest) ([]map[string]any, error)

Translate performs a remote ConceptMap/$translate request.

type RemoteTranslateClient

type RemoteTranslateClient interface {
	Translate(ctx context.Context, req TranslateRequest) ([]map[string]any, error)
}

RemoteTranslateClient performs ConceptMap translation against an external terminology server.

type Resolver

type Resolver interface {
	Resolve(ctx context.Context, canonical string) (Map, error)
}

Resolver loads ConceptMap resources by canonical URL.

type StaticResolver

type StaticResolver map[string]Map

StaticResolver resolves maps from an in-memory table.

func (StaticResolver) Resolve

func (r StaticResolver) Resolve(_ context.Context, canonical string) (Map, error)

type StoreResolver

type StoreResolver struct {
	Resources store.ResourceStore
	Registry  store.DefinitionStore
	// contains filtered or unexported fields
}

StoreResolver resolves ConceptMaps from ResourceStore with DefinitionStore fallback.

func (*StoreResolver) Resolve

func (r *StoreResolver) Resolve(ctx context.Context, canonical string) (Map, error)

type Target

type Target struct {
	Code         string `json:"code,omitempty"`
	Display      string `json:"display,omitempty"`
	Equivalence  string `json:"equivalence,omitempty"`
	Relationship string `json:"relationship,omitempty"`
}

type TerminologyStoreResolver

type TerminologyStoreResolver struct {
	Store   store.TerminologyStore
	ScopeID string
}

TerminologyStoreResolver resolves ConceptMaps from a terminology store projection.

func (*TerminologyStoreResolver) Resolve

func (r *TerminologyStoreResolver) Resolve(ctx context.Context, canonical string) (Map, error)

type TranslateRequest

type TranslateRequest struct {
	MapCanonical string
	Source       map[string]any
	TargetSystem string
}

TranslateRequest identifies a source coding and ConceptMap to apply.

type Translator

type Translator struct {
	Resolver Resolver
	Remote   RemoteTranslateClient
}

Translator translates source codings using ConceptMap resources.

func (Translator) Translate

func (t Translator) Translate(ctx context.Context, req TranslateRequest) ([]map[string]any, error)

Translate returns target codings for a source coding.

func (Translator) TranslateResolved

func (t Translator) TranslateResolved(ctx context.Context, req TranslateRequest) ([]map[string]any, Map, error)

TranslateResolved is Translate plus the ConceptMap that was resolved for provenance.

type Unmapped

type Unmapped struct {
	Mode    string `json:"mode,omitempty"`
	Code    string `json:"code,omitempty"`
	Display string `json:"display,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL