Compare commits
39 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a445b0bf5 | |||
|
|
775f079eb7 | ||
|
|
6dffd1fad4 | ||
|
|
c15710ddec | ||
|
|
2924d95f4b | ||
|
|
a39dcc06f4 | ||
|
|
e720f0d2d3 | ||
|
|
ce3ea4746b | ||
|
|
0150a0d120 | ||
|
|
b9c6aa9d02 | ||
|
|
4221815def | ||
|
|
eabac1b11b | ||
|
|
2d6db85b8c | ||
|
|
8e742ba1d3 | ||
|
|
2dc3baca3c | ||
|
|
274a80d605 | ||
|
|
3b38be8de5 | ||
|
|
83bf855485 | ||
|
|
9d4d417856 | ||
|
|
0446a160e1 | ||
|
|
640604c06d | ||
|
|
3accf6fc06 | ||
|
|
e3487eb340 | ||
|
|
c7154f0779 | ||
|
|
285a2add53 | ||
|
|
72d0d56868 | ||
|
|
ea4461fd7e | ||
|
|
0f22443b93 | ||
|
|
0d3161337f | ||
|
|
240f4fb3e7 | ||
|
|
bf36b023d3 | ||
|
|
a8127cc381 | ||
|
|
8615d8c3d5 | ||
|
|
6c327cf284 | ||
|
|
40648c5fd8 | ||
|
|
1b035e710f | ||
|
|
93f2188cc8 | ||
|
|
862d865338 | ||
|
|
ea4719ff01 |
@@ -1,2 +1,2 @@
|
||||
# tecamino-dbm-models
|
||||
# tecamino-json data models
|
||||
Database Manager Models
|
||||
|
||||
369
datapoints.go
369
datapoints.go
@@ -1,369 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket/wsjson"
|
||||
"github.com/google/uuid"
|
||||
serverModels "github.com/tecamino/tecamino-dbm/server/models"
|
||||
"github.com/tecamino/tecamino-dbm/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
OnCreate = "onCreate"
|
||||
OnChange = "onChange"
|
||||
OnDelete = "onDelete"
|
||||
)
|
||||
|
||||
type Datapoint struct {
|
||||
Datapoints map[string]*Datapoint `json:"-"`
|
||||
Uuid uuid.UUID `json:"uuid"`
|
||||
Path string `json:"path"`
|
||||
Value any `json:"value,omitempty"`
|
||||
CreateDateTime int64 `json:"createDateTime,omitempty"`
|
||||
UpdateDateTime int64 `json:"updateDateTime,omitempty"`
|
||||
Type Type `json:"type"`
|
||||
ReadWrite Rights `json:"readWrite"`
|
||||
Drivers Drivers `json:"drivers,omitempty"`
|
||||
Subscriptions Subscriptions `json:"-"`
|
||||
}
|
||||
|
||||
func (d *Datapoint) Set(path string, set Set) (bool, error) {
|
||||
var changed bool
|
||||
if path != "" {
|
||||
changed = true
|
||||
d.Path = path
|
||||
}
|
||||
|
||||
if set.Type != nil {
|
||||
changed = true
|
||||
d.Type = *set.Type
|
||||
}
|
||||
|
||||
if d.Type != "" {
|
||||
if d.Value == nil && set.Value == nil {
|
||||
changed = true
|
||||
d.Value = d.Type.DefaultValue()
|
||||
} else if d.Value != d.Type.ConvertValue(set.Value) {
|
||||
changed = true
|
||||
d.Value = d.Type.ConvertValue(set.Value)
|
||||
}
|
||||
|
||||
}
|
||||
if set.Rights != nil {
|
||||
changed = true
|
||||
d.ReadWrite = set.Rights.GetRights()
|
||||
}
|
||||
|
||||
if changed {
|
||||
d.UpdateDateTime = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
if set.Driver == nil {
|
||||
return changed, nil
|
||||
}
|
||||
if set.Driver.Type == "" {
|
||||
return changed, errors.New("driver type missing")
|
||||
}
|
||||
if set.Driver.Bus == "" {
|
||||
return changed, errors.New("driver bus name missing")
|
||||
}
|
||||
|
||||
if d.Drivers == nil {
|
||||
d.Drivers = NewDrivers()
|
||||
}
|
||||
d.Drivers.AddDriver(set.Driver.Type, set.Driver.Bus, set.Driver.Address)
|
||||
d.UpdateDateTime = time.Now().UnixMilli()
|
||||
|
||||
return changed, nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) GetValueUint64() uint64 {
|
||||
return utils.Uint64From(d.Value)
|
||||
}
|
||||
|
||||
func (d *Datapoint) CreateDatapoints(conns *serverModels.Connections, sets ...Set) (created []Set, err error) {
|
||||
if len(sets) == 0 {
|
||||
return
|
||||
}
|
||||
for _, dp := range sets {
|
||||
parts := strings.Split(dp.Path, ":")
|
||||
|
||||
current := d
|
||||
for i, part := range parts {
|
||||
if current.Datapoints == nil {
|
||||
current.Datapoints = make(map[string]*Datapoint)
|
||||
}
|
||||
|
||||
if i == len(parts)-1 {
|
||||
// Leaf node: create or update datapoint
|
||||
if existing, ok := current.Datapoints[part]; ok {
|
||||
publish, err := existing.Set("", dp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
created = append(created, Set{
|
||||
Path: existing.Path,
|
||||
Type: &existing.Type,
|
||||
Value: existing.Value,
|
||||
Rights: &existing.ReadWrite,
|
||||
Drivers: &existing.Drivers,
|
||||
Updated: true,
|
||||
})
|
||||
|
||||
if publish {
|
||||
existing.Publish(conns, OnChange)
|
||||
}
|
||||
} else {
|
||||
// Create new
|
||||
current.Datapoints[part] = &Datapoint{
|
||||
Uuid: uuid.New(),
|
||||
CreateDateTime: time.Now().UnixMilli(),
|
||||
Subscriptions: InitSubscribtion(),
|
||||
}
|
||||
publish, err := current.Datapoints[part].Set(strings.Join(parts, ":"), dp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
created = append(created, Set{
|
||||
Path: current.Path,
|
||||
Type: ¤t.Type,
|
||||
Value: current.Value,
|
||||
Rights: ¤t.ReadWrite,
|
||||
Driver: dp.Driver,
|
||||
})
|
||||
if publish {
|
||||
current.Publish(conns, OnChange)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Traverse or create intermediate datapoints
|
||||
if next, ok := current.Datapoints[part]; ok {
|
||||
current = next
|
||||
} else {
|
||||
newDp := &Datapoint{
|
||||
Uuid: uuid.New(),
|
||||
Path: strings.Join(parts[:i+1], ":"),
|
||||
Type: NONE,
|
||||
CreateDateTime: time.Now().UnixMilli(),
|
||||
UpdateDateTime: time.Now().UnixMilli(),
|
||||
Subscriptions: InitSubscribtion(),
|
||||
}
|
||||
|
||||
if dp.Rights != nil {
|
||||
newDp.ReadWrite = dp.Rights.GetRights()
|
||||
}
|
||||
|
||||
current.Datapoints[part] = newDp
|
||||
current = newDp
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) ImportDatapoint(conns *serverModels.Connections, dp Datapoint, path string) error {
|
||||
parts := strings.Split(dp.Path, ":")
|
||||
|
||||
current := d
|
||||
for i, part := range parts {
|
||||
if current.Datapoints == nil {
|
||||
current.Datapoints = make(map[string]*Datapoint)
|
||||
}
|
||||
|
||||
if i == len(parts)-1 {
|
||||
// Leaf node: import the datapoint
|
||||
if existing, ok := current.Datapoints[part]; ok {
|
||||
existing.Type = dp.Type
|
||||
existing.Value = current.Type.ConvertValue(dp.Value)
|
||||
existing.ReadWrite = dp.ReadWrite.GetRights()
|
||||
existing.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Publish(conns, OnChange)
|
||||
} else {
|
||||
dp.Path = strings.Join(parts, ":")
|
||||
dp.ReadWrite = dp.ReadWrite.GetRights()
|
||||
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Subscriptions = InitSubscribtion()
|
||||
current.Datapoints[part] = &dp
|
||||
dp.Publish(conns, OnChange)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Traverse or create intermediate nodes
|
||||
if next, ok := current.Datapoints[part]; ok {
|
||||
current = next
|
||||
} else {
|
||||
newDp := &Datapoint{
|
||||
Path: strings.Join(parts[:i+1], ":"),
|
||||
Type: NONE,
|
||||
ReadWrite: dp.ReadWrite.GetRights(),
|
||||
UpdateDateTime: time.Now().UnixMilli(),
|
||||
}
|
||||
newDp.ReadWrite = newDp.ReadWrite.GetRights()
|
||||
current.Datapoints[part] = newDp
|
||||
current = newDp
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) UpdateDatapointValue(conns *serverModels.Connections, value any, path string) error {
|
||||
|
||||
paths := strings.Split(path, ":")
|
||||
|
||||
current := d
|
||||
for i, part := range paths {
|
||||
dp, ok := current.Datapoints[part]
|
||||
if !ok {
|
||||
return fmt.Errorf("datapoint path not found: %s (at %s)", path, part)
|
||||
}
|
||||
if i == len(paths)-1 {
|
||||
dp.Value = dp.Type.ConvertValue(value)
|
||||
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Publish(conns, OnChange)
|
||||
return nil
|
||||
}
|
||||
current = dp
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) RemoveDatapoint(conns *serverModels.Connections, set Set) (Set, error) {
|
||||
parts := strings.Split(set.Path, ":")
|
||||
|
||||
if len(parts) < 1 {
|
||||
return Set{}, fmt.Errorf("invalid path: '%s'", set.Path)
|
||||
}
|
||||
|
||||
current := d
|
||||
for i := range len(parts) - 1 {
|
||||
next, ok := current.Datapoints[parts[i]]
|
||||
if !ok {
|
||||
return Set{}, fmt.Errorf("path not found: '%s'", strings.Join(parts[:i+1], ":"))
|
||||
}
|
||||
current = next
|
||||
}
|
||||
|
||||
toDelete := parts[len(parts)-1]
|
||||
if dp, ok := current.Datapoints[toDelete]; ok {
|
||||
dp.Publish(conns, OnDelete)
|
||||
delete(current.Datapoints, toDelete)
|
||||
return Set{
|
||||
Path: set.Path,
|
||||
}, nil
|
||||
}
|
||||
return Set{}, fmt.Errorf("datapoint '%s' not found", set.Path)
|
||||
}
|
||||
|
||||
func (d *Datapoint) GetAllDatapoints(depth int) (dps []*Datapoint) {
|
||||
|
||||
var dfs func(dp *Datapoint, currentDepth int)
|
||||
dfs = func(dp *Datapoint, currentDepth int) {
|
||||
if depth == 1 {
|
||||
return
|
||||
} else if depth == 0 {
|
||||
// Return all descendants
|
||||
for _, child := range dp.Datapoints {
|
||||
dps = append(dps, child)
|
||||
dfs(child, currentDepth+1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if currentDepth == depth-1 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, child := range dp.Datapoints {
|
||||
dps = append(dps, child)
|
||||
dfs(child, currentDepth+1)
|
||||
}
|
||||
}
|
||||
dps = append(dps, d)
|
||||
dfs(d, 0)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) QueryDatapoints(depth int, path string) (dps []*Datapoint) {
|
||||
parts := strings.Split(path, ":")
|
||||
|
||||
var dfs func(current *Datapoint, index int)
|
||||
dfs = func(current *Datapoint, index int) {
|
||||
|
||||
if index == len(parts) {
|
||||
dps = append(dps, current.GetAllDatapoints(depth)...)
|
||||
return
|
||||
}
|
||||
|
||||
pattern := "^" + parts[index] + "$"
|
||||
re, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for name, dp := range current.Datapoints {
|
||||
if re.MatchString(name) {
|
||||
dfs(dp, index+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dfs(d, 0)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) AddSubscribtion(id string, sub *Subscribe) {
|
||||
if d.Subscriptions == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if s, ok := d.Subscriptions[id]; ok {
|
||||
s.OnCreate = sub.GetOnCreate()
|
||||
s.OnChange = sub.GetOnChange()
|
||||
s.OnDelete = sub.GetOnDelete()
|
||||
} else {
|
||||
d.Subscriptions[id] = &Subscription{
|
||||
OnCreate: sub.GetOnCreate(),
|
||||
OnChange: sub.GetOnChange(),
|
||||
OnDelete: sub.GetOnDelete(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Datapoint) RemoveSubscribtion(id string) {
|
||||
if _, ok := d.Subscriptions[id]; !ok {
|
||||
return
|
||||
}
|
||||
delete(d.Subscriptions, id)
|
||||
}
|
||||
|
||||
func (d *Datapoint) Publish(conns *serverModels.Connections, eventType string) error {
|
||||
if conns.Clients == nil {
|
||||
return nil
|
||||
}
|
||||
conns.RLock()
|
||||
defer conns.RUnlock()
|
||||
|
||||
for id := range d.Subscriptions {
|
||||
if client, ok := conns.Clients[id]; !ok {
|
||||
delete(d.Subscriptions, id)
|
||||
} else {
|
||||
err := wsjson.Write(client.Ctx, client.Conn, Publish{
|
||||
Event: eventType,
|
||||
Path: d.Path,
|
||||
Value: d.Value,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
20
drivers.go
20
drivers.go
@@ -1,20 +0,0 @@
|
||||
package models
|
||||
|
||||
type Drivers map[string]*Driver
|
||||
|
||||
type Driver struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
Bus string `json:"bus"`
|
||||
Address int `json:"address"`
|
||||
}
|
||||
|
||||
func NewDrivers() Drivers {
|
||||
return make(Drivers)
|
||||
}
|
||||
|
||||
func (d *Drivers) AddDriver(typ, bus string, address int) {
|
||||
(*d)[typ] = &Driver{
|
||||
Bus: bus,
|
||||
Address: address,
|
||||
}
|
||||
}
|
||||
13
get.go
13
get.go
@@ -1,13 +0,0 @@
|
||||
package models
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
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"`
|
||||
}
|
||||
5
go.mod
Normal file
5
go.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module gitea.tecamino.com/paadi/tecamino-json_data
|
||||
|
||||
go 1.21.0
|
||||
|
||||
require github.com/google/uuid v1.6.0
|
||||
2
go.sum
Normal file
2
go.sum
Normal file
@@ -0,0 +1,2 @@
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
37
jsonData.go
37
jsonData.go
@@ -1,37 +0,0 @@
|
||||
package models
|
||||
|
||||
type JsonData struct {
|
||||
Get *[]Get `json:"get,omitempty"`
|
||||
Set *[]Set `json:"set,omitempty"`
|
||||
Subscribe *[]Subscribe `json:"subscribe,omitempty"`
|
||||
Subscribed *[]Subscribed `json:"subscribed,omitempty"`
|
||||
Unsubscribe *[]Subscribe `json:"unsubscribe,omitempty"`
|
||||
Publish *[]Publish `json:"publish,omitempty"`
|
||||
}
|
||||
|
||||
func NewRequest() *JsonData {
|
||||
return &JsonData{}
|
||||
|
||||
}
|
||||
|
||||
func (r *JsonData) AddGet(path string, query Query) {
|
||||
if r.Get == nil {
|
||||
r.Get = &[]Get{}
|
||||
}
|
||||
|
||||
*r.Get = append(*r.Get, Get{
|
||||
Path: path,
|
||||
Query: &query,
|
||||
})
|
||||
}
|
||||
|
||||
func (r *JsonData) AddSet(path string, value any, create bool) {
|
||||
if r.Set == nil {
|
||||
r.Set = &[]Set{}
|
||||
}
|
||||
|
||||
*r.Set = append(*r.Set, Set{
|
||||
Path: path,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package models
|
||||
|
||||
type JsonResponse struct {
|
||||
Error *bool `json:"error,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Data string `json:"data,omitempty"`
|
||||
Event string `json:"event,omitempty"`
|
||||
Path string `json:"path,omitempty"`
|
||||
Value any `json:"value,omitempty"`
|
||||
Set *[]Set `json:"set,omitempty"`
|
||||
Subscribe *[]Subscribe `json:"subscribe,omitempty"`
|
||||
Unubscribe *[]Subscribe `json:"unsubscribe,omitempty"`
|
||||
}
|
||||
56
models/bus.go
Normal file
56
models/bus.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package models
|
||||
|
||||
import "slices"
|
||||
|
||||
// bus model
|
||||
type Bus struct {
|
||||
Name string `json:"name"`
|
||||
Address []uint `json:"address,omitempty"` // address of bus
|
||||
Topic *Topic `json:"topic,omitempty"` // address of bus
|
||||
}
|
||||
|
||||
func (b *Bus) AddAddress(address ...uint) {
|
||||
for _, a := range address {
|
||||
if slices.Contains(b.Address, a) {
|
||||
continue
|
||||
}
|
||||
b.Address = append(b.Address, a)
|
||||
slices.Sort(b.Address)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bus) AddSubscription(sub ...string) {
|
||||
if b.Topic == nil {
|
||||
b.Topic = &Topic{}
|
||||
}
|
||||
b.Topic.AddSubscription(sub...)
|
||||
}
|
||||
|
||||
func (b *Bus) AddPublish(pub ...string) {
|
||||
if b.Topic == nil {
|
||||
b.Topic = &Topic{}
|
||||
}
|
||||
b.Topic.AddPublish(pub...)
|
||||
}
|
||||
|
||||
func (b *Bus) RemoveAddress(address ...uint) {
|
||||
if len(b.Address) == 1 {
|
||||
b.Address = []uint{}
|
||||
return
|
||||
}
|
||||
for _, toDelete := range address {
|
||||
for i, a := range b.Address {
|
||||
if a == toDelete {
|
||||
b.Address = slices.Delete(b.Address, i, i+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bus) RemoveSubscription(sub ...string) {
|
||||
b.Topic.RemoveSubscription(sub...)
|
||||
}
|
||||
|
||||
func (b *Bus) RemovePublish(pub ...string) {
|
||||
b.Topic.RemovePublish(pub...)
|
||||
}
|
||||
71
models/driver.go
Normal file
71
models/driver.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package models
|
||||
|
||||
// driver model
|
||||
type Driver struct {
|
||||
Type string `json:"type"` // driver type name of driver in collection
|
||||
Buses []*Bus `json:"buses,omitempty"` // name of driver bus
|
||||
}
|
||||
|
||||
func (d *Driver) AddNewBus(name string) *Bus {
|
||||
for _, b := range d.Buses {
|
||||
if b.Name == name {
|
||||
return b
|
||||
}
|
||||
}
|
||||
b := &Bus{Name: name}
|
||||
d.Buses = append(d.Buses, b)
|
||||
return b
|
||||
}
|
||||
|
||||
func (d *Driver) AddBuses(buses []*Bus) {
|
||||
next:
|
||||
for _, newBus := range buses {
|
||||
for _, currentBus := range d.Buses {
|
||||
if currentBus.Name != newBus.Name {
|
||||
continue
|
||||
}
|
||||
currentBus.AddAddress(newBus.Address...)
|
||||
if newBus.Topic == nil {
|
||||
continue next
|
||||
}
|
||||
currentBus.AddSubscription(newBus.Topic.Subscribe...)
|
||||
currentBus.AddPublish(newBus.Topic.Publish...)
|
||||
continue next
|
||||
}
|
||||
d.Buses = append(d.Buses, newBus)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Driver) RemoveBuses(buses []*Bus) {
|
||||
for _, bus := range buses {
|
||||
for i := 0; i < len(d.Buses); i++ {
|
||||
currentBus := d.Buses[i]
|
||||
if currentBus.Name != bus.Name {
|
||||
continue
|
||||
}
|
||||
|
||||
currentBus.RemoveAddress(bus.Address...)
|
||||
|
||||
shouldRemove := true
|
||||
if bus.Topic != nil {
|
||||
currentBus.RemoveSubscription(bus.Topic.Subscribe...)
|
||||
currentBus.RemovePublish(bus.Topic.Publish...)
|
||||
shouldRemove = len(currentBus.Topic.Subscribe) == 0 && len(currentBus.Topic.Publish) == 0
|
||||
}
|
||||
if len(currentBus.Address) == 0 && shouldRemove {
|
||||
d.Buses = append(d.Buses[:i], d.Buses[i+1:]...)
|
||||
i-- // adjust index after removal
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Driver) GetBus(name string) *Bus {
|
||||
for _, b := range d.Buses {
|
||||
if b.Name == name {
|
||||
return b
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
36
models/drivers.go
Normal file
36
models/drivers.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package models
|
||||
|
||||
// collection of drivers ordered in map
|
||||
type Drivers map[string]*Driver
|
||||
|
||||
func (d *Drivers) AddNewDriver(typ string) *Driver {
|
||||
if drv, ok := (*d)[typ]; ok {
|
||||
return drv
|
||||
}
|
||||
drv := &Driver{Type: typ}
|
||||
(*d)[typ] = drv
|
||||
return drv
|
||||
}
|
||||
|
||||
func (d *Drivers) AddDriver(drv *Driver) {
|
||||
if driver, ok := (*d)[drv.Type]; ok {
|
||||
driver.AddBuses(drv.Buses)
|
||||
return
|
||||
}
|
||||
(*d)[drv.Type] = drv
|
||||
}
|
||||
|
||||
func (d *Drivers) RemoveDriver(drv *Driver) {
|
||||
if driver, ok := (*d)[drv.Type]; ok {
|
||||
driver.RemoveBuses(drv.Buses)
|
||||
}
|
||||
d.CleanEmptyDrivers()
|
||||
}
|
||||
|
||||
func (d *Drivers) CleanEmptyDrivers() {
|
||||
for key, drv := range *d {
|
||||
if len(drv.Buses) == 0 {
|
||||
delete(*d, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
7
models/errorResponse.go
Normal file
7
models/errorResponse.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package models
|
||||
|
||||
// Json error response
|
||||
type ErrorResponse struct {
|
||||
Error bool `json:"error,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
31
models/get.go
Normal file
31
models/get.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package models
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
// Get model
|
||||
type Get struct {
|
||||
Uuid uuid.UUID `json:"uuid"` // universally unique identifier
|
||||
Path string `json:"path"` // dbm path
|
||||
Query *Query `json:"query,omitempty"` // query paramater
|
||||
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
||||
Type Type `json:"type,omitempty"` // dbm datatype
|
||||
Value any `json:"value,omitempty"` // dbm value
|
||||
HasChild bool `json:"hasChild,omitempty"` // inidicates path has children
|
||||
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 {
|
||||
g.Query = query
|
||||
}
|
||||
}
|
||||
|
||||
// search dbm datapoints by uuid
|
||||
func (g *Get) ByUuid(uid string, query *Query) {
|
||||
g.Uuid = uuid.MustParse(uid)
|
||||
if query != nil {
|
||||
g.Query = query
|
||||
}
|
||||
}
|
||||
49
models/publish.go
Normal file
49
models/publish.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitea.tecamino.com/paadi/tecamino-json_data/utils"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// publish model
|
||||
type Publish struct {
|
||||
Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete
|
||||
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
||||
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
||||
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:
|
||||
return utils.BoolFrom(p.Value)
|
||||
case BYS:
|
||||
return utils.Int8From(p.Value)
|
||||
case BYU:
|
||||
return utils.Uint8From(p.Value)
|
||||
case WOS:
|
||||
return utils.Int16From(p.Value)
|
||||
case WOU:
|
||||
return utils.Uint16From(p.Value)
|
||||
case DWS:
|
||||
return utils.Int32From(p.Value)
|
||||
case DWU:
|
||||
return utils.Uint32From(p.Value)
|
||||
case LOS:
|
||||
return utils.Int64From(p.Value)
|
||||
case LOU:
|
||||
return utils.Uint64From(p.Value)
|
||||
case F32:
|
||||
return utils.Float32From(p.Value)
|
||||
case F64:
|
||||
return utils.Float64From(p.Value)
|
||||
case STR:
|
||||
return fmt.Sprintf("%v", p.Value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
7
models/query.go
Normal file
7
models/query.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package models
|
||||
|
||||
// Query model
|
||||
type Query struct {
|
||||
Depth uint `json:"depth,omitempty"` // depth of query
|
||||
RegExp string `json:"regExp,omitempty"` // additional regex paramaters
|
||||
}
|
||||
69
models/request.go
Normal file
69
models/request.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package models
|
||||
|
||||
// Request model
|
||||
type Request struct {
|
||||
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, Subscription{
|
||||
Path: path,
|
||||
Depth: depth,
|
||||
OnCreate: onCreate,
|
||||
OnDelete: onChange,
|
||||
OnChange: onDelete,
|
||||
})
|
||||
}
|
||||
|
||||
// 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, Subscription{
|
||||
Path: path,
|
||||
Depth: depth,
|
||||
Driver: driverName,
|
||||
OnCreate: onCreate,
|
||||
OnDelete: onChange,
|
||||
OnChange: onDelete,
|
||||
})
|
||||
}
|
||||
|
||||
// Add new unsubsciption to request collection
|
||||
func (r *Request) AddUnsubscribe(path string, depth uint) {
|
||||
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, Subscription{
|
||||
Path: path,
|
||||
Depth: depth,
|
||||
Driver: driverName,
|
||||
})
|
||||
}
|
||||
84
models/response.go
Normal file
84
models/response.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package models
|
||||
|
||||
// Response model
|
||||
type Response struct {
|
||||
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{}
|
||||
}
|
||||
|
||||
// Set response as error
|
||||
func (r *Response) SetError() {
|
||||
r.Error = true
|
||||
}
|
||||
|
||||
// Set response message
|
||||
func (r *Response) SetMessage(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)
|
||||
}
|
||||
|
||||
// Add new subscription to response collection
|
||||
func (r *Response) AddSubscription(sub Subscription) {
|
||||
r.Subscribe = append(r.Subscribe, sub)
|
||||
}
|
||||
|
||||
// Add new unsubscribe to response collection
|
||||
func (r *Response) AddUnsubscription(sub Subscription) {
|
||||
r.Unsubscribe = append(r.Unsubscribe, sub)
|
||||
}
|
||||
|
||||
// Add new data publish to response
|
||||
func (r *Response) AddPublish(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
|
||||
}
|
||||
|
||||
// Returns slice of subscriptions collection
|
||||
func (r *Response) FetchSubscribes() []Subscription {
|
||||
return r.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
|
||||
}
|
||||
@@ -1,17 +1,18 @@
|
||||
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 == nil {
|
||||
return ReadWrite
|
||||
} else if *r == "" {
|
||||
if *r == "" {
|
||||
return ReadWrite
|
||||
}
|
||||
return *r
|
||||
67
models/set.go
Normal file
67
models/set.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package models
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
// Set model
|
||||
type Set struct {
|
||||
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
|
||||
Create bool `json:"create,omitempty"` // dbm create new datapoint
|
||||
HasChild bool `json:"hasChild,omitempty"` // inidicates path has children
|
||||
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
|
||||
s.Value = value
|
||||
s.Rights = rights.GetRights()
|
||||
}
|
||||
|
||||
// sets new driver typ with bus and address
|
||||
func (s *Set) NewDriverAddress(typ, bus string, address uint) {
|
||||
if s.Drivers == nil {
|
||||
s.Drivers = &Drivers{}
|
||||
}
|
||||
drv := s.Drivers.AddNewDriver(typ)
|
||||
b := drv.AddNewBus(bus)
|
||||
b.AddAddress(address)
|
||||
}
|
||||
|
||||
// sets new driver typ with bus and subscription
|
||||
func (s *Set) NewDriverSubscribe(typ, bus string, sub string) {
|
||||
if s.Drivers == nil {
|
||||
s.Drivers = &Drivers{}
|
||||
}
|
||||
drv := s.Drivers.AddNewDriver(typ)
|
||||
b := drv.AddNewBus(bus)
|
||||
b.AddSubscription(sub)
|
||||
}
|
||||
|
||||
// sets new driver typ with bus and publish
|
||||
func (s *Set) NewDriverPublish(typ, bus string, pub string) {
|
||||
if s.Drivers == nil {
|
||||
s.Drivers = &Drivers{}
|
||||
}
|
||||
drv := s.Drivers.AddNewDriver(typ)
|
||||
b := drv.AddNewBus(bus)
|
||||
b.AddPublish(pub)
|
||||
}
|
||||
18
models/subscription.go
Normal file
18
models/subscription.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package models
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
// Subscription model
|
||||
type Subscription struct {
|
||||
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
||||
Path string `json:"path,omitempty"` // dbm path
|
||||
Depth uint `json:"depth,omitempty"` // depth of subscriptions from found path or uuid
|
||||
Value any `json:"value,omitempty"` // current value
|
||||
Rights Rights `json:"rights,omitempty"` // read /write rights
|
||||
HasChild bool `json:"hasChild,omitempty"` // inidicates path has children
|
||||
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
||||
Driver string `json:"driver,omitempty"` // driver type to assign this subscription
|
||||
OnCreate bool `json:"onCreate,omitempty"` // notify at datapoint creation
|
||||
OnDelete bool `json:"onDelete,omitempty"` // notify on deletion
|
||||
OnChange bool `json:"onChange,omitempty"` // notify change
|
||||
}
|
||||
60
models/topic.go
Normal file
60
models/topic.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"slices"
|
||||
)
|
||||
|
||||
// topic model
|
||||
type Topic struct {
|
||||
Publish []string `json:"publish,omitempty"`
|
||||
Subscribe []string `json:"subscribe,omitempty"`
|
||||
}
|
||||
|
||||
func (t *Topic) AddSubscription(subs ...string) {
|
||||
for _, sub := range subs {
|
||||
if slices.Contains(t.Subscribe, sub) {
|
||||
continue
|
||||
}
|
||||
t.Subscribe = append(t.Subscribe, sub)
|
||||
slices.Sort(t.Subscribe)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Topic) AddPublish(pubs ...string) {
|
||||
for _, pub := range pubs {
|
||||
if slices.Contains(t.Publish, pub) {
|
||||
continue
|
||||
}
|
||||
t.Subscribe = append(t.Publish, pub)
|
||||
slices.Sort(t.Publish)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Topic) RemoveSubscription(subs ...string) {
|
||||
if len(t.Subscribe) == 1 {
|
||||
t.Subscribe = []string{}
|
||||
return
|
||||
}
|
||||
|
||||
for _, toDelete := range subs {
|
||||
for i, a := range t.Subscribe {
|
||||
if a == toDelete {
|
||||
t.Subscribe = slices.Delete(t.Subscribe, i, i+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Topic) RemovePublish(pubs ...string) {
|
||||
if len(t.Publish) == 1 {
|
||||
t.Publish = []string{}
|
||||
return
|
||||
}
|
||||
for _, toDelete := range pubs {
|
||||
for i, a := range t.Publish {
|
||||
if a == toDelete {
|
||||
t.Publish = slices.Delete(t.Publish, i, i+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,14 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
|
||||
"github.com/tecamino/tecamino-dbm/utils"
|
||||
"gitea.tecamino.com/paadi/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
|
||||
@@ -20,11 +20,13 @@ const (
|
||||
LOU Type = "LOU" // UINT64
|
||||
F32 Type = "F32" // FLOAT32
|
||||
F64 Type = "F64" // FLOAT64
|
||||
STR Type = "STRING" // STRING
|
||||
STR Type = "STR" // STRING
|
||||
)
|
||||
|
||||
// dbm datatype model
|
||||
type Type string
|
||||
|
||||
// return default value of assigned datatype
|
||||
func (t *Type) DefaultValue() any {
|
||||
switch *t {
|
||||
case BIT:
|
||||
@@ -37,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:
|
||||
@@ -66,37 +69,3 @@ func (t *Type) ConvertValue(v any) any {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func RandomType() Type {
|
||||
n := rand.Intn(11) + 1
|
||||
|
||||
switch n {
|
||||
case 1:
|
||||
return "BIT"
|
||||
case 2:
|
||||
return "BYU"
|
||||
case 3:
|
||||
return "BYS"
|
||||
case 4:
|
||||
return "WOS"
|
||||
case 5:
|
||||
return "WOU"
|
||||
case 6:
|
||||
return "DWS"
|
||||
case 7:
|
||||
return "DWU"
|
||||
case 8:
|
||||
return "LOS"
|
||||
case 9:
|
||||
return "LOU"
|
||||
case 10:
|
||||
return "F32"
|
||||
case 11:
|
||||
return "F64"
|
||||
case 12:
|
||||
return "STRING"
|
||||
default:
|
||||
return "NONE"
|
||||
}
|
||||
|
||||
}
|
||||
11
publish.go
11
publish.go
@@ -1,11 +0,0 @@
|
||||
package models
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type Publish struct {
|
||||
Event string `json:"event,omitempty"`
|
||||
Uuid uuid.UUID `json:"uuid,omitempty"`
|
||||
Path string `json:"path,omitempty"`
|
||||
Value any `json:"value,omitempty"`
|
||||
Driver *Driver `json:"driver,omitempty"`
|
||||
}
|
||||
6
query.go
6
query.go
@@ -1,6 +0,0 @@
|
||||
package models
|
||||
|
||||
type Query struct {
|
||||
Depth int `json:"depth,omitempty"`
|
||||
RegExp string `json:"regExp,omitempty"`
|
||||
}
|
||||
28
request.go
Normal file
28
request.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package tecaminodbmjson_data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
|
||||
"gitea.tecamino.com/paadi/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 {
|
||||
return nil, err
|
||||
}
|
||||
r := models.Request{}
|
||||
|
||||
err = json.Unmarshal(b, &r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &r, nil
|
||||
}
|
||||
28
response.go
Normal file
28
response.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package tecaminodbmjson_data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
|
||||
"gitea.tecamino.com/paadi/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 {
|
||||
return nil, err
|
||||
}
|
||||
r := models.Response{}
|
||||
|
||||
err = json.Unmarshal(b, &r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &r, nil
|
||||
}
|
||||
12
set.go
12
set.go
@@ -1,12 +0,0 @@
|
||||
package models
|
||||
|
||||
type Set struct {
|
||||
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"`
|
||||
Create bool `json:"create,omitempty"`
|
||||
Updated bool `json:"-"`
|
||||
}
|
||||
38
subscribe.go
38
subscribe.go
@@ -1,38 +0,0 @@
|
||||
package models
|
||||
|
||||
type Subscribe struct {
|
||||
Path string `json:"path"`
|
||||
Depth *int `json:"depth,omitempty"`
|
||||
Driver *string `json:"driver,omitempty"`
|
||||
OnCreate *bool `json:"onCreate,omitempty"`
|
||||
OnDelete *bool `json:"onDelete,omitempty"`
|
||||
OnChange *bool `json:"onChange,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Subscribe) GetDepth() int {
|
||||
if s.Depth == nil {
|
||||
return 0
|
||||
}
|
||||
return *s.Depth
|
||||
}
|
||||
|
||||
func (s *Subscribe) GetOnCreate() bool {
|
||||
if s.OnCreate == nil {
|
||||
return false
|
||||
}
|
||||
return *s.OnCreate
|
||||
}
|
||||
|
||||
func (s *Subscribe) GetOnChange() bool {
|
||||
if s.OnChange == nil {
|
||||
return false
|
||||
}
|
||||
return *s.OnChange
|
||||
}
|
||||
|
||||
func (s *Subscribe) GetOnDelete() bool {
|
||||
if s.OnDelete == nil {
|
||||
return false
|
||||
}
|
||||
return *s.OnDelete
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package models
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type Subscribed struct {
|
||||
Uuid uuid.UUID `json:"uuid"`
|
||||
Path string `json:"path"`
|
||||
Driver *Driver `json:"driver,omitempty"`
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
448
utils/convert.go
Normal file
448
utils/convert.go
Normal file
@@ -0,0 +1,448 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Returns float32 type from type interface
|
||||
func Float32From(v any) float32 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case float32:
|
||||
return val
|
||||
case float64:
|
||||
return float32(val)
|
||||
case int:
|
||||
return float32(val)
|
||||
case int8:
|
||||
return float32(val)
|
||||
case int16:
|
||||
return float32(val)
|
||||
case int32:
|
||||
return float32(val)
|
||||
case int64:
|
||||
return float32(val)
|
||||
case uint8:
|
||||
return float32(val)
|
||||
case uint16:
|
||||
return float32(val)
|
||||
case uint32:
|
||||
return float32(val)
|
||||
case uint64:
|
||||
return float32(val)
|
||||
case string:
|
||||
if f64, err := strconv.ParseFloat(val, 32); err == nil {
|
||||
return float32(f64)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// Returns float64 type from type interface
|
||||
func Float64From(v any) float64 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case float32:
|
||||
return float64(val)
|
||||
case float64:
|
||||
return val
|
||||
case int:
|
||||
return float64(val)
|
||||
case int8:
|
||||
return float64(val)
|
||||
case int16:
|
||||
return float64(val)
|
||||
case int32:
|
||||
return float64(val)
|
||||
case int64:
|
||||
return float64(val)
|
||||
case uint8:
|
||||
return float64(val)
|
||||
case uint16:
|
||||
return float64(val)
|
||||
case uint32:
|
||||
return float64(val)
|
||||
case uint64:
|
||||
return float64(val)
|
||||
case string:
|
||||
if f64, err := strconv.ParseFloat(val, 32); err == nil {
|
||||
return f64
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// Returns Int8 type from type interface
|
||||
func Int8From(v any) int8 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return int8(val)
|
||||
case int8:
|
||||
return val
|
||||
case int16:
|
||||
return int8(val)
|
||||
case int32:
|
||||
return int8(val)
|
||||
case int64:
|
||||
return int8(val)
|
||||
case uint8:
|
||||
return int8(val)
|
||||
case uint16:
|
||||
return int8(val)
|
||||
case uint32:
|
||||
return int8(val)
|
||||
case uint64:
|
||||
return int8(val)
|
||||
case float32:
|
||||
return int8(val)
|
||||
case float64:
|
||||
return int8(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return int8(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// Returns Int16 type from type interface
|
||||
func Int16From(v any) int16 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return int16(val)
|
||||
case int8:
|
||||
return int16(val)
|
||||
case int16:
|
||||
return val
|
||||
case int32:
|
||||
return int16(val)
|
||||
case int64:
|
||||
return int16(val)
|
||||
case uint8:
|
||||
return int16(val)
|
||||
case uint16:
|
||||
return int16(val)
|
||||
case uint32:
|
||||
return int16(val)
|
||||
case uint64:
|
||||
return int16(val)
|
||||
case float32:
|
||||
return int16(val)
|
||||
case float64:
|
||||
return int16(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return int16(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// Returns Int32 type from type interface
|
||||
func Int32From(v any) int32 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return int32(val)
|
||||
case int8:
|
||||
return int32(val)
|
||||
case int16:
|
||||
return int32(val)
|
||||
case int32:
|
||||
return val
|
||||
case int64:
|
||||
return int32(val)
|
||||
case uint8:
|
||||
return int32(val)
|
||||
case uint16:
|
||||
return int32(val)
|
||||
case uint32:
|
||||
return int32(val)
|
||||
case uint64:
|
||||
return int32(val)
|
||||
case float32:
|
||||
return int32(val)
|
||||
case float64:
|
||||
return int32(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return int32(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// Returns Int64 type from type interface
|
||||
func Int64From(v any) int64 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return int64(val)
|
||||
case int8:
|
||||
return int64(val)
|
||||
case int16:
|
||||
return int64(val)
|
||||
case int32:
|
||||
return int64(val)
|
||||
case int64:
|
||||
return val
|
||||
case uint8:
|
||||
return int64(val)
|
||||
case uint16:
|
||||
return int64(val)
|
||||
case uint32:
|
||||
return int64(val)
|
||||
case uint64:
|
||||
return int64(val)
|
||||
case float32:
|
||||
return int64(val)
|
||||
case float64:
|
||||
return int64(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return int64(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// Returns Uint8 type from type interface
|
||||
func Uint8From(v any) uint8 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return uint8(val)
|
||||
case int8:
|
||||
return uint8(val)
|
||||
case int16:
|
||||
return uint8(val)
|
||||
case int32:
|
||||
return uint8(val)
|
||||
case int64:
|
||||
return uint8(val)
|
||||
case uint8:
|
||||
return val
|
||||
case uint16:
|
||||
return uint8(val)
|
||||
case uint32:
|
||||
return uint8(val)
|
||||
case uint64:
|
||||
return uint8(val)
|
||||
case float32:
|
||||
return uint8(val)
|
||||
case float64:
|
||||
return uint8(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return uint8(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// Returns Uint16 type from type interface
|
||||
func Uint16From(v any) uint16 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return uint16(val)
|
||||
case int8:
|
||||
return uint16(val)
|
||||
case int16:
|
||||
return uint16(val)
|
||||
case int32:
|
||||
return uint16(val)
|
||||
case int64:
|
||||
return uint16(val)
|
||||
case uint8:
|
||||
return uint16(val)
|
||||
case uint16:
|
||||
return val
|
||||
case uint32:
|
||||
return uint16(val)
|
||||
case uint64:
|
||||
return uint16(val)
|
||||
case float32:
|
||||
return uint16(val)
|
||||
case float64:
|
||||
return uint16(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return uint16(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// Returns Uint32 type from type interface
|
||||
func Uint32From(v any) uint32 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return uint32(val)
|
||||
case int8:
|
||||
return uint32(val)
|
||||
case int16:
|
||||
return uint32(val)
|
||||
case int32:
|
||||
return uint32(val)
|
||||
case int64:
|
||||
return uint32(val)
|
||||
case uint8:
|
||||
return uint32(val)
|
||||
case uint16:
|
||||
return uint32(val)
|
||||
case uint32:
|
||||
return val
|
||||
case uint64:
|
||||
return uint32(val)
|
||||
case float32:
|
||||
return uint32(val)
|
||||
case float64:
|
||||
return uint32(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return uint32(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// Returns Uint64 type from type interface
|
||||
func Uint64From(v any) uint64 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return uint64(val)
|
||||
case int8:
|
||||
return uint64(val)
|
||||
case int16:
|
||||
return uint64(val)
|
||||
case int32:
|
||||
return uint64(val)
|
||||
case int64:
|
||||
return uint64(val)
|
||||
case uint8:
|
||||
return uint64(val)
|
||||
case uint16:
|
||||
return uint64(val)
|
||||
case uint32:
|
||||
return uint64(val)
|
||||
case uint64:
|
||||
return val
|
||||
case float32:
|
||||
return uint64(val)
|
||||
case float64:
|
||||
return uint64(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return uint64(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// Returns bool type from type interface
|
||||
func BoolFrom(v any) bool {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
return val
|
||||
case int:
|
||||
return val > 0
|
||||
case int8:
|
||||
return val > 0
|
||||
case int16:
|
||||
return val > 0
|
||||
case int32:
|
||||
return val > 0
|
||||
case int64:
|
||||
return val > 0
|
||||
case uint8:
|
||||
return val > 0
|
||||
case uint16:
|
||||
return val > 0
|
||||
case uint32:
|
||||
return val > 0
|
||||
case uint64:
|
||||
return val > 0
|
||||
case float32:
|
||||
return val >= 1
|
||||
case float64:
|
||||
return val >= 1
|
||||
case string:
|
||||
if strings.ToLower(val) == "true" {
|
||||
return true
|
||||
} else if strings.ToLower(val) == "false" {
|
||||
return false
|
||||
} else if i, err := strconv.Atoi(val); err == nil {
|
||||
return i > 0
|
||||
}
|
||||
return false
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user