Skip to content

Commit

Permalink
relaxed contain rule so exports of different kinds can overlap in sub…
Browse files Browse the repository at this point in the history
…jects (#25)
  • Loading branch information
aricart committed Dec 7, 2018
1 parent f26fb76 commit 7d84585
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
29 changes: 20 additions & 9 deletions exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@ func (e *Exports) Add(i ...*Export) {
*e = append(*e, i...)
}

// Validate calls validate on all of the exports
func (e *Exports) Validate(vr *ValidationResults) error {
var subjects []Subject
for _, v := range *e {
subjects = append(subjects, v.Subject)
v.Validate(vr)
}
// collect all the subjects, and validate that no subject is a subset
func isContainedIn(kind ExportType, subjects []Subject, vr *ValidationResults) {
m := make(map[string]string)
for i, ns := range subjects {
for j, s := range subjects {
Expand All @@ -73,10 +66,28 @@ func (e *Exports) Validate(vr *ValidationResults) error {
for k, v := range m {
var vi ValidationIssue
vi.Blocking = true
vi.Description = fmt.Sprintf("export subject %q already exports %q", k, v)
vi.Description = fmt.Sprintf("%s export subject %q already exports %q", kind, k, v)
vr.Add(&vi)
}
}
}

// Validate calls validate on all of the exports
func (e *Exports) Validate(vr *ValidationResults) error {
var serviceSubjects []Subject
var streamSubjects []Subject

for _, v := range *e {
if v.IsService() {
serviceSubjects = append(serviceSubjects, v.Subject)
} else {
streamSubjects = append(streamSubjects, v.Subject)
}
v.Validate(vr)
}

isContainedIn(Service, serviceSubjects, vr)
isContainedIn(Stream, streamSubjects, vr)

return nil
}
Expand Down
47 changes: 46 additions & 1 deletion exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestExportsValidation(t *testing.T) {
}

if exports.HasExportContainingSubject("bar.*") {
t.Errorf("Export list does not has the subject, and should say so")
t.Errorf("Export list does not have the subject, and should say so")
}
}

Expand All @@ -98,3 +98,48 @@ func TestOverlappingExports(t *testing.T) {
t.Errorf("export has overlapping subjects")
}
}

func TestDifferentExportTypes_OverlapOK(t *testing.T) {
i := &Export{Subject: "bar.foo", Type: Service}
i2 := &Export{Subject: "bar.*", Type: Stream}

exports := &Exports{}
exports.Add(i, i2)

vr := CreateValidationResults()
exports.Validate(vr)

if len(vr.Issues) != 0 {
t.Errorf("should allow overlaps on different export kind")
}
}

func TestDifferentExportTypes_SameSubjectOK(t *testing.T) {
i := &Export{Subject: "bar", Type: Service}
i2 := &Export{Subject: "bar", Type: Stream}

exports := &Exports{}
exports.Add(i, i2)

vr := CreateValidationResults()
exports.Validate(vr)

if len(vr.Issues) != 0 {
t.Errorf("should allow overlaps on different export kind")
}
}

func TestSameExportType_SameSubject(t *testing.T) {
i := &Export{Subject: "bar", Type: Service}
i2 := &Export{Subject: "bar", Type: Service}

exports := &Exports{}
exports.Add(i, i2)

vr := CreateValidationResults()
exports.Validate(vr)

if len(vr.Issues) != 1 {
t.Errorf("should not allow same subject on same export kind")
}
}

0 comments on commit 7d84585

Please sign in to comment.