Skip to content

Commit 1135fa3

Browse files
committed
update example to schema v2
1 parent e01e8ce commit 1135fa3

File tree

2 files changed

+112
-96
lines changed

2 files changed

+112
-96
lines changed

doc.go

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,59 @@ Package bstates implements a parser for Idefix event blobs.
33
44
# Overview
55
6-
Each binary blob is an event queues which is composed of one or more state encoded using the same schema. States represent a system state in a point of time. Schemas are json files which define States binary formats. Here there is an example schema which defines a 116bit [State] composed of 4 fields:
7-
8-
{
9-
"fields": [
10-
{
11-
"name": "3BITS_INT",
12-
"type": "int",
13-
"size": 3
14-
},
15-
{
16-
"name": "STATE_CODE",
17-
"type": "uint",
18-
"size": 2
19-
},
20-
{
21-
"name": "BOOL2",
22-
"type": "bool"
23-
},
24-
{
25-
"name": "MESSAGE_BUFFER",
26-
"type": "buffer",
27-
"size": 96
28-
}
29-
],
30-
"decoderIntMaps":
31-
{
32-
"STATE_MAP": {
33-
"0" : "IDLE",
34-
"1" : "STOPPED",
35-
"2" : "RUNNING"
36-
}
37-
},
38-
"mappedFields":
39-
{
40-
"STATE": {
41-
"from": "STATE_CODE",
42-
"mapId": "STATE_MAP"
43-
}
44-
},
45-
"decodedFields":
46-
{
47-
"MESSAGE": {
48-
"from": "MESSAGE_BUFFER",
49-
"decoder": "BufferToString"
50-
}
51-
}
6+
Each binary blob is an event queue which is composed of one or more state encoded using the same schema. States represent a system state in a point of time. Schemas are json files which define States binary formats. Here there is an example schema which defines a 102bit [State] composed of 4 fields:
7+
8+
{
9+
"version": "2.0",
10+
"encoderPipeline": "t:z",
11+
12+
"decodedFields":
13+
{
14+
{
15+
"name": "MESSAGE"
16+
"from": "MESSAGE_BUFFER",
17+
"decoder": "BufferToString"
18+
19+
},
20+
{
21+
"name": "STATE",
22+
"decoder": "IntMap",
23+
"params": {
24+
"from": "STATE_CODE",
25+
"mapId": "STATE_MAP"
26+
}
27+
},
28+
29+
"decoderIntMaps":
30+
{
31+
"STATE_MAP": {
32+
"0" : "IDLE",
33+
"1" : "STOPPED",
34+
"2" : "RUNNING"
35+
}
36+
},
37+
38+
"fields": [
39+
{
40+
"name": "3BITS_INT",
41+
"type": "int",
42+
"size": 3
43+
},
44+
{
45+
"name": "STATE_CODE",
46+
"type": "uint",
47+
"size": 2
48+
},
49+
{
50+
"name": "BOOL",
51+
"type": "bool"
52+
},
53+
{
54+
"name": "MESSAGE_BUFFER",
55+
"type": "buffer",
56+
"size": 96
57+
}
58+
]
5259
}
5360
5461
Decoders allow for complex codification of data, those saving space. In the above example there is a decodedField MESAGE which allows us to read MESSAGE_BUFFER as a string. Decoders work by executing the "decoder" parameter on the "from" field. Currently there are three types of [Decoder].

examples/e0/main.go

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,68 +10,79 @@ import (
1010

1111
func main() {
1212
schemaRaw := `
13-
{
14-
"fields": [
13+
{
14+
"version": "2.0",
15+
"encoderPipeline": "t:z",
16+
"decoderIntMaps":
17+
{
18+
"STATE_MAP": {
19+
"0" : "IDLE",
20+
"1" : "STOPPED",
21+
"2" : "RUNNING"
22+
}
23+
},
24+
"decodedFields": [
1525
{
16-
"name": "3BITS_INT",
17-
"type": "int",
18-
"size": 3
26+
"name": "MESSAGE",
27+
"decoder": "BufferToString",
28+
"params": {
29+
"from": "MESSAGE_BUFFER"
30+
}
1931
},
2032
{
21-
"name": "6BITS_UINT",
22-
"type": "uint",
23-
"size": 6
33+
"name": "STATE",
34+
"decoder": "IntMap",
35+
"params": {
36+
"from": "STATE_CODE",
37+
"mapId": "STATE_MAP"
38+
}
2439
},
40+
{
41+
"name": "TIMESTAMP_MS",
42+
"decoder": "NumberToUnixTsMs",
43+
"params": {
44+
"from": "48BIT_SECS_FROM_2022",
45+
"year": "2022",
46+
"factor": 1000
47+
}
48+
}
49+
],
50+
"fields": [
2551
{
2652
"name": "STATE_CODE",
27-
"type": "uint",
53+
"type": "int",
2854
"size": 2
29-
},
30-
{
31-
"name": "BOOL1",
32-
"type": "bool"
3355
},
3456
{
35-
"name": "8BITS_INT",
57+
"name": "CHAR",
3658
"type": "int",
3759
"size": 8
3860
},
3961
{
40-
"name": "BOOL2",
62+
"name": "BOOL",
4163
"type": "bool"
4264
},
65+
{
66+
"name": "3BITS_INT",
67+
"type": "int",
68+
"size": 3
69+
},
70+
{
71+
"name": "48BIT_SECS_FROM_2022",
72+
"type": "uint",
73+
"size": 48
74+
},
4375
{
4476
"name": "MESSAGE_BUFFER",
4577
"type": "buffer",
4678
"size": 96
4779
},
48-
{
49-
"name": "FLOAT32",
50-
"type": "float32"
51-
}
52-
],
53-
"decoderIntMaps":
54-
{
55-
"STATE_MAP": {
56-
"0" : "IDLE",
57-
"1" : "STOPPED",
58-
"2" : "RUNNING"
59-
}
60-
},
61-
"mappedFields":
62-
{
63-
"STATE": {
64-
"from": "STATE_CODE",
65-
"mapId": "STATE_MAP"
66-
}
67-
},
68-
"decodedFields":
69-
{
70-
"MESSAGE": {
71-
"from": "MESSAGE_BUFFER",
72-
"decoder": "BufferToString"
80+
{
81+
"name": "FLOAT32",
82+
"type": "float32"
7383
}
74-
}
84+
85+
]
7586
}`
7687

7788
var schema bstates.StateSchema
@@ -88,24 +99,22 @@ func main() {
8899
if err = state.Set("3BITS_INT", -3); err != nil {
89100
perrf("can't update value: %v\n", err)
90101
}
91-
if err = state.Set("6BITS_UINT", 0b111111); err != nil {
92-
perrf("can't update value: %v\n", err)
93-
}
94102
if err = state.Set("STATE_CODE", 2); err != nil {
95103
perrf("can't update value: %v\n", err)
96104
}
97-
if err = state.Set("BOOL1", true); err != nil {
105+
if err = state.Set("BOOL", true); err != nil {
98106
perrf("can't update value: %v\n", err)
99107
}
100-
if err = state.Set("8BITS_INT", -127); err != nil {
101-
perrf("can't update value: %v\n", err)
102-
}
103-
if err = state.Set("BOOL2", true); err != nil {
108+
if err = state.Set("MESSAGE_BUFFER", "Hello World"); err != nil {
104109
perrf("can't update value: %v\n", err)
105110
}
106-
if err = state.Set("MESSAGE_BUFFER", "Hello World"); err != nil {
111+
if err = state.Set("FLOAT32", 12345678.5); err != nil {
107112
perrf("can't update value: %v\n", err)
108113
}
114+
// update time
115+
//offsetDate := time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC)
116+
state.Set("48BIT_SECS_FROM_2022", 0)
117+
109118
if err = state.Set("FLOAT32", 12345678.5); err != nil {
110119
perrf("can't update value: %v\n", err)
111120
}

0 commit comments

Comments
 (0)