File: main.go

package info (click to toggle)
golang-github-tinylib-msgp 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,168 kB
  • sloc: makefile: 45
file content (350 lines) | stat: -rw-r--r-- 8,363 bytes parent folder | download
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
package main

import (
	"fmt"
	"os"
	"strings"
)

const template = `
// DecodeMsg decodes the message from the reader.
func (s *Foo) DecodeMsg(reader *msgp.Reader) error {
	if reader.IsNil() {
		*s = nil
		return reader.Skip()
	}
	sz, err := reader.ReadArrayHeader()
	if err != nil {
		return err
	}
	dst := *s
	if dst == nil {
		dst = make(Foo, sz)
	} else {
		for k := range dst {
			delete(dst, k)
		}
	}
	for i := uint32(0); i < sz; i++ {
		var k string
		k, err = reader.ReadString()
		if err != nil {
			return err
		}
		dst[string(k)] = struct{}{}
	}
	*s = dst
	return nil
}

// UnmarshalMsg decodes the message from the bytes.
func (s *Foo) UnmarshalMsg(bytes []byte) ([]byte, error) {
	if msgp.IsNil(bytes) {
		*s = nil
		return bytes[msgp.NilSize:], nil
	}
	// Read the array header
	sz, bytes, err := msgp.ReadArrayHeaderBytes(bytes)
	if err != nil {
		return nil, err
	}
	dst := *s
	if dst == nil {
		dst = make(Foo, sz)
	} else {
		for k := range dst {
			delete(dst, k)
		}
	}
	for i := uint32(0); i < sz; i++ {
		var k string
		k, bytes, err = msgp.ReadStringBytes(bytes)
		if err != nil {
			return nil, err
		}
		dst[string(k)] = struct{}{}
	}
	*s = dst
	return bytes, nil
}

// Msgsize returns the maximum size of the message.
func (s Foo) Msgsize() int {
	if s == nil {
		return msgp.NilSize
	}
	if len(s) == 0 {
		return msgp.ArrayHeaderSize
	}
	size := msgp.ArrayHeaderSize
	size += len(s) * msgp.StringPrefixSize
	return size
}
`

const unsorted = `
// Foo is a set of strings that will be stored as an array.
// Elements are not sorted and the order of elements is not guaranteed.
type Foo map[string]struct{}

// EncodeMsg encodes the message to the writer.
func (s Foo) EncodeMsg(writer *msgp.Writer) error {
	if s == nil {
		return writer.WriteNil()
	}
	err := writer.WriteArrayHeader(uint32(len(s)))
	if err != nil {
		return err
	}
	for k := range s {
		err = writer.WriteString(k)
		if err != nil {
			return err
		}
	}
	return nil
}

// MarshalMsg encodes the message to the bytes.
func (s Foo) MarshalMsg(bytes []byte) ([]byte, error) {
	if s == nil {
		return msgp.AppendNil(bytes), nil
	}
	if len(s) == 0 {
		return msgp.AppendArrayHeader(bytes, 0), nil
	}
	bytes = msgp.AppendArrayHeader(bytes, uint32(len(s)))
	for k := range s {
		bytes = msgp.AppendString(bytes, string(k))
	}
	return bytes, nil
}
`

const sorted = `
// Foo is a set of strings that will be stored as an array.
// Elements are sorted and the order of elements is guaranteed.
type Foo map[string]struct{}

// EncodeMsg encodes the message to the writer.
func (s Foo) EncodeMsg(writer *msgp.Writer) error {
	if s == nil {
		return writer.WriteNil()
	}
	err := writer.WriteArrayHeader(uint32(len(s)))
	if err != nil {
		return err
	}
	keys := make([]string, 0, len(s))
	for k := range s {
		keys = append(keys, k)
	}
	sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })

	for _, k := range keys {
		err = writer.WriteString(k)
		if err != nil {
			return err
		}
	}
	return nil
}

// MarshalMsg encodes the message to the bytes.
func (s Foo) MarshalMsg(bytes []byte) ([]byte, error) {
	if s == nil {
		return msgp.AppendNil(bytes), nil
	}
	if len(s) == 0 {
		return msgp.AppendArrayHeader(bytes, 0), nil
	}
	bytes = msgp.AppendArrayHeader(bytes, uint32(len(s)))
	keys := make([]string, 0, len(s))
	for k := range s {
		keys = append(keys, k)
	}
	sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
	for _, k := range keys {
		bytes = msgp.AppendString(bytes, k)
	}
	return bytes, nil
}
`

type replacer struct {
	GoType      string // 'string'
	PackageName string // 'Foo'
	DecodeValue string // 'ReadString'
	EncodeValue string // 'WriteString'
	AppendValue string // 'AppendString'
	KeyLen      string // 'size += msgp.StringPrefixSize'
	Sorter      string // 'sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })'
}

var replacers = []replacer{
	{
		GoType:      "string",
		PackageName: "Foo",
		DecodeValue: "ReadString",
		EncodeValue: "WriteString",
		AppendValue: "AppendString",
		KeyLen:      "size += len(s) * msgp.StringPrefixSize",
		Sorter:      "sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })",
	},
	{
		GoType:      "string",
		PackageName: "String",
		DecodeValue: "ReadString",
		EncodeValue: "WriteString",
		AppendValue: "AppendString",
		KeyLen:      "for key := range s {\n\t\t\tsize += msgp.StringPrefixSize + len(key)\n\t\t}",
		Sorter:      "sort.Strings(keys)",
	},
	{
		GoType:      "int",
		PackageName: "Int",
		DecodeValue: "ReadInt",
		EncodeValue: "WriteInt",
		AppendValue: "AppendInt",
		KeyLen:      "size += len(s) * msgp.IntSize",
	},
	{
		GoType:      "uint",
		PackageName: "Uint",
		DecodeValue: "ReadUint",
		EncodeValue: "WriteUint",
		AppendValue: "AppendUint",
		KeyLen:      "size += len(s) * msgp.UintSize",
	},
	{
		GoType:      "byte",
		PackageName: "Byte",
		DecodeValue: "ReadByte",
		EncodeValue: "WriteByte",
		AppendValue: "AppendByte",
		KeyLen:      "size += len(s) * msgp.ByteSize",
	},
	{
		GoType:      "int8",
		PackageName: "Int8",
		DecodeValue: "ReadInt8",
		EncodeValue: "WriteInt8",
		AppendValue: "AppendInt8",
		KeyLen:      "size += len(s) * msgp.Int8Size",
	},
	{
		GoType:      "uint8",
		PackageName: "Uint8",
		DecodeValue: "ReadUint8",
		EncodeValue: "WriteUint8",
		AppendValue: "AppendUint8",
		KeyLen:      "size += len(s) * msgp.Uint8Size",
	},
	{
		GoType:      "int16",
		PackageName: "Int16",
		DecodeValue: "ReadInt16",
		EncodeValue: "WriteInt16",
		AppendValue: "AppendInt16",
		KeyLen:      "size += len(s) * msgp.Int16Size",
	},
	{
		GoType:      "uint16",
		PackageName: "Uint16",
		DecodeValue: "ReadUint16",
		EncodeValue: "WriteUint16",
		AppendValue: "AppendUint16",
		KeyLen:      "size += len(s) * msgp.Uint16Size",
	},
	{
		GoType:      "int32",
		PackageName: "Int32",
		DecodeValue: "ReadInt32",
		EncodeValue: "WriteInt32",
		AppendValue: "AppendInt32",
		KeyLen:      "size += len(s) * msgp.Int32Size",
	},
	{
		GoType:      "uint32",
		PackageName: "Uint32",
		DecodeValue: "ReadUint32",
		EncodeValue: "WriteUint32",
		AppendValue: "AppendUint32",
		KeyLen:      "size += len(s) * msgp.Uint32Size",
	},
	{
		GoType:      "int64",
		PackageName: "Int64",
		DecodeValue: "ReadInt64",
		EncodeValue: "WriteInt64",
		AppendValue: "AppendInt64",
		KeyLen:      "size += len(s) * msgp.Int64Size",
	},
	{
		GoType:      "uint64",
		PackageName: "Uint64",
		DecodeValue: "ReadUint64",
		EncodeValue: "WriteUint64",
		AppendValue: "AppendUint64",
		KeyLen:      "size += len(s) * msgp.Uint64Size",
	},
	{
		GoType:      "float64",
		PackageName: "Float64",
		DecodeValue: "ReadFloat64",
		EncodeValue: "WriteFloat",
		AppendValue: "AppendFloat",
		KeyLen:      "size += len(s) * msgp.Float64Size",
	},
	{
		GoType:      "float32",
		PackageName: "Float32",
		DecodeValue: "ReadFloat32",
		EncodeValue: "WriteFloat32",
		AppendValue: "AppendFloat32",
		KeyLen:      "size += len(s) * msgp.Float32Size",
	},
}

func main() {
	out, err := os.Create("../generated.go")
	if err != nil {
		panic(err)
	}
	defer out.Close()
	fmt.Fprintln(out, `// Code generated by ./gen/main.go. DO NOT EDIT.

package setof

import (
	"sort"

	"github.com/tinylib/msgp/msgp"
)`)

	base := replacers[0]
	for _, r := range replacers[1:] {
		replaced := unsorted + template
		replaced = strings.ReplaceAll(replaced, base.GoType, r.GoType)
		replaced = strings.ReplaceAll(replaced, base.PackageName, r.PackageName)
		replaced = strings.ReplaceAll(replaced, base.EncodeValue, r.EncodeValue)
		replaced = strings.ReplaceAll(replaced, base.DecodeValue, r.DecodeValue)
		replaced = strings.ReplaceAll(replaced, base.AppendValue, r.AppendValue)
		replaced = strings.ReplaceAll(replaced, base.KeyLen, r.KeyLen)

		fmt.Fprintln(out, replaced)

		replaced = sorted + template
		replaced = strings.ReplaceAll(replaced, base.GoType, r.GoType)
		replaced = strings.ReplaceAll(replaced, base.PackageName, r.PackageName+"Sorted")
		replaced = strings.ReplaceAll(replaced, base.EncodeValue, r.EncodeValue)
		replaced = strings.ReplaceAll(replaced, base.DecodeValue, r.DecodeValue)
		replaced = strings.ReplaceAll(replaced, base.AppendValue, r.AppendValue)
		replaced = strings.ReplaceAll(replaced, base.KeyLen, r.KeyLen)
		if r.Sorter != "" {
			replaced = strings.ReplaceAll(replaced, base.Sorter, r.Sorter)
		}

		fmt.Fprintln(out, replaced)
	}
}