Skip to content

Commit 48d659c

Browse files
committed
Adjust default Type code for collation #15.
1 parent e8f80d4 commit 48d659c

6 files changed

+138
-143
lines changed

cmd/cmd_test.go

+18-17
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ func TestCmdArgs(t *testing.T) {
4242
{
4343
[]string{"-inpfile", "example.json", "-json2collate"},
4444
[]byte("Json: \"hello world\"\n" +
45-
"Coll: \"\\x06hello world\\x00\\x00\"\n" +
46-
"Coll: [6 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
45+
"Coll: \"\\x10hello world\\x00\\x00\"\n" +
46+
"Coll: [16 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
4747
},
4848
{
4949
[]string{"-inptxt", `"hello world"`, "-json2collate"},
5050
[]byte("Json: \"hello world\"\n" +
51-
"Coll: \"\\x06hello world\\x00\\x00\"\n" +
52-
"Coll: [6 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
51+
"Coll: \"\\x10hello world\\x00\\x00\"\n" +
52+
"Coll: [16 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
5353
},
5454
// json options
5555
{
@@ -80,20 +80,20 @@ func TestCmdArgs(t *testing.T) {
8080
{
8181
[]string{"-inpfile", "example.cbor", "-cbor2collate"},
8282
[]byte("Cbor: \"khello world\"\n" +
83-
"Coll: \"\\x06hello world\\x00\\x00\"\n" +
84-
"Coll: [6 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
83+
"Coll: \"\\x10hello world\\x00\\x00\"\n" +
84+
"Coll: [16 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
8585
},
8686
{
8787
[]string{"-inptxt", "khello world", "-cbor2collate"},
8888
[]byte("Cbor: \"khello world\"\n" +
89-
"Coll: \"\\x06hello world\\x00\\x00\"\n" +
90-
"Coll: [6 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
89+
"Coll: \"\\x10hello world\\x00\\x00\"\n" +
90+
"Coll: [16 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
9191
},
9292
{
9393
[]string{"-inptxt", "khello world", "-cbor2collate"},
9494
[]byte("Cbor: \"khello world\"\n" +
95-
"Coll: \"\\x06hello world\\x00\\x00\"\n" +
96-
"Coll: [6 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
95+
"Coll: \"\\x10hello world\\x00\\x00\"\n" +
96+
"Coll: [16 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
9797
},
9898
// cbor options
9999
{
@@ -115,17 +115,18 @@ func TestCmdArgs(t *testing.T) {
115115
// collate transformations
116116
{
117117
[]string{"-inpfile", "example.coll", "-collate2value"},
118-
[]byte("Coll: \"\\x06hello world\\x00\\x00\"\nValu: hello world\n"),
118+
[]byte("Coll: \"\\x10hello world\\x00\\x00\"\nValu: hello world\n"),
119119
},
120120
{
121121
[]string{"-inpfile", "example.coll", "-collate2json"},
122-
[]byte("Coll: \"\\x06hello world\\x00\\x00\"\nJson: \"hello world\"\n"),
122+
[]byte("Coll: \"\\x10hello world\\x00\\x00\"\nJson: \"hello world\"\n"),
123123
},
124124
{
125125
[]string{"-inpfile", "example.coll", "-collate2cbor"},
126-
[]byte("Coll: \"\\x06hello world\\x00\\x00\"\n" +
127-
"Cbor: \"khello world\"\n" +
128-
"Cbor: [107 104 101 108 108 111 32 119 111 114 108 100]\n"),
126+
[]byte(
127+
"Coll: \"\\x10hello world\\x00\\x00\"\n" +
128+
"Cbor: \"khello world\"\n" +
129+
"Cbor: [107 104 101 108 108 111 32 119 111 114 108 100]\n"),
129130
},
130131
// value transformations
131132
{
@@ -140,8 +141,8 @@ func TestCmdArgs(t *testing.T) {
140141
{
141142
[]string{"-inptxt", `"hello world"`, "-value2collate"},
142143
[]byte("Valu: hello world\n" +
143-
"Coll: \"\\x06hello world\\x00\\x00\"\n" +
144-
"Coll: [6 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
144+
"Coll: \"\\x10hello world\\x00\\x00\"\n" +
145+
"Coll: [16 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
145146
},
146147
}
147148
for _, testcase := range testcases {

cmd/example.coll

0 Bytes
Binary file not shown.

collate.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import "bytes"
66
// Collation order for supported types, to change the order set these
77
// values in your init() function.
88
var (
9-
Terminator byte
10-
TypeMissing byte = 1
11-
TypeNull byte = 2
12-
TypeFalse byte = 3
13-
TypeTrue byte = 4
14-
TypeNumber byte = 5
15-
TypeString byte = 6
16-
TypeLength byte = 7
17-
TypeArray byte = 8
18-
TypeObj byte = 9
19-
TypeBinary byte = 10
9+
Terminator byte = 0
10+
TypeMissing byte = 11
11+
TypeNull byte = 12
12+
TypeFalse byte = 13
13+
TypeTrue byte = 14
14+
TypeNumber byte = 15
15+
TypeString byte = 16
16+
TypeLength byte = 17
17+
TypeArray byte = 18
18+
TypeObj byte = 19
19+
TypeBinary byte = 20
2020
)
2121

2222
// Missing denotes a special type for an item that evaluates to _nothing_.

collate_cbor_test.go

+50-58
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import "fmt"
77
var _ = fmt.Sprintf("dummy")
88

99
func TestCbor2CollateNil(t *testing.T) {
10-
inp, ref, config := "null", `\x02\x00`, NewDefaultConfig()
10+
inp, ref, config := "null", `\f\x00`, NewDefaultConfig()
1111
clt := config.NewCollate(make([]byte, 1024), 0)
1212
cbr := config.NewCbor(make([]byte, 1024), 0)
1313
cltback := config.NewCollate(make([]byte, 1024), 0)
@@ -21,7 +21,7 @@ func TestCbor2CollateNil(t *testing.T) {
2121
}
2222

2323
func TestCbor2CollateTrue(t *testing.T) {
24-
inp, ref, config := "true", `\x04\x00`, NewDefaultConfig()
24+
inp, ref, config := "true", `\x0e\x00`, NewDefaultConfig()
2525
clt := config.NewCollate(make([]byte, 1024), 0)
2626
cbr := config.NewCbor(make([]byte, 1024), 0)
2727
cltback := config.NewCollate(make([]byte, 1024), 0)
@@ -35,7 +35,7 @@ func TestCbor2CollateTrue(t *testing.T) {
3535
}
3636

3737
func TestCbor2CollateFalse(t *testing.T) {
38-
inp, ref, config := "false", `\x03\x00`, NewDefaultConfig()
38+
inp, ref, config := "false", `\r\x00`, NewDefaultConfig()
3939
clt := config.NewCollate(make([]byte, 1024), 0)
4040
cbr := config.NewCbor(make([]byte, 1024), 0)
4141
cltback := config.NewCollate(make([]byte, 1024), 0)
@@ -50,34 +50,34 @@ func TestCbor2CollateFalse(t *testing.T) {
5050

5151
func TestCbor2CollateNumber(t *testing.T) {
5252
testcases := [][3]interface{}{
53-
{"10.2", `\x05>>2102-\x00`, FloatNumber},
54-
{"10", `\x05>>21-\x00`, FloatNumber},
55-
{"10.2", `\x05>>2102-\x00`, SmartNumber},
56-
{"10", `\x05>>21-\x00`, SmartNumber},
57-
{"10", `\x05>>21-\x00`, FloatNumber},
58-
{"-10", `\x05--78>\x00`, SmartNumber},
59-
{"-10", `\x05--78>\x00`, FloatNumber},
60-
{"200", `\x05>>32-\x00`, SmartNumber},
61-
{"200", `\x05>>32-\x00`, FloatNumber},
62-
{"-200", `\x05--67>\x00`, SmartNumber},
63-
{"-200", `\x05--67>\x00`, FloatNumber},
53+
{"10.2", `\x0f>>2102-\x00`, FloatNumber},
54+
{"10", `\x0f>>21-\x00`, FloatNumber},
55+
{"10.2", `\x0f>>2102-\x00`, SmartNumber},
56+
{"10", `\x0f>>21-\x00`, SmartNumber},
57+
{"10", `\x0f>>21-\x00`, FloatNumber},
58+
{"-10", `\x0f--78>\x00`, SmartNumber},
59+
{"-10", `\x0f--78>\x00`, FloatNumber},
60+
{"200", `\x0f>>32-\x00`, SmartNumber},
61+
{"200", `\x0f>>32-\x00`, FloatNumber},
62+
{"-200", `\x0f--67>\x00`, SmartNumber},
63+
{"-200", `\x0f--67>\x00`, FloatNumber},
6464
{
65-
"4294967297", `\x05>>>2104294967297-\x00`, FloatNumber},
65+
"4294967297", `\x0f>>>2104294967297-\x00`, FloatNumber},
6666
{
67-
"-4294967297", `\x05---7895705032702>\x00`, FloatNumber},
67+
"-4294967297", `\x0f---7895705032702>\x00`, FloatNumber},
6868
{
69-
"4294967297", `\x05>>>2104294967297-\x00`, SmartNumber},
69+
"4294967297", `\x0f>>>2104294967297-\x00`, SmartNumber},
7070
{
71-
"-4294967297", `\x05---7895705032702>\x00`, SmartNumber},
71+
"-4294967297", `\x0f---7895705032702>\x00`, SmartNumber},
7272
{
73-
"9007199254740992", `\x05>>>2169007199254740992-\x00`, FloatNumber},
73+
"9007199254740992", `\x0f>>>2169007199254740992-\x00`, FloatNumber},
7474
{
75-
"-9007199254740993", `\x05---7830992800745259007>\x00`, FloatNumber},
75+
"-9007199254740993", `\x0f---7830992800745259007>\x00`, FloatNumber},
7676
{
77-
"9007199254740992", `\x05>>>2169007199254740992-\x00`, SmartNumber},
77+
"9007199254740992", `\x0f>>>2169007199254740992-\x00`, SmartNumber},
7878

7979
{
80-
"-9007199254740993", `\x05---7830992800745259006>\x00`, SmartNumber},
80+
"-9007199254740993", `\x0f---7830992800745259006>\x00`, SmartNumber},
8181
}
8282

8383
for _, tcase := range testcases {
@@ -103,9 +103,9 @@ func TestCbor2CollateNumber(t *testing.T) {
103103

104104
func TestCbor2CollateString(t *testing.T) {
105105
testcases := [][2]string{
106-
{`""`, `\x06\x00\x00`},
107-
{`"hello world"`, `\x06hello world\x00\x00`},
108-
{fmt.Sprintf(`"%s"`, MissingLiteral), `\x01\x00`},
106+
{`""`, `\x10\x00\x00`},
107+
{`"hello world"`, `\x10hello world\x00\x00`},
108+
{fmt.Sprintf(`"%s"`, MissingLiteral), `\v\x00`},
109109
}
110110

111111
config := NewDefaultConfig()
@@ -131,7 +131,7 @@ func TestCbor2CollateString(t *testing.T) {
131131

132132
// missing string without doMissing configuration
133133
inp := fmt.Sprintf(`"%s"`, MissingLiteral)
134-
refcode := `\x06~[]{}falsenilNA~\x00\x00`
134+
refcode := `\x10~[]{}falsenilNA~\x00\x00`
135135
config = NewDefaultConfig().UseMissing(false)
136136
clt = config.NewCollate(make([]byte, 1024), 0)
137137
cbr = config.NewCbor(make([]byte, 1024), 0)
@@ -159,7 +159,7 @@ func TestCbor2CollateString(t *testing.T) {
159159
}
160160

161161
func TestCbor2CollateBytes(t *testing.T) {
162-
inp, refcode := []byte("hello world"), `\nhello world\x00`
162+
inp, refcode := []byte("hello world"), `\x14hello world\x00`
163163
config := NewDefaultConfig()
164164
clt := config.NewCollate(make([]byte, 1024), 0)
165165
cbr := config.NewCbor(make([]byte, 1024), 0)
@@ -177,19 +177,20 @@ func TestCbor2CollateArray(t *testing.T) {
177177
// without length prefix
178178
testcases := [][4]string{
179179
{`[]`,
180-
`\b\x00`,
181-
`\b\a0\x00\x00`,
180+
`\x12\x00`,
181+
`\x12\x110\x00\x00`,
182182
`[]`},
183183
{`[null,true,false,10.0,"hello"]`,
184-
`\b\x02\x00\x04\x00\x03\x00\x05>>21-\x00\x06hello\x00\x00\x00`,
185-
`\b\a>5\x00\x02\x00\x04\x00\x03\x00\x05>>21-\x00` +
186-
`\x06hello\x00\x00\x00`,
184+
`\x12\f\x00\x0e\x00\r\x00\x0f>>21-\x00\x10hello\x00\x00\x00`,
185+
`\x12\x11>5\x00\f\x00\x0e\x00\r\x00\x0f>>21-\x00\x10hello\x00` +
186+
`\x00\x00`,
187187
`[null,true,false,+0.1e+2,"hello"]`},
188188
{`[null,true,10.0,10.2,[],{"key":{}}]`,
189-
`\b\x02\x00\x04\x00\x05>>21-\x00\x05>>2102-\x00\b\x00` +
190-
`\t\a>1\x00\x06key\x00\x00\t\a0\x00\x00\x00\x00`,
191-
`\b\a>6\x00\x02\x00\x04\x00\x05>>21-\x00\x05>>2102-\x00` +
192-
`\b\a0\x00\x00\t\a>1\x00\x06key\x00\x00\t\a0\x00\x00\x00\x00`,
189+
`\x12\f\x00\x0e\x00\x0f>>21-\x00\x0f>>2102-\x00\x12\x00` +
190+
`\x13\x11>1\x00\x10key\x00\x00\x13\x110\x00\x00\x00\x00`,
191+
`\x12\x11>6\x00\f\x00\x0e\x00\x0f>>21-\x00\x0f>>2102-\x00` +
192+
`\x12\x110\x00\x00\x13\x11>1\x00\x10key\x00\x00\x13\x110` +
193+
`\x00\x00\x00\x00`,
193194
`[null,true,+0.1e+2,+0.102e+2,[],{"key":{}}]`},
194195
}
195196

@@ -201,15 +202,14 @@ func TestCbor2CollateArray(t *testing.T) {
201202
for _, tcase := range testcases {
202203
inp, refcode := tcase[0], tcase[1]
203204

204-
t.Logf("%v", tcase[0])
205-
206205
config.NewJson(
207206
[]byte(inp), -1).Tocollate(
208207
clt.Reset(nil)).Tocbor(
209208
cbr.Reset(nil)).Tocollate(cltback.Reset(nil))
210209

211210
seqn := fmt.Sprintf("%q", cltback.Bytes())
212211
if seqn = seqn[1 : len(seqn)-1]; seqn != refcode {
212+
t.Logf("%v", tcase[0])
213213
t.Errorf("expected %v, got %v", refcode, seqn)
214214
}
215215
}
@@ -222,16 +222,14 @@ func TestCbor2CollateArray(t *testing.T) {
222222

223223
for _, tcase := range testcases {
224224
inp, refcode := tcase[0], tcase[2]
225-
226-
t.Logf("%v", tcase[0])
227-
228225
config.NewJson(
229226
[]byte(inp), -1).Tocollate(
230227
clt.Reset(nil)).Tocbor(
231228
cbr.Reset(nil)).Tocollate(cltback.Reset(nil))
232229

233230
seqn := fmt.Sprintf("%q", cltback.Bytes())
234231
if seqn = seqn[1 : len(seqn)-1]; seqn != refcode {
232+
t.Logf("%v", tcase[0])
235233
t.Errorf("expected %v, got %v", refcode, seqn)
236234
}
237235
}
@@ -244,16 +242,14 @@ func TestCbor2CollateArray(t *testing.T) {
244242

245243
for _, tcase := range testcases {
246244
inp, refcode := tcase[0], tcase[2]
247-
248-
t.Logf("%v", tcase[0])
249-
250245
config.NewJson(
251246
[]byte(inp), -1).Tocollate(
252247
clt.Reset(nil)).Tocbor(
253248
cbr.Reset(nil)).Tocollate(cltback.Reset(nil))
254249

255250
seqn := fmt.Sprintf("%q", cltback.Bytes())
256251
if seqn = seqn[1 : len(seqn)-1]; seqn != refcode {
252+
t.Logf("%v", tcase[0])
257253
t.Errorf("expected %v, got %v", refcode, seqn)
258254
}
259255
}
@@ -264,17 +260,17 @@ func TestCbor2CollateMap(t *testing.T) {
264260
testcases := [][4]string{
265261
{
266262
`{}`,
267-
`\t\a0\x00\x00`,
268-
`\t\x00`,
263+
`\x13\x110\x00\x00`,
264+
`\x13\x00`,
269265
`{}`},
270266
{
271267
`{"a":null,"b":true,"c":false,"d":10.0,"e":"hello","f":["wo"]}`,
272-
`\t\a>6\x00\x06a\x00\x00\x02\x00\x06b\x00\x00\x04\x00\x06c` +
273-
`\x00\x00\x03\x00\x06d\x00\x00\x05>>21-\x00\x06e\x00\x00` +
274-
`\x06hello\x00\x00\x06f\x00\x00\b\x06wo\x00\x00\x00\x00`,
275-
`\t\x06a\x00\x00\x02\x00\x06b\x00\x00\x04\x00\x06c\x00\x00` +
276-
`\x03\x00\x06d\x00\x00\x05>>21-\x00\x06e\x00\x00` +
277-
`\x06hello\x00\x00\x06f\x00\x00\b\x06wo\x00\x00\x00\x00`,
268+
`\x13\x11>6\x00\x10a\x00\x00\f\x00\x10b\x00\x00\x0e\x00\x10c` +
269+
`\x00\x00\r\x00\x10d\x00\x00\x0f>>21-\x00\x10e\x00\x00\x10hello` +
270+
`\x00\x00\x10f\x00\x00\x12\x10wo\x00\x00\x00\x00`,
271+
`\x13\x10a\x00\x00\f\x00\x10b\x00\x00\x0e\x00\x10c\x00\x00` +
272+
`\r\x00\x10d\x00\x00\x0f>>21-\x00\x10e\x00\x00\x10hello` +
273+
`\x00\x00\x10f\x00\x00\x12\x10wo\x00\x00\x00\x00`,
278274
`{"a":null,"b":true,"c":false,"d":+0.1e+2,"e":"hello","f":["wo"]}`},
279275
}
280276

@@ -285,16 +281,14 @@ func TestCbor2CollateMap(t *testing.T) {
285281

286282
for _, tcase := range testcases {
287283
inp, refcode := tcase[0], tcase[1]
288-
289-
t.Logf("%v", tcase[0])
290-
291284
config.NewJson(
292285
[]byte(inp), -1).Tocollate(
293286
clt.Reset(nil)).Tocbor(
294287
cbr.Reset(nil)).Tocollate(cltback.Reset(nil))
295288

296289
seqn := fmt.Sprintf("%q", cltback.Bytes())
297290
if seqn = seqn[1 : len(seqn)-1]; seqn != refcode {
291+
t.Logf("%v", tcase[0])
298292
t.Errorf("expected %v, got %v", refcode, seqn)
299293
}
300294
}
@@ -308,16 +302,14 @@ func TestCbor2CollateMap(t *testing.T) {
308302

309303
for _, tcase := range testcases {
310304
inp, refcode := tcase[0], tcase[2]
311-
312-
t.Logf("%v", tcase[0])
313-
314305
config.NewJson(
315306
[]byte(inp), -1).Tocollate(
316307
clt.Reset(nil)).Tocbor(
317308
cbr.Reset(nil)).Tocollate(cltback.Reset(nil))
318309

319310
seqn := fmt.Sprintf("%q", cltback.Bytes())
320311
if seqn = seqn[1 : len(seqn)-1]; seqn != refcode {
312+
t.Logf("%v", tcase[0])
321313
t.Errorf("expected %v, got %v", refcode, seqn)
322314
}
323315
}

0 commit comments

Comments
 (0)