diff --git a/dns-sd.go b/dns-sd.go index 07ba360..d77c798 100644 --- a/dns-sd.go +++ b/dns-sd.go @@ -78,7 +78,7 @@ const ( // Format: // .. // _sub.... -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)) @@ -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 { @@ -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 } diff --git a/dns-sd_test.go b/dns-sd_test.go index cd2f986..d27c3fc 100644 --- a/dns-sd_test.go +++ b/dns-sd_test.go @@ -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", @@ -36,7 +36,7 @@ func TestQueryName(t *testing.T) { } func TestParseTypePath(t *testing.T) { - expect := &Type{ + expect := Type{ Name: "_chat._tcp", Domain: "local", } @@ -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", diff --git a/options.go b/options.go index aca0f84..59bbf66 100644 --- a/options.go +++ b/options.go @@ -8,7 +8,7 @@ import ( ) type browser struct { - types []*Type + types []Type *cache } @@ -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), diff --git a/service.go b/service.go index 7b4519e..7337886 100644 --- a/service.go +++ b/service.go @@ -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:], } @@ -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, ",") @@ -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 } @@ -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"` @@ -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, @@ -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 } @@ -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 }