Skip to content

Commit 4a53502

Browse files
committed
unexport static table indices and stop sharing msgp varuint markers with vpack
1 parent e29e203 commit 4a53502

File tree

5 files changed

+171
-138
lines changed

5 files changed

+171
-138
lines changed

network/vpack/defs.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,24 @@
1717
package vpack
1818

1919
const (
20-
// 0x00 - 0x7f reserved for dynamic table entries
20+
// 0x00 - 0xbf reserved for dynamic table entries
21+
// 0xc0 - 0xef reserved for static table entries
22+
// 0xf0 - 0xff reserved for markers
2123

2224
// Binary types: 64-byte, 80-byte literals and 32-byte dynamic binary values
23-
markerLiteralBin64 = 0xc8 // signatures
24-
markerLiteralBin80 = 0xc9 // pf
25-
markerDynamicBin32 = 0xca // digests, addresses, pubkeys
25+
markerLiteralBin64 = 0xf0 // signatures
26+
markerLiteralBin80 = 0xf1 // pf
27+
markerDynamicBin32 = 0xf2 // digests, addresses, pubkeys
2628

27-
markerDynamicFixuint = 0xcb // msgpack fixuint
28-
markerDynamicUint8 = 0xcc // msgpack uint8
29-
markerDynamicUint16 = 0xcd // msgpack uint16
30-
markerDynamicUint32 = 0xce // msgpack uint32
31-
markerDynamicUint64 = 0xcf // msgpack uint64
29+
markerDynamicFixuint = 0xf3 // msgpack fixuint
30+
markerDynamicUint8 = 0xf4 // msgpack uint8
31+
markerDynamicUint16 = 0xf5 // msgpack uint16
32+
markerDynamicUint32 = 0xf6 // msgpack uint32
33+
markerDynamicUint64 = 0xf7 // msgpack uint64
3234
)
3335

3436
func isStaticIdx(idx uint8) bool {
35-
return idx >= 0xc1 && idx <= 0xe6
37+
return idx >= staticIdxStart && idx <= staticIdxEnd
3638
}
3739

3840
const (

network/vpack/gen.go

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232

3333
// staticItem represents one entry in the static table.
3434
type staticItem struct {
35-
ConstName string // e.g. "StaticIdxPsField"
35+
ConstName string // e.g. "staticIdxPsField"
3636
Index uint8
3737
Data []byte
3838
Comment string
@@ -41,7 +41,7 @@ type staticItem struct {
4141
// specialPattern is used to generate code snippets in createGeneratedStaticTable()
4242
// that do more complex expansions (like all-zero or numeric-value appends).
4343
type specialPattern struct {
44-
Code string // e.g. "zeroField := append(...) ; t[StaticIdxAllZeroPsField] = zeroField"
44+
Code string // e.g. "zeroField := append(...) ; t[staticIdxAllZeroPsField] = zeroField"
4545
}
4646

4747
// codeGenerator is our main driver for reflection + generating code via templates.
@@ -66,15 +66,15 @@ type parseFuncData struct {
6666
// fieldData describes one field in the struct for parse code generation.
6767
type fieldData struct {
6868
CodecName string // e.g. "ps", "step", "data"
69-
FieldNameConst string // e.g. "StaticIdxPsField"
69+
FieldNameConst string // e.g. "staticIdxPsField"
7070

7171
IsSubStruct bool
7272
SubStructName string
7373

7474
ArrayLen int
7575
IsLiteral bool
7676
IsZeroCheck bool // e.g. "0" special for a binary field
77-
ZeroConst string // e.g. "StaticIdxAllZeroDataField"
77+
ZeroConst string // e.g. "staticIdxAllZeroDataField"
7878
VpackSpecial string // e.g. "1,2,3" for numeric
7979

8080
IsUint64Alias bool
@@ -116,6 +116,10 @@ const (
116116
{{- range .Items}}
117117
{{.ConstName}} uint8 = 0x{{printf "%02x" .Index}}
118118
{{- end}}
119+
120+
// Constants for static index range bounds
121+
staticIdxStart uint8 = 0x{{printf "%02x" .StaticIdxStart}}
122+
staticIdxEnd uint8 = 0x{{printf "%02x" .StaticIdxEnd}}
119123
)
120124
121125
var staticTable = createGeneratedStaticTable()
@@ -141,7 +145,7 @@ const parseFuncTemplate = `
141145
if cnt < 1 || cnt > {{.MaxFieldCount}} {
142146
return fmt.Errorf("expected fixmap size for {{.TypeName}} 1 <= cnt <= {{.MaxFieldCount}}, got %d", cnt)
143147
}
144-
c.writeStatic(StaticIdxMapMarker0+cnt)
148+
c.writeStatic(staticIdxMapMarker0+cnt)
145149
146150
for range cnt {
147151
key, err := p.readString()
@@ -176,7 +180,9 @@ const parseFuncTemplate = `
176180
}
177181
{{end}}
178182
{{ if $fd.VpackSpecial }}c.writeStatic({{$fd.FieldNameConst}}){{ end }}
179-
c.writeDynamicVaruint(valBytes)
183+
if err := c.writeDynamicVaruint(valBytes); err != nil {
184+
return fmt.Errorf("writing {{$fd.CodecName}}: %w", err)
185+
}
180186
{{- else if gt $fd.ArrayLen 0}}
181187
{{ if and (not $fd.VpackSpecial) (not $fd.IsZeroCheck) }}c.writeStatic({{$fd.FieldNameConst}}){{end}}
182188
val, err := p.readBin{{$fd.ArrayLen}}()
@@ -276,7 +282,7 @@ import (
276282
if err != nil {
277283
return fmt.Errorf("formatting static table: %w", err)
278284
}
279-
if os.WriteFile("static_table.go", formatted, 0644); err != nil {
285+
if err := os.WriteFile("static_table.go", formatted, 0644); err != nil {
280286
return err
281287
}
282288

@@ -285,7 +291,7 @@ import (
285291
if err != nil {
286292
return fmt.Errorf("formatting parser: %w", err)
287293
}
288-
if os.WriteFile("parse.go", formatted, 0644); err != nil {
294+
if err := os.WriteFile("parse.go", formatted, 0644); err != nil {
289295
return err
290296
}
291297

@@ -356,10 +362,10 @@ func (g *codeGenerator) analyzeType(t reflect.Type) error {
356362
}
357363

358364
// defineSpecialZeroBinary is a generic helper for any fixed-length binary type
359-
// with "vpack_special_values:'0'". We produce e.g. "StaticIdxAllZeroDataField"
365+
// with "vpack_special_values:'0'". We produce e.g. "staticIdxAllZeroDataField"
360366
// and a snippet in specialPatterns that appends the bin8 marker + zero bytes.
361367
func (g *codeGenerator) defineSpecialZeroBinary(codecName string, length int) string {
362-
allZeroConst := "StaticIdxAllZero" + strings.Title(codecName) + "Field"
368+
allZeroConst := "staticIdxAllZero" + strings.Title(codecName) + "Field"
363369
if g.findItemIndexByConstName(allZeroConst) >= 0 {
364370
return allZeroConst
365371
}
@@ -373,7 +379,7 @@ func (g *codeGenerator) defineSpecialZeroBinary(codecName string, length int) st
373379
Comment: fmt.Sprintf("All-zero %s field for length %d", codecName, length),
374380
})
375381

376-
baseField := g.getOrCreateStaticIndexForField(codecName) // e.g. "StaticIdxDataField"
382+
baseField := g.getOrCreateStaticIndexForField(codecName) // e.g. "staticIdxDataField"
377383

378384
// The snippet:
379385
// zeroVal := append(t[<baseField>], t[<bin8Const>]...)
@@ -390,14 +396,14 @@ t[%s] = zeroVal`,
390396

391397
// defineSpecialValuesNumeric handles integer fields with vpack_special_values="1,2,3,...".
392398
func (g *codeGenerator) defineSpecialValuesNumeric(codecName, specialValues string) {
393-
// For each special value, we create e.g. "StaticIdxStepVal1Field" and a snippet
394-
// t[StaticIdxStepVal1Field] = append(t[StaticIdxStepField], []byte{0x01}...)
395-
baseField := g.getOrCreateStaticIndexForField(codecName) // e.g. "StaticIdxStepField"
399+
// For each special value, we create e.g. "staticIdxStepVal1Field" and a snippet
400+
// t[staticIdxStepVal1Field] = append(t[staticIdxStepField], []byte{0x01}...)
401+
baseField := g.getOrCreateStaticIndexForField(codecName) // e.g. "staticIdxStepField"
396402

397403
vals := strings.Split(specialValues, ",")
398404
for _, v := range vals {
399405
v = strings.TrimSpace(v)
400-
cn := fmt.Sprintf("StaticIdx%sVal%sField", strings.Title(codecName), v)
406+
cn := fmt.Sprintf("staticIdx%sVal%sField", strings.Title(codecName), v)
401407
if g.findItemIndexByConstName(cn) >= 0 {
402408
continue
403409
}
@@ -423,9 +429,9 @@ func parseByteValue(s string) byte {
423429
return b
424430
}
425431

426-
// getOrCreateMapMarkerIndex ensures we have e.g. "StaticIdxMapMarker3" => 0x83
432+
// getOrCreateMapMarkerIndex ensures we have e.g. "staticIdxMapMarker3" => 0x83
427433
func (g *codeGenerator) getOrCreateMapMarkerIndex(n int) {
428-
cn := fmt.Sprintf("StaticIdxMapMarker%d", n)
434+
cn := fmt.Sprintf("staticIdxMapMarker%d", n)
429435
if g.findItemIndexByConstName(cn) >= 0 {
430436
return
431437
}
@@ -445,7 +451,7 @@ func (g *codeGenerator) getOrCreateMapMarkerIndex(n int) {
445451

446452
// getOrCreateStaticIndexForField creates a fixstr entry for a field name: e.g. 0xa3,"snd"
447453
func (g *codeGenerator) getOrCreateStaticIndexForField(fieldName string) string {
448-
cn := "StaticIdx" + strings.Title(fieldName) + "Field"
454+
cn := "staticIdx" + strings.Title(fieldName) + "Field"
449455
idxNum := g.findItemIndexByConstName(cn)
450456
if idxNum >= 0 {
451457
return cn
@@ -480,7 +486,23 @@ func (g *codeGenerator) renderConstBlock() (string, error) {
480486
sort.Slice(g.items, func(i, j int) bool {
481487
return g.items[i].Index < g.items[j].Index
482488
})
483-
data := struct{ Items []staticItem }{Items: g.items}
489+
490+
// Find the minimum and maximum static index values
491+
var minIdx, maxIdx uint8
492+
if len(g.items) > 0 {
493+
minIdx = g.items[0].Index // First item after sorting
494+
maxIdx = g.items[len(g.items)-1].Index // Last item after sorting
495+
}
496+
497+
data := struct {
498+
Items []staticItem
499+
StaticIdxStart uint8
500+
StaticIdxEnd uint8
501+
}{
502+
Items: g.items,
503+
StaticIdxStart: minIdx,
504+
StaticIdxEnd: maxIdx,
505+
}
484506

485507
tmpl, err := template.New("constBlock").Parse(constBlockTemplate)
486508
if err != nil {
@@ -530,10 +552,10 @@ func (g *codeGenerator) renderParseFunction(typeName string) (string, error) {
530552
"split": func(s, sep string) []string {
531553
return strings.Split(s, sep)
532554
},
533-
// top-level function to build e.g. "StaticIdxStepVal3Field"
555+
// top-level function to build e.g. "staticIdxStepVal3Field"
534556
"makeNumericConst": func(fieldName, val string) string {
535-
// fieldName = "step", val="3" => "StaticIdxStepVal3Field"
536-
return fmt.Sprintf("StaticIdx%sVal%sField", strings.Title(fieldName), val)
557+
// fieldName = "step", val="3" => "staticIdxStepVal3Field"
558+
return fmt.Sprintf("staticIdx%sVal%sField", strings.Title(fieldName), val)
537559
},
538560
"renderParseFunction": func(typeName string) string {
539561
ret, err := g.renderParseFunction(typeName)

0 commit comments

Comments
 (0)