Skip to content

Commit 6bc3a00

Browse files
fix spellings for new
bug fix bug fix bug fix, strict int float encodings bug fix, strict int float encodings
1 parent 2ce15d5 commit 6bc3a00

7 files changed

Lines changed: 274 additions & 83 deletions

File tree

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ STuple(
429429
```go
430430
func TestDefaultHugoConfigRoundTripMultiCheck(t *testing.T) {
431431

432-
SchemeJsonStr := `
432+
SchemaJsonStr := `
433433
{
434434
"type": "tuple",
435435
"variableLength": true,
@@ -523,22 +523,22 @@ func TestDefaultHugoConfigRoundTripMultiCheck(t *testing.T) {
523523
}
524524
}`
525525

526-
var SchemeJson SchemeJSON
527-
require.NoError(t, json.Unmarshal([]byte(SchemeJsonStr), &SchemeJson), "failed to unmarshal config")
526+
var SchemaJson SchemaJSON
527+
require.NoError(t, json.Unmarshal([]byte(SchemaJsonStr), &SchemaJson), "failed to unmarshal config")
528528

529-
schain := scheme.SChain(scheme.BuildScheme(SchemeJson))
529+
schain := schema.SChain(schema.BuildSchema(&SchemaJson))
530530

531531
// Decode into generic map
532532
var decoded map[string]any
533533
require.NoError(t, json.Unmarshal([]byte(configJSON), &decoded), "failed to unmarshal config")
534534

535-
// Encode using your scheme
536-
encoded, err := scheme.EncodeValue(decoded, schain)
537-
require.NoError(t, err, "scheme encode failed")
535+
// Encode using your schema
536+
encoded, err := schema.EncodeValue(decoded, schain)
537+
require.NoError(t, err, "schema encode failed")
538538

539-
// Decode back using your scheme
540-
roundTrip, err := scheme.DecodeBuffer(encoded, schain)
541-
require.NoError(t, err, "scheme decode failed")
539+
// Decode back using your schema
540+
roundTrip, err := schema.DecodeBuffer(encoded, schain)
541+
require.NoError(t, err, "schema decode failed")
542542

543543
// Marshal both original and round-trip to canonical JSON
544544
origJSON, err := json.Marshal(decoded)

packable/pack.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ func NewTuple(args ...access.Packable) Tuple {
1515

1616
// ValueSize returns the size of the packed
1717
func (p Tuple) ValueSize() int {
18+
if len(*p.args) == 0 {
19+
// return 0
20+
return 0
21+
}
1822
value_size := 0
1923
for _, arg := range *p.args {
2024
value_size += arg.ValueSize()

packable/packable_mapPackables.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ type PackMapSorted map[string]access.Packable
1111

1212
// ValueSize returns the size of the packed map's content.
1313
func (p PackMapSorted) ValueSize() int {
14+
if len(p) == 0 {
15+
return 0
16+
}
1417
size := 0
1518
for k, v := range p {
1619
// Add the size of the key and the size of the packed value.
@@ -26,6 +29,9 @@ func (p PackMapSorted) HeaderType() typetags.Type {
2629

2730
// Add packs the map into a byte buffer by sorting keys first for a deterministic result.
2831
func (p PackMapSorted) Write(buf []byte, pos int) int {
32+
if len(p) == 0 {
33+
return pos
34+
}
2935
keys := utils.SortKeys(p)
3036
headerSize := len(p)*2*access.HeaderTagSize + access.HeaderTagSize
3137
first := pos
@@ -51,6 +57,9 @@ type PackMap map[string]access.Packable
5157

5258
// ValueSize returns the size of the packed map's content.
5359
func (p PackMap) ValueSize() int {
60+
if len(p) == 0 {
61+
return 0
62+
}
5463
size := 0
5564
for k, v := range p {
5665
// Add the size of the key and the size of the packed value.
@@ -66,6 +75,9 @@ func (p PackMap) HeaderType() typetags.Type {
6675

6776
// Add packs the map into a byte buffer. This version does not sort keys.
6877
func (p PackMap) Write(buf []byte, pos int) int {
78+
if len(p) == 0 {
79+
return pos
80+
}
6981
headerSize := len(p)*2*access.HeaderTagSize + access.HeaderTagSize
7082
first := pos
7183
posH := pos
@@ -89,6 +101,9 @@ type PackMapStr map[string]string
89101

90102
// ValueSize returns the size of the packed map's content.
91103
func (p PackMapStr) ValueSize() int {
104+
if len(p) == 0 {
105+
return 0
106+
}
92107
size := 0
93108
for k, v := range p {
94109
// Add the size of the key and the size of the packed value.
@@ -104,6 +119,9 @@ func (p PackMapStr) HeaderType() typetags.Type {
104119

105120
// Add packs the map into a byte buffer. This version does not sort keys.
106121
func (p PackMapStr) Write(buf []byte, pos int) int {
122+
if len(p) == 0 {
123+
return pos
124+
}
107125
headerSize := len(p)*2*access.HeaderTagSize + access.HeaderTagSize
108126
first := pos
109127
posH := pos
@@ -127,6 +145,9 @@ type PackMapStrInt32 map[string]int32
127145

128146
// ValueSize returns the size of the packed map's content.
129147
func (p PackMapStrInt32) ValueSize() int {
148+
if len(p) == 0 {
149+
return 0
150+
}
130151
size := 0
131152
for k := range p {
132153
size += len(k)
@@ -142,6 +163,9 @@ func (p PackMapStrInt32) HeaderType() typetags.Type {
142163

143164
// Write packs the map into a byte buffer. This version does not sort keys.
144165
func (p PackMapStrInt32) Write(buf []byte, pos int) int {
166+
if len(p) == 0 {
167+
return pos
168+
}
145169
headerSize := len(p)*2*access.HeaderTagSize + access.HeaderTagSize
146170
first := pos
147171
posH := pos
@@ -166,6 +190,9 @@ type PackMapStrInt64 map[string]int64
166190

167191
// ValueSize returns the size of the packed map's content.
168192
func (p PackMapStrInt64) ValueSize() int {
193+
if len(p) == 0 {
194+
return 0
195+
}
169196
size := 0
170197
for k := range p {
171198
size += len(k)
@@ -181,6 +208,9 @@ func (p PackMapStrInt64) HeaderType() typetags.Type {
181208

182209
// Write packs the map into a byte buffer. This version does not sort keys.
183210
func (p PackMapStrInt64) Write(buf []byte, pos int) int {
211+
if len(p) == 0 {
212+
return pos
213+
}
184214
headerSize := len(p)*2*access.HeaderTagSize + access.HeaderTagSize
185215
first := pos
186216
posH := pos
@@ -231,6 +261,9 @@ func (p *PackableMapOrdered) Set(key string, val access.Packable) {
231261

232262
// ValueSize returns the size of the packed map's content.
233263
func (p *PackableMapOrdered) ValueSize() int {
264+
if len(p.om.Keys()) == 0 {
265+
return 0
266+
}
234267
size := 0
235268
for k, v := range p.om.ItemsIter() {
236269
size += len(k) + v.ValueSize()
@@ -245,6 +278,9 @@ func (p *PackableMapOrdered) HeaderType() typetags.Type {
245278

246279
// Write packs the map into a byte buffer in insertion order.
247280
func (p *PackableMapOrdered) Write(buf []byte, pos int) int {
281+
if len(p.om.Keys()) == 0 {
282+
return pos
283+
}
248284
headerSize := len(p.om.Keys())*2*access.HeaderTagSize + access.HeaderTagSize
249285
first := pos
250286
posH := pos
@@ -292,6 +328,24 @@ func (pack PackMapStr) PackInto(p *access.PutAccess) {
292328
bPool.Release(buffer)
293329
}
294330

331+
func (pack PackMapStrInt32) PackInto(p *access.PutAccess) {
332+
size := pack.ValueSize()
333+
buffer := bPool.Acquire(size)
334+
pos := 0
335+
pos = pack.Write(buffer, pos)
336+
p.AppendTagAndValue(typetags.TypeMap, buffer[:pos])
337+
bPool.Release(buffer)
338+
}
339+
340+
func (pack PackMapStrInt64) PackInto(p *access.PutAccess) {
341+
size := pack.ValueSize()
342+
buffer := bPool.Acquire(size)
343+
pos := 0
344+
pos = pack.Write(buffer, pos)
345+
p.AppendTagAndValue(typetags.TypeMap, buffer[:pos])
346+
bPool.Release(buffer)
347+
}
348+
295349
// PackInto packs the ordered map into the PutAccess buffer.
296350
func (pack *PackableMapOrdered) PackInto(p *access.PutAccess) {
297351
size := pack.ValueSize()

0 commit comments

Comments
 (0)