Skip to content

Commit

Permalink
Add AttributeCollection type.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Nov 7, 2023
1 parent 7605d03 commit efefb65
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 29 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ The format is based on [Keep a Changelog], and this project adheres to
[keep a changelog]: https://keepachangelog.com/en/1.0.0/
[semantic versioning]: https://semver.org/spec/v2.0.0.html

## [Unreleased]

### Added

- Added `dnssd.AttributeCollection` type

### Changed

- **[BC]** Changed `dnssd.ServiceInstance.Attributes` type from `[]Attributes` to `AttributeCollection`

### Removed

- **[BC]** Removed `dnssd.AttributeCollectionsEqual()` function

## [0.3.1] - 2023-08-15

### Changed
Expand Down
18 changes: 11 additions & 7 deletions dnssd/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,22 @@ func normalizeAttributeKey(k string) (string, error) {
return w.String(), nil
}

// AttributeCollectionsEqual returns true if lhs and rhs contain the same sets
// of attributes, in any order.
func AttributeCollectionsEqual(lhs, rhs []Attributes) bool {
if len(lhs) != len(rhs) {
// AttributeCollection is a collection of [Attributes]. Each entry in the slice
// contains the attributes conveyed in a separate TXT record.
type AttributeCollection []Attributes

// Equal returns true if c and x contain the same sets of attributes, in any
// order.
func (c AttributeCollection) Equal(x AttributeCollection) bool {
if len(c) != len(x) {
return false
}

visited := make([]bool, len(rhs))
visited := make([]bool, len(x))

left:
for _, l := range lhs {
for i, r := range rhs {
for _, l := range c {
for i, r := range x {
if visited[i] {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions dnssd/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type ServiceInstance struct {
//
// An empty slice is a valid representation of an instance with a single
// empty TXT record.
Attributes []Attributes
Attributes AttributeCollection

// TTL is the time-to-live of the instance's DNS records.
TTL time.Duration
Expand All @@ -65,6 +65,6 @@ func (i ServiceInstance) Equal(inst ServiceInstance) bool {
i.TargetPort == inst.TargetPort &&
i.Priority == inst.Priority &&
i.Weight == inst.Weight &&
AttributeCollectionsEqual(i.Attributes, inst.Attributes) &&
i.Attributes.Equal(inst.Attributes) &&
i.TTL == inst.TTL
}
28 changes: 14 additions & 14 deletions dnssd/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var _ = Describe("type ServiceInstance", func() {
TargetPort: 80,
Priority: 10,
Weight: 20,
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("txtvers", []byte{1}).
WithFlag("default"),
Expand All @@ -49,7 +49,7 @@ var _ = Describe("type ServiceInstance", func() {
TargetPort: 80,
Priority: 10,
Weight: 20,
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("txtvers", []byte{1}).
WithFlag("default"),
Expand All @@ -60,7 +60,7 @@ var _ = Describe("type ServiceInstance", func() {
Entry(
"attributes in a different order",
ServiceInstance{
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("txtvers", []byte{1}).
WithFlag("default"),
Expand All @@ -69,7 +69,7 @@ var _ = Describe("type ServiceInstance", func() {
},
},
ServiceInstance{
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("txtvers", []byte{2}),
NewAttributes().
Expand All @@ -81,15 +81,15 @@ var _ = Describe("type ServiceInstance", func() {
Entry(
"multiple copies of the same set of attributes",
ServiceInstance{
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("txtvers", []byte{1}),
NewAttributes().
WithPair("txtvers", []byte{1}),
},
},
ServiceInstance{
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("txtvers", []byte{1}),
NewAttributes().
Expand Down Expand Up @@ -170,13 +170,13 @@ var _ = Describe("type ServiceInstance", func() {
Entry(
"different attributes",
ServiceInstance{
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("txtvers", []byte{1}),
},
},
ServiceInstance{
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("txtvers", []byte{2}),
},
Expand All @@ -185,13 +185,13 @@ var _ = Describe("type ServiceInstance", func() {
Entry(
"different attributes - flag vs empty value",
ServiceInstance{
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("flag", nil),
},
},
ServiceInstance{
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithFlag("flag"),
},
Expand All @@ -200,13 +200,13 @@ var _ = Describe("type ServiceInstance", func() {
Entry(
"different attributes - subset",
ServiceInstance{
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("txtvers", []byte{1}),
},
},
ServiceInstance{
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("txtvers", []byte{1}), // <-- same
NewAttributes().
Expand All @@ -217,15 +217,15 @@ var _ = Describe("type ServiceInstance", func() {
Entry(
"different attributes - multiple copies of the same set of attributes",
ServiceInstance{
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("txtvers", []byte{1}), // <-- same
NewAttributes().
WithPair("txtvers", []byte{1}), // <-- same
},
},
ServiceInstance{
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("txtvers", []byte{1}), // <-- same
NewAttributes().
Expand Down
4 changes: 2 additions & 2 deletions dnssd/records_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var _ = Context("DNS records", func() {
TargetPort: 12345,
Priority: 10,
Weight: 20,
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("<key>", []byte("<value>")),
},
Expand Down Expand Up @@ -220,7 +220,7 @@ var _ = Context("DNS records", func() {
})

It("returns a single empty record if there are only empty attribute collections", func() {
instance.Attributes = []Attributes{{}, {}}
instance.Attributes = AttributeCollection{{}, {}}
rec := NewTXTRecords(instance)

Expect(rec).To(ConsistOf(
Expand Down
4 changes: 2 additions & 2 deletions dnssd/unicastresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var _ = Context("UnicastResolver", func() {
TargetPort: 12345,
Priority: 10,
Weight: 20,
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("<key>", []byte("<instance-a>")),
},
Expand All @@ -51,7 +51,7 @@ var _ = Context("UnicastResolver", func() {
TargetPort: 12345,
Priority: 10,
Weight: 20,
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("<key>", []byte("<instance-b0>")),
NewAttributes().
Expand Down
4 changes: 2 additions & 2 deletions dnssd/unicastserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var _ = Context("UnicastServer", func() {
TargetPort: 12345,
Priority: 10,
Weight: 20,
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("<key>", []byte("<instance-a>")),
},
Expand All @@ -48,7 +48,7 @@ var _ = Context("UnicastServer", func() {
TargetPort: 12345,
Priority: 10,
Weight: 20,
Attributes: []Attributes{
Attributes: AttributeCollection{
NewAttributes().
WithPair("<key>", []byte("<instance-b0>")),
NewAttributes().
Expand Down

0 comments on commit efefb65

Please sign in to comment.