-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparco_compiler.go
140 lines (107 loc) · 3.94 KB
/
parco_compiler.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
130
131
132
133
134
135
136
137
138
139
140
package parco
import (
"encoding/binary"
"io"
"time"
)
type (
fieldCompiler[T any] interface {
Compile(item *T, writer io.Writer) error
}
Compiler[T any] struct {
fields []fieldCompiler[T]
}
)
func (c Compiler[T]) Compile(value T, w io.Writer) error {
for _, f := range c.fields {
if err := f.Compile(&value, w); err != nil {
return err
}
}
return nil
}
func CompilerModel[T any]() *Compiler[T] {
return &Compiler[T]{}
}
func (c *Compiler[T]) Struct(field fieldCompiler[T]) *Compiler[T] {
return c.register(field)
}
func (c *Compiler[T]) Slice(field fieldCompiler[T]) *Compiler[T] {
return c.register(field)
}
func (c *Compiler[T]) Array(field fieldCompiler[T]) *Compiler[T] {
return c.register(field)
}
func (c *Compiler[T]) Map(field fieldCompiler[T]) *Compiler[T] {
return c.register(field)
}
func (c *Compiler[T]) Varchar(getter Getter[T, string]) *Compiler[T] {
return c.register(StringFieldGetter[T](Varchar(), getter))
}
func (c *Compiler[T]) SmallVarchar(getter Getter[T, string]) *Compiler[T] {
return c.register(StringFieldGetter[T](SmallVarchar(), getter))
}
func (c *Compiler[T]) Bool(getter Getter[T, bool]) *Compiler[T] {
return c.register(BoolFieldGetter[T](Bool(), getter))
}
func (c *Compiler[T]) Byte(getter Getter[T, byte]) *Compiler[T] {
return c.register(UInt8FieldGetter[T](Byte(), getter))
}
func (c *Compiler[T]) UInt8(getter Getter[T, uint8]) *Compiler[T] {
return c.register(UInt8FieldGetter[T](UInt8(), getter))
}
func (c *Compiler[T]) Int8(getter Getter[T, int8]) *Compiler[T] {
return c.register(Int8FieldGetter[T](Int8(), getter))
}
func (c *Compiler[T]) UInt16(order binary.ByteOrder, getter Getter[T, uint16]) *Compiler[T] {
return c.register(UInt16FieldGetter[T](UInt16(order), getter))
}
func (c *Compiler[T]) Int16(order binary.ByteOrder, getter Getter[T, int16]) *Compiler[T] {
return c.register(Int16FieldGetter[T](Int16(order), getter))
}
func (c *Compiler[T]) UInt16LE(getter Getter[T, uint16]) *Compiler[T] {
return c.register(UInt16FieldGetter[T](UInt16LE(), getter))
}
func (c *Compiler[T]) UInt16BE(getter Getter[T, uint16]) *Compiler[T] {
return c.register(UInt16FieldGetter[T](UInt16BE(), getter))
}
func (c *Compiler[T]) UInt32(order binary.ByteOrder, getter Getter[T, uint32]) *Compiler[T] {
return c.register(UInt32FieldGetter[T](UInt32(order), getter))
}
func (c *Compiler[T]) Int32(order binary.ByteOrder, getter Getter[T, int32]) *Compiler[T] {
return c.register(Int32FieldGetter[T](Int32(order), getter))
}
func (c *Compiler[T]) UInt64(order binary.ByteOrder, getter Getter[T, uint64]) *Compiler[T] {
return c.register(UInt64FieldGetter[T](UInt64(order), getter))
}
func (c *Compiler[T]) Int64(order binary.ByteOrder, getter Getter[T, int64]) *Compiler[T] {
return c.register(Int64FieldGetter[T](Int64(order), getter))
}
func (c *Compiler[T]) Int(order binary.ByteOrder, getter Getter[T, int]) *Compiler[T] {
return c.register(IntFieldGetter[T](Int(order), getter))
}
func (c *Compiler[T]) Float32(order binary.ByteOrder, getter Getter[T, float32]) *Compiler[T] {
return c.register(Float32FieldGetter[T](Float32(order), getter))
}
func (c *Compiler[T]) Float64(order binary.ByteOrder, getter Getter[T, float64]) *Compiler[T] {
return c.register(Float64FieldGetter[T](Float64(order), getter))
}
func (c *Compiler[T]) Time(withLocation bool, getter Getter[T, time.Time]) *Compiler[T] {
return c.register(TimeFieldGetter[T](withLocation, getter))
}
func (c *Compiler[T]) TimeLocation(getter Getter[T, time.Time]) *Compiler[T] {
return c.register(TimeFieldGetter[T](true, getter))
}
func (c *Compiler[T]) TimeUTC(getter Getter[T, time.Time]) *Compiler[T] {
return c.register(TimeFieldGetter[T](false, getter))
}
func (c *Compiler[T]) Option(f fieldCompiler[T]) *Compiler[T] {
return c.register(f)
}
func (c *Compiler[T]) Field(f fieldCompiler[T]) *Compiler[T] {
return c.register(f)
}
func (c *Compiler[T]) register(field fieldCompiler[T]) *Compiler[T] {
c.fields = append(c.fields, field)
return c
}