Skip to content

Commit

Permalink
Fix: Change Type to be a value type
Browse files Browse the repository at this point in the history
  • Loading branch information
betamos committed Aug 31, 2023
1 parent 5c20ede commit 3aa9678
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 21 deletions.
6 changes: 3 additions & 3 deletions dns-sd.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const (
// Format:
// <type>.<domain>.
// _sub.<subtype>.<type>.<domain>.
func responderNames(ty *Type) (types []string) {
func responderNames(ty Type) (types []string) {
types = append(types, fmt.Sprintf("%s.%s.", ty.Name, ty.Domain))
for _, sub := range ty.Subtypes {
types = append(types, fmt.Sprintf("%s._sub.%s.%s.", sub, ty.Name, ty.Domain))
Expand All @@ -87,7 +87,7 @@ func responderNames(ty *Type) (types []string) {
}

// Returns the query DNS name to use in e.g. a PTR query.
func queryName(ty *Type) (str string) {
func queryName(ty Type) (str string) {
if len(ty.Subtypes) > 0 {
return fmt.Sprintf("%s._sub.%s.%s.", ty.Subtypes[0], ty.Name, ty.Domain)
} else {
Expand Down Expand Up @@ -117,7 +117,7 @@ func parseServicePath(s string) (svc *Service, err error) {
name := unescapeDns(parts[0])
typeName := fmt.Sprintf("%s.%s", parts[1], parts[2])
domain := strings.Join(parts[3:], ".")
ty := &Type{typeName, nil, domain}
ty := Type{typeName, nil, domain}
if err := ty.Validate(); err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions dns-sd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestParseServicePath(t *testing.T) {

// TODO: Rename to typePath?
func TestQueryName(t *testing.T) {
ty := &Type{
ty := Type{
Name: "_chat._tcp",
Subtypes: []string{"_printer"},
Domain: "local",
Expand All @@ -36,7 +36,7 @@ func TestQueryName(t *testing.T) {
}

func TestParseTypePath(t *testing.T) {
expect := &Type{
expect := Type{
Name: "_chat._tcp",
Domain: "local",
}
Expand All @@ -53,7 +53,7 @@ func TestParseTypePath(t *testing.T) {
}

func TestParseTypePathSubtype(t *testing.T) {
expect := &Type{
expect := Type{
Name: "_chat._tcp",
Subtypes: []string{"_emoji"},
Domain: "local",
Expand Down
4 changes: 2 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type browser struct {
types []*Type
types []Type
*cache
}

Expand Down Expand Up @@ -67,7 +67,7 @@ func (o *Options) Publish(svc *Service) *Options {
// services are ignored.
//
// A type may have at most one subtype, in order to narrow the search.
func (o *Options) Browse(cb func(Event), types ...*Type) *Options {
func (o *Options) Browse(cb func(Event), types ...Type) *Options {
o.browser = &browser{
types: types,
cache: newCache(cb),
Expand Down
20 changes: 7 additions & 13 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ type Type struct {
// separated list of subtypes can be added at the end. Here is a full example:
//
// `_my-service._tcp.custom.domain,_printer,_sub1,_sub2`
func NewType(typeStr string) *Type {
func NewType(typeStr string) Type {
typeParts := strings.Split(typeStr, ",")
ty := &Type{
ty := Type{
Name: typeParts[0],
Subtypes: typeParts[1:],
}
Expand All @@ -51,7 +51,7 @@ func NewType(typeStr string) *Type {
}

// Returns the type, domain and any subtypes, e.g. `_chat._tcp.local,_emoji`.
func (t *Type) String() string {
func (t Type) String() string {
var sub string
if len(t.Subtypes) > 0 {
sub = "," + strings.Join(t.Subtypes, ",")
Expand All @@ -60,10 +60,7 @@ func (t *Type) String() string {
}

// Returns true if the types are equal (excluding subtypes)
func (t *Type) Equal(o *Type) bool {
if t == o {
return true
}
func (t Type) Equal(o Type) bool {
return t.Name == o.Name && t.Domain == o.Domain
}

Expand Down Expand Up @@ -96,7 +93,7 @@ func (t *Type) Validate() error {
// A service reachable on the local network.
type Service struct {
// The service type
Type *Type
Type Type

// A name that uniquely identifies a service of a given type, e.g. `Office Printer 32`.
Name string `json:"name"`
Expand All @@ -120,7 +117,7 @@ type Service struct {

// Create a new service for publishing. The hostname is generated based on `os.Hostname()`.
// Choose a unique name to avoid conflicts with other services of the same type.
func NewService(ty *Type, name string, port uint16) *Service {
func NewService(ty Type, name string, port uint16) *Service {
osHostname, _ := os.Hostname()
return &Service{
Type: ty,
Expand All @@ -137,9 +134,6 @@ func (s *Service) String() string {
}

func (s *Service) Validate() error {
if s.Type == nil {
return errors.New("no type specified")
}
if err := s.Type.Validate(); err != nil {
return err
}
Expand Down Expand Up @@ -177,7 +171,7 @@ func (s *Service) deepEqual(o *Service) bool {
}

// Returns true if this service matches the provided query type (including subtype, if present).
func (s *Service) Matches(q *Type) bool {
func (s *Service) Matches(q Type) bool {
if !q.Equal(s.Type) {
return false // Main types are not equal
}
Expand Down

0 comments on commit 3aa9678

Please sign in to comment.