-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
129 lines (110 loc) · 1.95 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"encoding/json"
"github.com/gorilla/websocket"
)
type Sub struct {
ws *websocket.Conn
}
func makeSub(ws *websocket.Conn) *Sub {
result := new(Sub)
result.ws = ws
return result
}
type Subs = map[*Sub]bool
type SubStorage = map[string]Subs
type WebSocketSubMap = map[*websocket.Conn]*Sub
type SchemaId string
type FieldId string
type FieldType int32
const (
F_STR FieldType = iota
F_INT
F_FLOAT
F_INT_ARRAY
F_FLOAT_ARRAY
)
type Schema struct {
Fields map[FieldId]FieldType
}
type Schemas = map[SchemaId]Schema
type InstanceData map[FieldId]any
type Instance struct {
Data InstanceData
SchemaId SchemaId
}
type InstanceId string
type Instances = map[InstanceId]Instance
type ReqJson struct {
Id string
Type string
Msg json.RawMessage
}
type ReqAuth struct {
Id string
}
type ReqSub struct {
Id InstanceId
IsTopic any
}
type ReqSchemaSet struct {
SchemaId SchemaId
Schema Schema
}
type ReqSchemaGet struct {
Id SchemaId
}
type ReqMut struct {
Id InstanceId
Change InstanceData
}
type ReqInst struct {
SchemaId SchemaId
}
type ResJson struct {
Id string
Type string
Error any
Msg json.RawMessage
}
type ResList struct {
Topics []SchemaId
}
type ResSchemaGet struct {
Id SchemaId
Schema Schema
}
type ResPub struct {
Id InstanceId
Change InstanceData
}
type ResInst struct {
InstanceId InstanceId
Instance Instance
}
func FieldDefaultForType(FieldType FieldType) any {
if FieldType == F_STR {
return ""
} else if FieldType == F_INT {
return 0
} else if FieldType == F_FLOAT {
return 0.00
} else if FieldType == F_FLOAT_ARRAY {
return []float32{}
} else if FieldType == F_INT_ARRAY {
return []int32{}
}
return nil
}
func valueIsFieldType(v any, f FieldType, isStrict bool) bool {
// fmt.Println("Type", reflect.TypeOf(v))
switch v.(type) {
case string:
return f == F_STR
case int:
case float32:
case float64:
return f == F_FLOAT || f == F_INT
}
return false
}