From e3487eb340a9093b43a8bf7eb0e513e6fa4fbb28 Mon Sep 17 00:00:00 2001 From: Adrian Zuercher Date: Sun, 4 May 2025 20:55:58 +0200 Subject: [PATCH] add comments and new parameter id for client request order --- models/drivers.go | 11 +++++++--- models/errorResponse.go | 1 + models/get.go | 17 +++++++++------ models/publish.go | 12 ++++++----- models/query.go | 5 +++-- models/request.go | 29 ++++++++++++++++++------- models/response.go | 48 +++++++++++++++++++++++++++-------------- models/rights.go | 7 ++++-- models/set.go | 19 ++++++++++------ models/subscribe.go | 15 ------------- models/subscribtion.go | 13 ----------- models/type.go | 6 +++++- request.go | 2 ++ response.go | 2 ++ utils/convert.go | 11 ++++++++++ 15 files changed, 119 insertions(+), 79 deletions(-) delete mode 100644 models/subscribe.go delete mode 100644 models/subscribtion.go diff --git a/models/drivers.go b/models/drivers.go index 253700a..b02fd80 100644 --- a/models/drivers.go +++ b/models/drivers.go @@ -1,17 +1,22 @@ package models +// collection of drivers ordered in map type Drivers map[string]*Driver +// driver model type Driver struct { - Type string `json:"type,omitempty"` - Bus string `json:"bus"` - Address uint `json:"address"` + Type string `json:"type,omitempty"` // driver type name of driver in collection + Bus string `json:"bus"` // name of driver bus + Address uint `json:"address"` // address of bus } +// returns a new collection of drivers func NewDrivers() Drivers { return make(Drivers) } +// Add new driver to driver collection +// typ is the name of the driver in driver collection func (d *Drivers) AddDriver(typ, bus string, address uint) { (*d)[typ] = &Driver{ Bus: bus, diff --git a/models/errorResponse.go b/models/errorResponse.go index 5e9b906..d81e002 100644 --- a/models/errorResponse.go +++ b/models/errorResponse.go @@ -1,5 +1,6 @@ package models +// Json error response type ErrorResponse struct { Error bool `json:"error,omitempty"` Message string `json:"message,omitempty"` diff --git a/models/get.go b/models/get.go index 61cefce..93589c3 100644 --- a/models/get.go +++ b/models/get.go @@ -2,16 +2,18 @@ package models import "github.com/google/uuid" +// Get model type Get struct { - Uuid uuid.UUID `json:"uuid"` - Path string `json:"path"` - Query *Query `json:"query,omitempty"` - Drivers *Drivers `json:"driver,omitempty"` - Type Type `json:"type,omitempty"` - Value any `json:"value,omitempty"` - Rights Rights `json:"rights,omitempty"` + Uuid uuid.UUID `json:"uuid"` // universally unique identifier + Path string `json:"path"` // dbm path + Query *Query `json:"query,omitempty"` // query paramater + Drivers *Drivers `json:"driver,omitempty"` // assigned drivers + Type Type `json:"type,omitempty"` // dbm datatype + Value any `json:"value,omitempty"` // dbm value + Rights Rights `json:"rights,omitempty"` // dbm read /write rights } +// search dbm datapoints by path func (g *Get) ByPath(path string, query *Query) { g.Path = path if query != nil { @@ -19,6 +21,7 @@ func (g *Get) ByPath(path string, query *Query) { } } +// search dbm datapoints by uuid func (g *Get) ByUuid(uid string, query *Query) { g.Uuid = uuid.MustParse(uid) if query != nil { diff --git a/models/publish.go b/models/publish.go index 6152740..f7e3f06 100644 --- a/models/publish.go +++ b/models/publish.go @@ -7,14 +7,16 @@ import ( "github.com/tecamino/tecamino-json_data/utils" ) +// publish model type Publish struct { - Event string `json:"event,omitempty"` - Uuid uuid.UUID `json:"uuid,omitempty"` - Path string `json:"path,omitempty"` - Type Type `json:"type,omitempty"` - Value any `json:"value,omitempty"` + Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete + Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier + Path string `json:"path,omitempty"` // dbm path + Type Type `json:"type,omitempty"` // dbm datatype + Value any `json:"value,omitempty"` // dbm value } +// returns input parameter as assigned dbm datatype func (p *Publish) ConvertValue() any { switch p.Type { case BIT: diff --git a/models/query.go b/models/query.go index a22d7e1..f186ea6 100644 --- a/models/query.go +++ b/models/query.go @@ -1,6 +1,7 @@ package models +// Query model type Query struct { - Depth uint `json:"depth,omitempty"` - RegExp string `json:"regExp,omitempty"` + Depth uint `json:"depth,omitempty"` // depth of query + RegExp string `json:"regExp,omitempty"` // additional regex paramaters } diff --git a/models/request.go b/models/request.go index 13a2379..a29559b 100644 --- a/models/request.go +++ b/models/request.go @@ -1,26 +1,36 @@ package models +// Request model type Request struct { - Get []Get `json:"get,omitempty"` - Set []Set `json:"set,omitempty"` - Subscribe []Subscribe `json:"subscribe,omitempty"` - Unsubscribe []Subscribe `json:"unsubscribe,omitempty"` + Id string `json:"id,omitempty"` // identification from requesting client + Get []Get `json:"get,omitempty"` // collection of requested Gets + Set []Set `json:"set,omitempty"` // collection of requested Sets + Subscribe []Subscription `json:"subscribe,omitempty"` // collection of requested Subsciptions + Unsubscribe []Subscription `json:"unsubscribe,omitempty"` // collection of requested Unsubsciptions } +// Returns a new json_data Request model +func NewRequest() *Request { + return &Request{} +} + +// Add new get to request collection func (r *Request) AddGet() *Get { get := Get{} r.Get = append(r.Get, get) return &get } +// Add new set to request collection func (r *Request) AddSet() *Set { set := Set{} r.Set = append(r.Set, set) return &set } +// Add new subsciption to request collection func (r *Request) AddSubscription(path string, depth uint, onCreate, onChange, onDelete bool) { - r.Subscribe = append(r.Subscribe, Subscribe{ + r.Subscribe = append(r.Subscribe, Subscription{ Path: path, Depth: depth, OnCreate: onCreate, @@ -29,8 +39,9 @@ func (r *Request) AddSubscription(path string, depth uint, onCreate, onChange, o }) } +// Add new driver specific subsciption to request collection func (r *Request) AddDriverSubscription(path, driverName string, depth uint, onCreate, onChange, onDelete bool) { - r.Subscribe = append(r.Subscribe, Subscribe{ + r.Subscribe = append(r.Subscribe, Subscription{ Path: path, Depth: depth, Driver: driverName, @@ -40,15 +51,17 @@ func (r *Request) AddDriverSubscription(path, driverName string, depth uint, onC }) } +// Add new unsubsciption to request collection func (r *Request) AddUnsubscribe(path string, depth uint) { - r.Unsubscribe = append(r.Unsubscribe, Subscribe{ + r.Unsubscribe = append(r.Unsubscribe, Subscription{ Path: path, Depth: depth, }) } +// Add new driver unsubsciption to request collection func (r *Request) AddUnsubscribeDriver(path, driverName string, depth uint) { - r.Unsubscribe = append(r.Unsubscribe, Subscribe{ + r.Unsubscribe = append(r.Unsubscribe, Subscription{ Path: path, Depth: depth, Driver: driverName, diff --git a/models/response.go b/models/response.go index 5303a06..181de10 100644 --- a/models/response.go +++ b/models/response.go @@ -1,68 +1,84 @@ package models +// Response model type Response struct { - Error bool `json:"error,omitempty"` - Message string `json:"message,omitempty"` - Get []Get `json:"get,omitempty"` - Set []Set `json:"set,omitempty"` - Subscribe []Subscribe `json:"subscribe,omitempty"` - Unsubscribe []Subscribe `json:"unsubscribe,omitempty"` - Publish []Publish `json:"publish,omitempty"` + Id string `json:"id,omitempty"` // identification for requesting client + Error bool `json:"error,omitempty"` // indicated response is a error + Message string `json:"message,omitempty"` // response message + Get []Get `json:"get,omitempty"` // collection of requested Gets + Set []Set `json:"set,omitempty"` // collection of requested Sets + Subscribe []Subscription `json:"subscribe,omitempty"` // collection of requested Subscriptions + Unsubscribe []Subscription `json:"unsubscribe,omitempty"` // collection of requested Unscubscriptions + Publish []Publish `json:"publish,omitempty"` // collection of published data } +// Returns a new json_data Response model func NewResponse() *Response { return &Response{} } -func (r *Response) SendError(msg string) { +// Set response as error +func (r *Response) SetError() { r.Error = true +} + +// Set response message +func (r *Response) SetMessage(msg string) { r.Message = msg } -func (r *Response) SendMessage(msg string) { - r.Message = msg -} - +// Add new get to response collection func (r *Response) AddGet(get Get) { get.Query = nil r.Get = append(r.Get, get) } +// Add new set to response collection func (r *Response) AddSet(set Set) { r.Set = append(r.Set, set) } -func (r *Response) AddSubscription(sub Subscribe) { +// Add new subscription to response collection +func (r *Response) AddSubscription(sub Subscription) { r.Subscribe = append(r.Subscribe, sub) } -func (r *Response) AddUnsubscription(sub Subscribe) { +// Add new unsubscribe to response collection +func (r *Response) AddUnsubscription(sub Subscription) { r.Subscribe = append(r.Subscribe, sub) } +// Add new data publish to response func (r *Response) AddUPublish(pub Publish) { r.Publish = append(r.Publish, pub) } + +// Check if response has a error func (r *Response) IsValid() bool { return !r.Error } +// Returns slice of get collection func (r *Response) FetchGets() []Get { return r.Get } +// Returns slice of set collection func (r *Response) FetchSets() []Set { return r.Set } -func (r *Response) FetchSubscribes() []Subscribe { +// Returns slice of subscriptions collection +func (r *Response) FetchSubscribes() []Subscription { return r.Subscribe } -func (r *Response) FetchUnubscribes() []Subscribe { +// Returns slice of unsubscriptions collection +func (r *Response) FetchUnubscribes() []Subscription { return r.Unsubscribe } +// Returns slice of published data func (r *Response) FetchPublish() []Publish { return r.Publish } diff --git a/models/rights.go b/models/rights.go index fcb550c..b8309c9 100644 --- a/models/rights.go +++ b/models/rights.go @@ -1,13 +1,16 @@ package models -type Rights string - +// all avaiable read/write modes const ( Read Rights = "R" Write Rights = "W" ReadWrite Rights = "RW" ) +// dbm read/write model +type Rights string + +// return current rights func (r *Rights) GetRights() Rights { if *r == "" { return ReadWrite diff --git a/models/set.go b/models/set.go index 462c4c4..a4f84c6 100644 --- a/models/set.go +++ b/models/set.go @@ -2,27 +2,31 @@ package models import "github.com/google/uuid" +// Set model type Set struct { - Uuid uuid.UUID `json:"uuid,omitempty"` - Path string `json:"path"` - Driver *Driver `json:"driver,omitempty"` - Drivers *Drivers `json:"drivers,omitempty"` - Type Type `json:"type,omitempty"` - Value any `json:"value,omitempty"` - Rights Rights `json:"rights,omitempty"` + Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier + Path string `json:"path"` // dbm path + Driver *Driver `json:"driver,omitempty"` // assign new driver + Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers + Type Type `json:"type,omitempty"` // dbm datatype + Value any `json:"value,omitempty"` // dbm value + Rights Rights `json:"rights,omitempty"` // dbm read /write rights Updated bool `json:"-"` } +// sets value and path func (s *Set) ValueByPath(path string, value any) { s.Path = path s.Value = value } +// sets value and uuid func (s *Set) ValueByUuid(uid string, value any) { s.Uuid = uuid.MustParse(uid) s.Value = value } +// sets new datapoint with given parameters func (s *Set) New(path string, typ Type, value any, rights Rights) { s.Path = path s.Type = typ @@ -30,6 +34,7 @@ func (s *Set) New(path string, typ Type, value any, rights Rights) { s.Rights = rights.GetRights() } +// sets new driver typ with bus and address func (s *Set) NewDriver(typ, bus string, address uint) { s.Driver = &Driver{ Type: typ, diff --git a/models/subscribe.go b/models/subscribe.go deleted file mode 100644 index 2e4f2a1..0000000 --- a/models/subscribe.go +++ /dev/null @@ -1,15 +0,0 @@ -package models - -import "github.com/google/uuid" - -type Subscribe struct { - Uuid uuid.UUID `json:"uuid,omitempty"` - Path string `json:"path,omitempty"` - Depth uint `json:"depth,omitempty"` - Value any `json:"value,omitempty"` - Drivers *Drivers `json:"drivers,omitempty"` - Driver string `json:"driver,omitempty"` - OnCreate bool `json:"onCreate,omitempty"` - OnDelete bool `json:"onDelete,omitempty"` - OnChange bool `json:"onChange,omitempty"` -} diff --git a/models/subscribtion.go b/models/subscribtion.go deleted file mode 100644 index 1753d6e..0000000 --- a/models/subscribtion.go +++ /dev/null @@ -1,13 +0,0 @@ -package models - -//type Subscriptions map[string]*Subscription - -// type Subscription struct { -// OnCreate bool -// OnDelete bool -// OnChange bool -// } - -// func InitSubscribtion() Subscriptions { -// return make(Subscriptions) -// } diff --git a/models/type.go b/models/type.go index 5c1799d..43f584f 100644 --- a/models/type.go +++ b/models/type.go @@ -6,9 +6,10 @@ import ( "github.com/tecamino/tecamino-json_data/utils" ) +// all avaiable datatypes of dbm const ( NONE Type = "NONE" - BIT Type = "BIT" + BIT Type = "BIT" // BOOL BYU Type = "BYU" // UINT8 BYS Type = "BYS" // INT8 WOS Type = "WOS" // INT16 @@ -22,8 +23,10 @@ const ( STR Type = "STRING" // STRING ) +// dbm datatype model type Type string +// return default value of assigned datatype func (t *Type) DefaultValue() any { switch *t { case BIT: @@ -36,6 +39,7 @@ func (t *Type) DefaultValue() any { return nil } +// returns input parameter as assigned dbm datatype func (t *Type) ConvertValue(v any) any { switch *t { case BIT: diff --git a/request.go b/request.go index 3e82fc8..bab0bde 100644 --- a/request.go +++ b/request.go @@ -7,10 +7,12 @@ import ( "github.com/tecamino/tecamino-json_data/models" ) +// Returns a new json_data Request model func NewRequest() *models.Request { return &models.Request{} } +// Parses the request body of a http or web socket request func ParseRequest(body io.ReadCloser) (*models.Request, error) { b, err := io.ReadAll(body) if err != nil { diff --git a/response.go b/response.go index 3a64fdd..8719ac9 100644 --- a/response.go +++ b/response.go @@ -7,10 +7,12 @@ import ( "github.com/tecamino/tecamino-json_data/models" ) +// Returns a new json_data Response model func NewResponse() *models.Response { return &models.Response{} } +// Parses the response body of a http or web socket request func ParseResponse(body io.ReadCloser) (*models.Response, error) { b, err := io.ReadAll(body) if err != nil { diff --git a/utils/convert.go b/utils/convert.go index 4970f9a..326647b 100644 --- a/utils/convert.go +++ b/utils/convert.go @@ -5,6 +5,7 @@ import ( "strings" ) +// Returns float32 type from type interface func Float32From(v any) float32 { switch val := v.(type) { case bool: @@ -44,6 +45,7 @@ func Float32From(v any) float32 { } } +// Returns float64 type from type interface func Float64From(v any) float64 { switch val := v.(type) { case bool: @@ -83,6 +85,7 @@ func Float64From(v any) float64 { } } +// Returns Int8 type from type interface func Int8From(v any) int8 { switch val := v.(type) { case bool: @@ -122,6 +125,7 @@ func Int8From(v any) int8 { } } +// Returns Int16 type from type interface func Int16From(v any) int16 { switch val := v.(type) { case bool: @@ -161,6 +165,7 @@ func Int16From(v any) int16 { } } +// Returns Int32 type from type interface func Int32From(v any) int32 { switch val := v.(type) { case bool: @@ -200,6 +205,7 @@ func Int32From(v any) int32 { } } +// Returns Int64 type from type interface func Int64From(v any) int64 { switch val := v.(type) { case bool: @@ -239,6 +245,7 @@ func Int64From(v any) int64 { } } +// Returns Uint8 type from type interface func Uint8From(v any) uint8 { switch val := v.(type) { case bool: @@ -278,6 +285,7 @@ func Uint8From(v any) uint8 { } } +// Returns Uint16 type from type interface func Uint16From(v any) uint16 { switch val := v.(type) { case bool: @@ -317,6 +325,7 @@ func Uint16From(v any) uint16 { } } +// Returns Uint32 type from type interface func Uint32From(v any) uint32 { switch val := v.(type) { case bool: @@ -356,6 +365,7 @@ func Uint32From(v any) uint32 { } } +// Returns Uint64 type from type interface func Uint64From(v any) uint64 { switch val := v.(type) { case bool: @@ -395,6 +405,7 @@ func Uint64From(v any) uint64 { } } +// Returns bool type from type interface func BoolFrom(v any) bool { switch val := v.(type) { case bool: