55 lines
1.8 KiB
Go
55 lines
1.8 KiB
Go
package engine
|
|
|
|
type sequenceRegistration struct {
|
|
path Path
|
|
identityField string
|
|
}
|
|
|
|
type sequencePolicy struct {
|
|
identityFields map[string]string
|
|
}
|
|
|
|
// DefinitionWithSequenceRegistrations attaches facade-owned registrations to a
|
|
// detached engine definition without exposing the registration representation.
|
|
func DefinitionWithSequenceRegistrations[T any](definition Definition[T], paths []Path, identityFields []string) Definition[T] {
|
|
if paths == nil {
|
|
definition.sequenceRegistrations = nil
|
|
return definition
|
|
}
|
|
definition.sequenceRegistrations = make([]sequenceRegistration, len(paths))
|
|
for index := range paths {
|
|
definition.sequenceRegistrations[index] = sequenceRegistration{
|
|
path: paths[index].clone(), identityField: identityFields[index],
|
|
}
|
|
}
|
|
return definition
|
|
}
|
|
|
|
func compileSequencePolicy(registrations []sequenceRegistration) (sequencePolicy, []Diagnostic) {
|
|
policy := sequencePolicy{identityFields: make(map[string]string, len(registrations))}
|
|
var diagnostics []Diagnostic
|
|
for _, registration := range registrations {
|
|
path, err := normalizeDestination(registration.path)
|
|
if err != nil {
|
|
diagnostics = append(diagnostics, Diagnostic{
|
|
Code: CodeOptionsInvalid, Severity: SeverityError,
|
|
Path: registration.path.clone(), Message: "sequence registration path must contain only non-reserved mapping keys",
|
|
})
|
|
continue
|
|
}
|
|
|
|
key := path.String()
|
|
if current, ok := policy.identityFields[key]; ok {
|
|
if current != registration.identityField {
|
|
diagnostics = append(diagnostics, Diagnostic{
|
|
Code: CodeOptionsInvalid, Severity: SeverityError,
|
|
Path: path, Message: "sequence registrations at one path must use the same identity field",
|
|
})
|
|
}
|
|
continue
|
|
}
|
|
policy.identityFields[key] = registration.identityField
|
|
}
|
|
return policy, canonicalDiagnostics(diagnostics)
|
|
}
|