Skip to content

Commit

Permalink
Add a combined Datapoint interface
Browse files Browse the repository at this point in the history
This allows callers to use all the methods present on the types, not just
`Pack` and `Unpack`.
  • Loading branch information
annismckenzie committed Jan 7, 2024
1 parent ad252e3 commit 7399706
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 6 additions & 0 deletions knx/dpt/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ type DatapointMeta interface {
// fmt.Stringer provides a string representation of the datapoint.
fmt.Stringer
}

// Datapoint represents a datapoint with both its value and metadata.
type Datapoint interface {
DatapointValue
DatapointMeta
}
6 changes: 3 additions & 3 deletions knx/dpt/types_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var (
types = [...]DatapointValue{
types = [...]Datapoint{
// 1.xxx
new(DPT_1001),
new(DPT_1002),
Expand Down Expand Up @@ -248,14 +248,14 @@ func ListSupportedTypes() []string {
}

// Produce creates a new instance of the given datapoint-type name e.g. "1.001".
func Produce(name string) (d DatapointValue, ok bool) {
func Produce(name string) (d Datapoint, ok bool) {
// Setup the registry
setup()

// Lookup the given type and create a new instance of that type
x, ok := registry[name]
if ok {
d = reflect.New(x).Interface().(DatapointValue)
d = reflect.New(x).Interface().(Datapoint)
}
return d, ok
}

0 comments on commit 7399706

Please sign in to comment.