Refactor pkg/dal implementation

* Reworked errors to not brick the system (things keep track of
  issues.
* Reworked internal state management -- keeping invalid things
  present, cleanning up the code, utilizing issues.
* Cleanup/improve error messages
This commit is contained in:
Tomaž Jerman
2022-06-14 12:06:13 +02:00
parent 7bac115ceb
commit 8eb062293f
10 changed files with 1063 additions and 263 deletions
+47 -3
View File
@@ -5,8 +5,10 @@ type (
// ModelDiff defines one identified missmatch between two models
ModelDiff struct {
Type modelDiffType
Type modelDiffType
// Original will be nil when a new attribute is being added
Original *Attribute
// Asserted will be nil wen an existing attribute is being removed
Asserted *Attribute
}
@@ -14,8 +16,9 @@ type (
)
const (
AttributeMissing modelDiffType = "attributeMissing"
AttributeTypeMissmatch modelDiffType = "typeMissmatch"
AttributeMissing modelDiffType = "attributeMissing"
AttributeTypeMissmatch modelDiffType = "typeMissmatch"
AttributeSensitivityMissmatch modelDiffType = "sensitivityMissmatch"
)
// Diff calculates the diff between models a and b where a is used as base
@@ -34,6 +37,21 @@ func (a *Model) Diff(b *Model) (out ModelDiffSet) {
}
}
aIndex := make(map[string]struct {
found bool
attr *Attribute
})
for _, _attr := range a.Attributes {
attr := _attr
aIndex[attr.Ident] = struct {
found bool
attr *Attribute
}{
attr: attr,
}
}
// Deleted and update ones
for _, _attrA := range a.Attributes {
attrA := _attrA
@@ -55,6 +73,32 @@ func (a *Model) Diff(b *Model) (out ModelDiffSet) {
Asserted: attrBAux.attr,
})
}
// Other stuff
// @todo improve; for now it'll do
if attrA.SensitivityLevel != attrBAux.attr.SensitivityLevel {
out = append(out, &ModelDiff{
Type: AttributeSensitivityMissmatch,
Original: attrA,
Asserted: attrBAux.attr,
})
}
}
// New
for _, _attrB := range b.Attributes {
attrB := _attrB
// Missmatches
_, ok := aIndex[attrB.Ident]
if !ok {
out = append(out, &ModelDiff{
Type: AttributeMissing,
Original: nil,
Asserted: attrB,
})
continue
}
}
return