forked from chronoxor/FastBinaryEncoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator_go.cpp
More file actions
8648 lines (7418 loc) · 296 KB
/
generator_go.cpp
File metadata and controls
8648 lines (7418 loc) · 296 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
\file generator_go.cpp
\brief Fast binary encoding Go generator implementation
\author Ivan Shynkarenka
\date 20.11.2018
\copyright MIT License
*/
#include "generator_go.h"
namespace FBE {
void GeneratorGo::Generate(const std::shared_ptr<Package>& package)
{
GenerateFBEPackage("fbe");
GenerateFBEConstants("fbe");
GenerateFBEJson("fbe");
GenerateFBEOptional("fbe");
GenerateFBETypes("fbe");
GenerateFBEVersion("fbe", "fbe");
GenerateFBEBuffer("fbe");
GenerateFBEFieldModel("fbe", "Bool", "bool", "1", "false");
GenerateFBEFieldModel("fbe", "Byte", "byte", "1", "0");
GenerateFBEFieldModel("fbe", "Char", "rune", "1", "'\\000'");
GenerateFBEFieldModel("fbe", "WChar", "rune", "4", "'\\000'");
GenerateFBEFieldModel("fbe", "Int8", "int8", "1", "0");
GenerateFBEFieldModel("fbe", "UInt8", "uint8", "1", "0");
GenerateFBEFieldModel("fbe", "Int16", "int16", "2", "0");
GenerateFBEFieldModel("fbe", "UInt16", "uint16", "2", "0");
GenerateFBEFieldModel("fbe", "Int32", "int32", "4", "0");
GenerateFBEFieldModel("fbe", "UInt32", "uint32", "4", "0");
GenerateFBEFieldModel("fbe", "Int64", "int64", "8", "0");
GenerateFBEFieldModel("fbe", "UInt64", "uint64", "8", "0");
GenerateFBEFieldModel("fbe", "Float", "float32", "4", "0.0");
GenerateFBEFieldModel("fbe", "Double", "float64", "8", "0.0");
GenerateFBEFieldModelDecimal("fbe");
GenerateFBEFieldModelTimestamp("fbe");
GenerateFBEFieldModelUUID("fbe");
GenerateFBEFieldModelBytes("fbe");
GenerateFBEFieldModelString("fbe");
if (Final())
{
GenerateFBEFinalModel("fbe", "Bool", "bool", "1", "false");
GenerateFBEFinalModel("fbe", "Byte", "byte", "1", "0");
GenerateFBEFinalModel("fbe", "Char", "rune", "1", "'\\000'");
GenerateFBEFinalModel("fbe", "WChar", "rune", "4", "'\\000'");
GenerateFBEFinalModel("fbe", "Int8", "int8", "1", "0");
GenerateFBEFinalModel("fbe", "UInt8", "uint8", "1", "0");
GenerateFBEFinalModel("fbe", "Int16", "int16", "2", "0");
GenerateFBEFinalModel("fbe", "UInt16", "uint16", "2", "0");
GenerateFBEFinalModel("fbe", "Int32", "int32", "4", "0");
GenerateFBEFinalModel("fbe", "UInt32", "uint32", "4", "0");
GenerateFBEFinalModel("fbe", "Int64", "int64", "8", "0");
GenerateFBEFinalModel("fbe", "UInt64", "uint64", "8", "0");
GenerateFBEFinalModel("fbe", "Float", "float32", "4", "0.0");
GenerateFBEFinalModel("fbe", "Double", "float64", "8", "0.0");
GenerateFBEFinalModelDecimal("fbe");
GenerateFBEFinalModelTimestamp("fbe");
GenerateFBEFinalModelUUID("fbe");
GenerateFBEFinalModelBytes("fbe");
GenerateFBEFinalModelString("fbe");
}
if (Proto())
{
GenerateFBESender("fbe");
GenerateFBEReceiver("fbe");
}
GeneratePackage(package);
}
void GeneratorGo::GenerateHeader(const std::string& source)
{
std::string code = R"CODE(// Automatically generated by the Fast Binary Encoding compiler, do not modify!
// https://github.com/chronoxor/FastBinaryEncoding
// Source: _INPUT_
// Version: _VERSION_
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("_INPUT_"), source);
code = std::regex_replace(code, std::regex("_VERSION_"), version);
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
}
void GeneratorGo::GenerateFooter()
{
}
void GeneratorGo::GenerateFBEPackage(const std::string& package)
{
CppCommon::Path path = CppCommon::Path(_output) / package;
// Create FBE package path
CppCommon::Directory::CreateTree(path);
}
void GeneratorGo::GenerateFBEConstants(const std::string& package)
{
CppCommon::Path path = CppCommon::Path(_output) / package;
// Generate the file
CppCommon::Path file = path / "Constants.go";
WriteBegin();
// Generate headers
GenerateHeader("fbe");
std::string code = R"CODE(
package fbe
// Maximal signed integer value
const MaxInt = int(^uint(0) >> 1)
// Minimal signed integer value
const MinInt = -MaxInt - 1
// Maximal unsigned integer value
const MaxUint = ^uint(0)
// Minimal unsigned integer value
const MinUint = 0
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
// Generate footer
GenerateFooter();
// Store the file
WriteEnd();
Store(file);
}
void GeneratorGo::GenerateFBEJson(const std::string& package)
{
CppCommon::Path path = CppCommon::Path(_output) / package;
// Generate the file
CppCommon::Path file = path / "Json.go";
WriteBegin();
// Generate headers
GenerateHeader("fbe");
std::string code = R"CODE(
package fbe
import "github.com/json-iterator/go"
// Json engine
var Json = jsoniter.ConfigCompatibleWithStandardLibrary
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
// Generate footer
GenerateFooter();
// Store the file
WriteEnd();
Store(file);
}
void GeneratorGo::GenerateFBEOptional(const std::string& package)
{
CppCommon::Path path = CppCommon::Path(_output) / package;
// Generate the file
CppCommon::Path file = path / "Optional.go";
WriteBegin();
// Generate headers
GenerateHeader("fbe");
std::string code = R"CODE(
package fbe
// Create an optional bool
func OptionalBool(value bool) *bool {
return &value
}
// Create an optional byte
func OptionalByte(value byte) *byte {
return &value
}
// Create an optional bytes
func OptionalBytes(value []byte) *[]byte {
return &value
}
// Create an optional rune
func OptionalRune(value rune) *rune {
return &value
}
// Create an optional int8
func OptionalInt8(value int8) *int8 {
return &value
}
// Create an optional uint8
func OptionalUInt8(value uint8) *uint8 {
return &value
}
// Create an optional int16
func OptionalInt16(value int16) *int16 {
return &value
}
// Create an optional uint16
func OptionalUInt16(value uint16) *uint16 {
return &value
}
// Create an optional int32
func OptionalInt32(value int32) *int32 {
return &value
}
// Create an optional uint32
func OptionalUInt32(value uint32) *uint32 {
return &value
}
// Create an optional int64
func OptionalInt64(value int64) *int64 {
return &value
}
// Create an optional uint64
func OptionalUInt64(value uint64) *uint64 {
return &value
}
// Create an optional float32
func OptionalFloat32(value float32) *float32 {
return &value
}
// Create an optional float64
func OptionalFloat64(value float64) *float64 {
return &value
}
// Create an optional decimal
func OptionalDecimal(value Decimal) *Decimal {
return &value
}
// Create an optional timestamp
func OptionalTimestamp(value Timestamp) *Timestamp {
return &value
}
// Create an optional string
func OptionalString(value string) *string {
return &value
}
// Create an optional UUID
func OptionalUUID(value UUID) *UUID {
return &value
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
// Generate footer
GenerateFooter();
// Store the file
WriteEnd();
Store(file);
}
void GeneratorGo::GenerateFBETypes(const std::string& package)
{
CppCommon::Path path = CppCommon::Path(_output) / package;
// Generate the file
CppCommon::Path file = path / "Types.go";
WriteBegin();
// Generate headers
GenerateHeader("fbe");
std::string code = R"CODE(
package fbe
import "time"
import "github.com/google/uuid"
import "github.com/shopspring/decimal"
// Decimal struct
type Decimal struct {
decimal.Decimal
}
// Create a new decimal from the given float value
func DecimalFromFloat(value float64) Decimal {
result := decimal.NewFromFloat(value)
return Decimal{result}
}
// Create a new decimal from the given string
func DecimalFromString(value string) Decimal {
result, _ := decimal.NewFromString(value)
return Decimal{result}
}
// Create zero decimal
func DecimalZero() Decimal {
return Decimal{decimal.New(0, 0)}
}
// Timestamp struct
type Timestamp struct {
time.Time
}
// Create a new timestamp from the given date
func TimestampFromDate(year, month, day int) Timestamp {
return Timestamp{time.Date(year, time.Month(month + 1), day, 0, 0, 0, 0, time.UTC)}
}
// Create a new timestamp from the given date
func TimestampFromDateTime(year, month, day, hour, minute, second, nanoseconds int) Timestamp {
return Timestamp{time.Date(year, time.Month(month + 1), day, hour, minute, second, nanoseconds, time.UTC)}
}
// Create a new timestamp from the given nanoseconds
func TimestampFromNanoseconds(nanoseconds uint64) Timestamp {
return Timestamp{time.Unix(int64(nanoseconds / 1000000000), int64(nanoseconds % 1000000000)).UTC()}
}
// Create Unix Epoch timestamp
func TimestampEpoch() Timestamp {
return Timestamp{time.Unix(0, 0).UTC()}
}
// Create the current UTC timestamp
func TimestampUTC() Timestamp {
return Timestamp{time.Now().UTC()}
}
// Convert timestamp to JSON
func (t *Timestamp) MarshalJSON() ([]byte, error) {
timestamp := t.UnixNano()
return Json.Marshal(×tamp)
}
// Convert JSON to timestamp
func (t *Timestamp) UnmarshalJSON(buffer []byte) error {
var nanoseconds int64
err := Json.Unmarshal(buffer, &nanoseconds)
if err != nil {
return err
}
*t = TimestampFromNanoseconds(uint64(nanoseconds))
return nil
}
// UUID struct
type UUID struct {
uuid.UUID
}
// Create a new UUID from the given bytes buffer
func UUIDFromBytes(buffer []byte) UUID {
result, _ := uuid.FromBytes(buffer)
return UUID{result}
}
// Create a new UUID from the given string
func UUIDFromString(value string) UUID {
result, _ := uuid.Parse(value)
return UUID{result}
}
// Create nil UUID0 (all bits set to zero)
func UUIDNil() UUID {
return UUID{uuid.Nil}
}
// Create sequential UUID1 (time based version)
func UUIDSequential() UUID {
result, _ := uuid.NewUUID()
return UUID{result}
}
// Create random UUID4 (randomly or pseudo-randomly generated version)
func UUIDRandom() UUID {
result, _ := uuid.NewRandom()
return UUID{result}
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
// Generate footer
GenerateFooter();
// Store the file
WriteEnd();
Store(file);
}
void GeneratorGo::GenerateFBEVersion(const std::string& package, const std::string& source)
{
CppCommon::Path path = CppCommon::Path(_output) / package;
// Generate the file
CppCommon::Path file = path / "Version.go";
WriteBegin();
// Generate headers
GenerateHeader(source);
std::string code = R"CODE(
package _PACKAGE_
// Package version
const Version = "_VERSION_"
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("_PACKAGE_"), package);
code = std::regex_replace(code, std::regex("_VERSION_"), version);
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
// Generate footer
GenerateFooter();
// Store the file
WriteEnd();
Store(file);
}
void GeneratorGo::GenerateFBEBuffer(const std::string& package)
{
CppCommon::Path path = CppCommon::Path(_output) / package;
// Generate the file
CppCommon::Path file = path / "Buffer.go";
WriteBegin();
// Generate headers
GenerateHeader("fbe");
std::string code = R"CODE(
package fbe
import "math"
// Fast Binary Encoding buffer based on dynamic byte array
type Buffer struct {
// Bytes memory buffer
data []byte
// Bytes memory buffer size
size int
// Bytes memory buffer offset
offset int
}
// Create an empty buffer
func NewEmptyBuffer() *Buffer {
return &Buffer{data: make([]byte, 0)}
}
// Create an empty buffer with the given capacity
func NewCapacityBuffer(capacity int) *Buffer {
return &Buffer{data: make([]byte, capacity)}
}
// Create a buffer with attached bytes memory buffer
func NewAttached(buffer []byte) *Buffer {
result := NewEmptyBuffer()
result.Attach(buffer)
return result
}
// Create a buffer with attached bytes memory buffer with offset and size
func NewAttachedBytes(buffer []byte, offset int, size int) *Buffer {
result := NewEmptyBuffer()
result.AttachBytes(buffer, offset, size)
return result
}
// Create a buffer with another attached buffer
func NewAttachedBuffer(buffer *Buffer) *Buffer {
result := NewEmptyBuffer()
result.AttachBuffer(buffer)
return result
}
// Is the buffer empty?
func (b *Buffer) Empty() bool { return (len(b.data) == 0) || (b.size <= 0) }
// Get bytes memory buffer
func (b *Buffer) Data() []byte { return b.data }
// Get bytes memory buffer capacity
func (b *Buffer) Capacity() int { return len(b.data) }
// Get bytes memory buffer size
func (b *Buffer) Size() int { return b.size }
// Get bytes memory buffer offset
func (b *Buffer) Offset() int { return b.offset }
// Attach an empty memory buffer
func (b *Buffer) AttachNew() {
b.data = make([]byte, 0)
b.size = 0
b.offset = 0
}
// Attach an empty memory buffer with the given capacity
func (b *Buffer) AttachCapacity(capacity int) {
b.data = make([]byte, capacity)
b.size = 0
b.offset = 0
}
// Attach the given bytes memory buffer
func (b *Buffer) Attach(buffer []byte) {
b.AttachBytes(buffer, 0, len(buffer))
}
// Attach the given bytes memory buffer with offset and size
func (b *Buffer) AttachBytes(buffer []byte, offset int, size int) {
if len(buffer) < size {
panic("invalid buffer")
}
if size <= 0 {
panic("invalid size")
}
if offset > size {
panic("invalid offset")
}
b.data = buffer
b.size = size
b.offset = offset
}
// Attach another buffer
func (b *Buffer) AttachBuffer(buffer *Buffer) {
b.AttachBytes(buffer.data, 0, buffer.size)
}
// Allocate memory in the current write buffer and return offset to the allocated memory block
func (b *Buffer) Allocate(size int) int {
if size < 0 {
panic("invalid allocation size")
}
offset := b.size
// Calculate a new buffer size
total := b.size + size
if total <= len(b.data) {
b.size = total
return offset
}
length := 2 * len(b.data)
if length < total {
length = total
}
data := make([]byte, length)
copy(data, b.data[:b.size])
b.data = data
b.size = total
return offset
}
// Remove some memory of the given size from the current write buffer
func (b *Buffer) Remove(offset int, size int) {
if (offset + size) > len(b.data) {
panic("invalid offset & size")
}
copy(b.data[offset:], b.data[offset+size:])
b.size -= size
if b.offset >= (offset + size) {
b.offset -= size
} else if b.offset >= offset {
b.offset -= b.offset - offset
if b.offset > b.size {
b.offset = b.size
}
}
}
// Reserve memory of the given capacity in the current write bufferb
func (b *Buffer) Reserve(capacity int) {
if capacity < 0 {
panic("invalid reserve capacity")
}
if capacity > len(b.data) {
length := 2 * len(b.data)
if length < capacity {
length = capacity
}
data := make([]byte, length)
copy(data, b.data[:b.size])
b.data = data
}
}
// Resize the current write buffer
func (b *Buffer) Resize(size int) {
b.Reserve(size)
b.size = size
if b.offset > b.size {
b.offset = b.size
}
}
// Reset the current write buffer and its offset
func (b *Buffer) Reset() {
b.size = 0
b.offset = 0
}
// Shift the current write buffer offset
func (b *Buffer) Shift(offset int) {
b.offset += offset
}
// Unshift the current write buffer offset
func (b *Buffer) Unshift(offset int) {
b.offset -= offset
}
// Buffer I/O methods
// Read bool from the buffer
func ReadBool(buffer []byte, offset int) bool {
return buffer[offset] != 0
}
// Read byte from the buffer
func ReadByte(buffer []byte, offset int) byte {
return buffer[offset]
}
// Read single byte character from the buffer
func ReadChar(buffer []byte, offset int) rune {
return rune(ReadUInt8(buffer, offset))
}
// Read four bytes character from the buffer
func ReadWChar(buffer []byte, offset int) rune {
return rune(ReadUInt32(buffer, offset))
}
// Read signed 8-bits integer from the buffer
func ReadInt8(buffer []byte, offset int) int8 {
return int8(buffer[offset])
}
// Read unsigned 8-bits integer from the buffer
func ReadUInt8(buffer []byte, offset int) uint8 {
return uint8(buffer[offset])
}
// Read signed 16-bits integer from the buffer
func ReadInt16(buffer []byte, offset int) int16 {
return (int16(buffer[offset + 0]) << 0) | (int16(buffer[offset + 1]) << 8)
}
// Read unsigned 16-bits integer from the buffer
func ReadUInt16(buffer []byte, offset int) uint16 {
return (uint16(buffer[offset + 0]) << 0) | (uint16(buffer[offset + 1]) << 8)
}
// Read signed 32-bits integer from the buffer
func ReadInt32(buffer []byte, offset int) int32 {
return (int32(buffer[offset + 0]) << 0) |
(int32(buffer[offset + 1]) << 8) |
(int32(buffer[offset + 2]) << 16) |
(int32(buffer[offset + 3]) << 24)
}
// Read unsigned 32-bits integer from the buffer
func ReadUInt32(buffer []byte, offset int) uint32 {
return (uint32(buffer[offset + 0]) << 0) |
(uint32(buffer[offset + 1]) << 8) |
(uint32(buffer[offset + 2]) << 16) |
(uint32(buffer[offset + 3]) << 24)
}
// Read signed 64-bits integer from the buffer
func ReadInt64(buffer []byte, offset int) int64 {
return (int64(buffer[offset + 0]) << 0) |
(int64(buffer[offset + 1]) << 8) |
(int64(buffer[offset + 2]) << 16) |
(int64(buffer[offset + 3]) << 24) |
(int64(buffer[offset + 4]) << 32) |
(int64(buffer[offset + 5]) << 40) |
(int64(buffer[offset + 6]) << 48) |
(int64(buffer[offset + 7]) << 56)
}
// Read unsigned 64-bits integer from the buffer
func ReadUInt64(buffer []byte, offset int) uint64 {
return (uint64(buffer[offset + 0]) << 0) |
(uint64(buffer[offset + 1]) << 8) |
(uint64(buffer[offset + 2]) << 16) |
(uint64(buffer[offset + 3]) << 24) |
(uint64(buffer[offset + 4]) << 32) |
(uint64(buffer[offset + 5]) << 40) |
(uint64(buffer[offset + 6]) << 48) |
(uint64(buffer[offset + 7]) << 56)
}
// Read float from the buffer
func ReadFloat(buffer []byte, offset int) float32 {
bits := ReadUInt32(buffer, offset)
return math.Float32frombits(bits)
}
// Read double from the buffer
func ReadDouble(buffer []byte, offset int) float64 {
bits := ReadUInt64(buffer, offset)
return math.Float64frombits(bits)
}
// Read bytes from the buffer
func ReadBytes(buffer []byte, offset int, size int) []byte {
return buffer[offset:offset + size]
}
// Read string from the buffer
func ReadString(buffer []byte, offset int, size int) string {
return string(buffer[offset:offset + size])
}
// Read timestamp from the buffer
func ReadTimestamp(buffer []byte, offset int) Timestamp {
nanoseconds := ReadUInt64(buffer, offset)
return TimestampFromNanoseconds(nanoseconds)
}
// Read UUID from the buffer
func ReadUUID(buffer []byte, offset int) UUID {
bytes := ReadBytes(buffer, offset, 16)
return UUIDFromBytes(bytes)
}
// Write bool into the buffer
func WriteBool(buffer []byte, offset int, value bool) {
if value {
buffer[offset] = 1
} else {
buffer[offset] = 0
}
}
// Write byte into the buffer
func WriteByte(buffer []byte, offset int, value byte) {
buffer[offset] = value
}
// Write single byte character into the buffer
func WriteChar(buffer []byte, offset int, value rune) {
WriteUInt8(buffer, offset, uint8(value))
}
// Write four bytes character into the buffer
func WriteWChar(buffer []byte, offset int, value rune) {
WriteUInt32(buffer, offset, uint32(value))
}
// Write signed 8-bits integer into the buffer
func WriteInt8(buffer []byte, offset int, value int8) {
buffer[offset] = byte(value)
}
// Write unsigned 8-bits integer into the buffer
func WriteUInt8(buffer []byte, offset int, value uint8) {
buffer[offset] = byte(value)
}
// Write signed 16-bits integer into the buffer
func WriteInt16(buffer []byte, offset int, value int16) {
buffer[offset + 0] = byte(value >> 0)
buffer[offset + 1] = byte(value >> 8)
}
// Write unsigned 16-bits integer into the buffer
func WriteUInt16(buffer []byte, offset int, value uint16) {
buffer[offset + 0] = byte(value >> 0)
buffer[offset + 1] = byte(value >> 8)
}
// Write signed 32-bits integer into the buffer
func WriteInt32(buffer []byte, offset int, value int32) {
buffer[offset + 0] = byte(value >> 0)
buffer[offset + 1] = byte(value >> 8)
buffer[offset + 2] = byte(value >> 16)
buffer[offset + 3] = byte(value >> 24)
}
// Write unsigned 32-bits integer into the buffer
func WriteUInt32(buffer []byte, offset int, value uint32) {
buffer[offset + 0] = byte(value >> 0)
buffer[offset + 1] = byte(value >> 8)
buffer[offset + 2] = byte(value >> 16)
buffer[offset + 3] = byte(value >> 24)
}
// Write signed 64-bits integer into the buffer
func WriteInt64(buffer []byte, offset int, value int64) {
buffer[offset + 0] = byte(value >> 0)
buffer[offset + 1] = byte(value >> 8)
buffer[offset + 2] = byte(value >> 16)
buffer[offset + 3] = byte(value >> 24)
buffer[offset + 4] = byte(value >> 32)
buffer[offset + 5] = byte(value >> 40)
buffer[offset + 6] = byte(value >> 48)
buffer[offset + 7] = byte(value >> 56)
}
// Write unsigned 64-bits integer into the buffer
func WriteUInt64(buffer []byte, offset int, value uint64) {
buffer[offset + 0] = byte(value >> 0)
buffer[offset + 1] = byte(value >> 8)
buffer[offset + 2] = byte(value >> 16)
buffer[offset + 3] = byte(value >> 24)
buffer[offset + 4] = byte(value >> 32)
buffer[offset + 5] = byte(value >> 40)
buffer[offset + 6] = byte(value >> 48)
buffer[offset + 7] = byte(value >> 56)
}
// Write float into the buffer
func WriteFloat(buffer []byte, offset int, value float32) {
WriteUInt32(buffer, offset, math.Float32bits(value))
}
// Write double into the buffer
func WriteDouble(buffer []byte, offset int, value float64) {
WriteUInt64(buffer, offset, math.Float64bits(value))
}
// Write bytes into the buffer
func WriteBytes(buffer []byte, offset int, value []byte) {
copy(buffer[offset:offset + len(value)], value)
}
// Write slice into the buffer
func WriteSlice(buffer []byte, offset int, value []byte, valueOffset int, valueSize int) {
copy(buffer[offset:offset + len(value)], value[valueOffset:valueOffset + valueSize])
}
// Write count of single byte into the buffer
func WriteCount(buffer []byte, offset int, value byte, valueCount int) {
for i := 0; i < valueCount; i++ {
buffer[offset + i] = value
}
}
// Write string into the buffer
func WriteString(buffer []byte, offset int, value string) {
WriteBytes(buffer, offset, []byte(value))
}
// Write timestamp into the buffer
func WriteTimestamp(buffer []byte, offset int, value Timestamp) {
nanoseconds := uint64(value.UnixNano())
WriteUInt64(buffer, offset, nanoseconds)
}
// Write UUID into the buffer
func WriteUUID(buffer []byte, offset int, value UUID) {
bytes, _ := value.MarshalBinary()
WriteBytes(buffer, offset, bytes)
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
// Generate footer
GenerateFooter();
// Store the file
WriteEnd();
Store(file);
}
void GeneratorGo::GenerateFBEFieldModel(const std::string& package, const std::string& name, const std::string& type, const std::string& size, const std::string& defaults)
{
CppCommon::Path path = CppCommon::Path(_output) / package;
// Generate the file
CppCommon::Path file = path / ("FieldModel" + name + ".go");
WriteBegin();
// Generate headers
GenerateHeader("fbe");
std::string code = R"CODE(
package fbe
import "errors"
// Fast Binary Encoding _NAME_ field model
type FieldModel_NAME_ struct {
// Field model buffer
buffer *Buffer
// Field model buffer offset
offset int
}
// Create a new _NAME_ field model
func NewFieldModel_NAME_(buffer *Buffer, offset int) *FieldModel_NAME_ {
return &FieldModel_NAME_{buffer: buffer, offset: offset}
}
// Get the field size
func (fm *FieldModel_NAME_) FBESize() int { return _SIZE_ }
// Get the field extra size
func (fm *FieldModel_NAME_) FBEExtra() int { return 0 }
// Get the field offset
func (fm *FieldModel_NAME_) FBEOffset() int { return fm.offset }
// Set the field offset
func (fm *FieldModel_NAME_) SetFBEOffset(value int) { fm.offset = value }
// Shift the current field offset
func (fm *FieldModel_NAME_) FBEShift(size int) { fm.offset += size }
// Unshift the current field offset
func (fm *FieldModel_NAME_) FBEUnshift(size int) { fm.offset -= size }
// Check if the value is valid
func (fm *FieldModel_NAME_) Verify() bool { return true }
// Get the value
func (fm *FieldModel_NAME_) Get() (_TYPE_, error) {
return fm.GetDefault(_DEFAULTS_)
}
// Get the value with provided default value
func (fm *FieldModel_NAME_) GetDefault(defaults _TYPE_) (_TYPE_, error) {
if (fm.buffer.Offset() + fm.FBEOffset() + fm.FBESize()) > fm.buffer.Size() {
return defaults, nil
}
return Read_NAME_(fm.buffer.Data(), fm.buffer.Offset() + fm.FBEOffset()), nil
}
// Set the value
func (fm *FieldModel_NAME_) Set(value _TYPE_) error {
if (fm.buffer.Offset() + fm.FBEOffset() + fm.FBESize()) > fm.buffer.Size() {
return errors.New("model is broken")
}
Write_NAME_(fm.buffer.Data(), fm.buffer.Offset() + fm.FBEOffset(), value)
return nil
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("_NAME_"), name);
code = std::regex_replace(code, std::regex("_TYPE_"), type);
code = std::regex_replace(code, std::regex("_SIZE_"), size);
code = std::regex_replace(code, std::regex("_DEFAULTS_"), defaults);
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
// Generate footer
GenerateFooter();
// Store the file