Documentation
¶
Overview ¶
Package schemaversionmediator implements the SchemaVersionMediator plugin. It walks inbound Beckn payloads, checks schema object compatibility against the local node manifest, and dispatches translation for incompatible objects.
Index ¶
- Variables
- func ComposeExpression(entries []MappingEntry) (string, error)
- func New(ctx context.Context, loader definition.ManifestLoader, cfg map[string]string) (definition.SchemaVersionMediator, func() error, error)
- type ArtifactFetchFailure
- type MappingEntry
- type MediationError
- type PayloadRef
- type PolicyAction
- type SchemaObjectRef
- type TranslationArtifact
- type TranslationNeeded
- type TranslationPolicy
Constants ¶
This section is empty.
Variables ¶
var ErrArtifactNotFound = errors.New("schemaversionmediator: translation artifact not found")
ErrArtifactNotFound is returned by fetchArtifact when no translation artifact exists at the derived URL (HTTP 404). Distinct from transient network errors so the mediation loop can apply OnFailure policy for "map doesn't exist yet" vs "registry unreachable".
var ErrNoManifest = errors.New("schemaversionmediator: node manifest unavailable, skipping mediation")
ErrNoManifest is returned by CheckCompatibility when the node manifest is nil. The caller should log a warning and skip mediation — translation targets cannot be determined without a manifest, but the absence of one is not a hard failure.
Functions ¶
func ComposeExpression ¶
func ComposeExpression(entries []MappingEntry) (string, error)
ComposeExpression combines N schema-object-level patch expressions into a single JSONata expression evaluated at the message root via $merge.
This function is NOT called by the mediator's translation loop. The mediator executes each artifact against its own subtree independently. ComposeExpression is provided for callers that need to assemble a single composed expression for testing or non-standard evaluation outside the mediation pipeline.
An empty entries list returns the identity expression "$". The returned string can be compiled and evaluated by Execute.
func New ¶
func New(ctx context.Context, loader definition.ManifestLoader, cfg map[string]string) (definition.SchemaVersionMediator, func() error, error)
New is the package-level constructor used by the plugin entrypoint.
Types ¶
type ArtifactFetchFailure ¶
type ArtifactFetchFailure struct {
Need TranslationNeeded
URL string // artifact URL that was attempted; empty when URL derivation failed
Reason error
}
ArtifactFetchFailure records a single failed artifact fetch with the full context needed for a structured log event: which schema object was being translated, from/to what version, which URL was attempted, and why it failed.
type MappingEntry ¶
type MappingEntry struct {
JSONataPath string // from WalkPayload, e.g. "$.message.fulfillment"
Expression string // JSONata expression scoped to the schema object subtree
}
MappingEntry pairs a translation artifact expression with the payload path of the schema object it targets.
NOTE: MappingEntry and ComposeExpression are NOT used by the mediation hot path. The mediator executes each artifact expression independently against its own schema object subtree (via getAtPath/setAtPath) so that artifact authors write expressions scoped to the object, not the message root. MappingEntry and ComposeExpression are retained as tested utilities for callers that want to assemble and evaluate a composed message-root patch expression outside the normal mediation flow.
If you are writing a translation artifact: express it relative to the schema object itself (e.g. `$ ~> |$|{"discountCode": "NONE"}|`), not relative to the message root.
type MediationError ¶
type MediationError struct {
Code string
Message string
DroppedFields []string // non-nil only for schemaTranslationDataLoss
// contains filtered or unexported fields
}
MediationError is a structured rejection returned by Mediate. It carries a camelCase error code and a human-readable message so the handler can build a Beckn NACK response with the correct fault details. cause is the underlying technical error; it is available via errors.Unwrap for logging but is not exposed in the user-facing Message.
func (*MediationError) Error ¶
func (e *MediationError) Error() string
func (*MediationError) Unwrap ¶
func (e *MediationError) Unwrap() error
type PayloadRef ¶
PayloadRef is the schema identity as extracted from the wire payload: the raw @context URL and @type value found at a specific node in the payload. It is distinct from model.SchemaObject, which is the manifest declaration.
type PolicyAction ¶
type PolicyAction string
PolicyAction defines what the mediator does when schema incompatibility is detected or when a translation attempt fails.
const ( // PolicyActionReject rejects the request immediately with a NACK. PolicyActionReject PolicyAction = "reject" // PolicyActionTranslate attempts translation for each incompatible schema // object. On failure the OnFailure policy applies. PolicyActionTranslate PolicyAction = "translate" // PolicyActionPassThrough forwards the request as-is with a structured log // signal. Valid only as onFailure — used when no artifact is published yet // and the operator accepts the risk of forwarding an untranslated payload. PolicyActionPassThrough PolicyAction = "passThrough" )
type SchemaObjectRef ¶
type SchemaObjectRef struct {
PayloadRef
JSONataPath string
}
SchemaObjectRef is a PayloadRef annotated with the JSONata dot-notation path to its location in the payload (e.g. "$.message.order"). The path flows through to TranslationNeeded for logging and debugging; it is not interpreted by ComposeExpression.
func WalkPayload ¶
func WalkPayload(payload []byte) (refs []SchemaObjectRef, skipped []string, err error)
WalkPayload recursively traverses a JSON payload and returns all schema objects declared via JSON-LD "@context" and "@type" fields, each annotated with the JSONata path to its location in the tree. The walk is depth-first and collects every qualifying node regardless of nesting level, including both a parent node and its nested children when both carry "@context"/"@type" declarations — each is an independent schema contract. The payload is not modified. WalkPayload recursively traverses a JSON payload and returns all schema objects declared via JSON-LD "@context" and "@type" fields, each annotated with its JSONata path from the payload root (e.g. "$.message.offer"). It collects both a parent node and its nested children when both carry "@context"/"@type" pairs. Also returns skipped paths — nodes that have "@context" but no "@type" — so callers can log warnings for misconfigured payloads without silently ignoring them.
type TranslationArtifact ¶
TranslationArtifact holds a fetched translation artifact and the Content-Type returned by the server. ContentType determines which Translator implementation the mediation loop dispatches to (e.g. "application/jsonata").
type TranslationNeeded ¶
type TranslationNeeded struct {
From PayloadRef
To *model.SchemaObject
CanonicalVersion string
JSONataPath string
}
TranslationNeeded describes a single payload schema object that requires translation.
From is the schema identity as declared in the payload. To is the manifest entry this node supports for the same type. CanonicalVersion is the resolved translation target version from To's policy — pre-computed at CheckCompatibility time so deriveArtifactURL needs no manifest context. JSONataPath is the payload path forwarded from SchemaObjectRef.
func CheckCompatibility ¶
func CheckCompatibility(extracted []SchemaObjectRef, manifest *model.NodeManifest) ([]TranslationNeeded, error)
CheckCompatibility compares extracted schema object refs against the local node manifest and returns those that require translation. An empty result means the payload is fully compatible and the mediator can short-circuit.
Returns ErrNoManifest if manifest is nil — the caller should log a warning and skip mediation rather than treating this as a hard failure.
For each extracted SchemaObjectRef:
- Exact match in manifest → compatible, omitted from result.
- Same Type, different ContextURL → TranslationNeeded with To set to the locally supported SchemaObject (version the node expects).
- Type absent from manifest entirely → TranslationNeeded with To nil; handling is delegated to the data-loss policy enforcer.
The JSONataPath from each ref is forwarded into TranslationNeeded for the caller's logging and debugging use; it is not interpreted by ComposeExpression.
func (TranslationNeeded) ToContextURL ¶
func (t TranslationNeeded) ToContextURL() string
ToContextURL returns the canonical @context URL for the translation target: {To.BaseURL}/{CanonicalVersion}/context.jsonld.
type TranslationPolicy ¶
type TranslationPolicy struct {
Action PolicyAction
OnFailure PolicyAction
}
TranslationPolicy governs mediator behaviour when schema incompatibilities are found. It is loaded from the plugin config map and applied by Mediate.
Action is evaluated immediately after CheckCompatibility returns incompatible objects — before any translation is attempted. OnFailure is only consulted when Action is PolicyActionTranslate and the translation attempt fails (no artifact found, or execution error).