Files
ConfigKit/sequence_identity.go

76 lines
2.8 KiB
Go

package configkit
import "gitea.maximumdirect.net/eric/ConfigKit/internal/engine"
// SequenceSelector identifies a sequence destination through literal mapping
// keys and arbitrary mapping-key segments. A zero selector identifies the root.
type SequenceSelector []SequenceSelectorSegment
// SequenceSelectorSegment is one literal mapping key or wildcard mapping key.
// Its zero value is the literal empty key.
type SequenceSelectorSegment struct {
key string
anyMappingKey bool
}
// SequenceLiteralKey selects a literal mapping key, including an empty key or
// the literal string "*".
func SequenceLiteralKey(key string) SequenceSelectorSegment {
return SequenceSelectorSegment{key: key}
}
// SequenceAnyMappingKey selects any single mapping key.
func SequenceAnyMappingKey() SequenceSelectorSegment {
return SequenceSelectorSegment{anyMappingKey: true}
}
// SequenceIdentity records the identity fields used for strict sequence
// composition, identified-record parent lookup, and final typed-result
// validation during loading.
type SequenceIdentity struct {
exactPath bool
path Path
selector SequenceSelector
identityFields []string
}
// SequenceIdentityAt registers an exact mapping path for strict-composition,
// inheritance, and final typed-result identity checks. The path and fields are
// copied; invalid paths and empty field sets are reported during load validation.
func SequenceIdentityAt(path Path, fields ...string) SequenceIdentity {
return SequenceIdentity{
exactPath: true,
path: path.clone(),
identityFields: append([]string(nil), fields...),
}
}
// SequenceIdentityMatching registers identity fields at destinations matched by
// selector for strict composition, inheritance, and final typed-result checks.
// Literal and wildcard selector segments are copied. Empty field sets are
// reported during load validation.
func SequenceIdentityMatching(selector SequenceSelector, fields ...string) SequenceIdentity {
return SequenceIdentity{
selector: append(SequenceSelector(nil), selector...),
identityFields: append([]string(nil), fields...),
}
}
func toEngineSequenceIdentity(identity SequenceIdentity) engine.SequenceIdentityRegistrationInput {
registration := engine.SequenceIdentityRegistrationInput{
IdentityFields: append([]string(nil), identity.identityFields...),
}
if identity.exactPath {
path := toEnginePath(identity.path.clone())
registration.ExactPath = &path
return registration
}
registration.Selector = make([]engine.SequenceIdentitySelectorSegment, len(identity.selector))
for index, segment := range identity.selector {
registration.Selector[index] = engine.SequenceIdentitySelectorSegment{
Key: segment.key, AnyMappingKey: segment.anyMappingKey,
}
}
return registration
}