Compare commits

..

3 Commits

3 changed files with 45 additions and 5 deletions

View File

@ -272,8 +272,8 @@ func (this *Generator) generateEncodeValue(typ Type, valueSource, tagSource stri
n += nn; if err != nil { return n, err }
this.push()
nn, err = this.iprintf(
"nn, err = encoder.WriteUintN(uint64(%s.CN()), len(%s))\n",
tagSource, valueSource)
"nn, err = encoder.WriteUintN(uint64(len(%s)), %s.CN())\n",
valueSource, tagSource)
n += nn; if err != nil { return n, err }
nn, err = this.generateErrorCheck()
n += nn; if err != nil { return n, err }
@ -288,8 +288,8 @@ func (this *Generator) generateEncodeValue(typ Type, valueSource, tagSource stri
case TypeArray:
// OTA: <length: UN> <elementTag: tape.Tag> <values>*
nn, err := this.iprintf(
"nn, err = encoder.WriteUintN(uint64(%s.CN()), len(%s))\n",
tagSource, valueSource)
"nn, err = encoder.WriteUintN(uint64(len(%s)), %s.CN())\n",
valueSource, tagSource)
n += nn; if err != nil { return n, err }
nn, err = this.generateErrorCheck()
n += nn; if err != nil { return n, err }
@ -325,6 +325,10 @@ func (this *Generator) generateEncodeValue(typ Type, valueSource, tagSource stri
n += nn; if err != nil { return n, err }
nn, err = this.iprintf("if itemTag.Is(tape.SBA) { itemTag += 1 << 5 }\n")
n += nn; if err != nil { return n, err }
nn, err = this.iprintf("nn, err = encoder.WriteTag(itemTag)\n")
n += nn; if err != nil { return n, err }
n += nn; if err != nil { return n, err }
nn, err = this.generateErrorCheck()
nn, err = this.iprintf("for _, item := range %s {\n", valueSource)
n += nn; if err != nil { return n, err }
this.push()

View File

@ -235,5 +235,41 @@ func TestGenerateRun(test *testing.T) {
[]byte { 0x00, 0x00, 0x66, 'r', 'a', 'r', 'i', 't', 'y' },
[]byte { 0x00, 0x01, 0x64, 'g', 'e', 'm', 's' },
))
messageUserList := MessageUserList {
Users: []User {
User {
Name: "rarity",
Bio: "asdjads",
Followers: 0x324,
},
User {
Name: "deez nuts",
Bio: "logy",
Followers: 0x8000,
},
User {
Name: "creekflow",
Bio: "im creekflow",
Followers: 0x3894,
},
},
}
testEncode(
&messageUserList,
tu.S(0xC1, 0x01, 0x00, 0x00,
0xA1, 0x03, 0xC1,
0x03,
0x00, 0x00, 0x66, 'r', 'a', 'r', 'i', 't', 'y',
0x00, 0x01, 0x67, 'a', 's', 'd', 'j', 'a', 'd', 's',
0x00, 0x02, 0x23, 0x00, 0x00, 0x03, 0x24,
0x03,
0x00, 0x00, 0x69, 'd', 'e', 'e', 'z', ' ', 'n', 'u', 't', 's',
0x00, 0x01, 0x64, 'l', 'o', 'g', 'y',
0x00, 0x02, 0x23, 0x00, 0x00, 0x80, 0x00,
0x03,
0x00, 0x00, 0x69, 'c', 'r', 'e', 'e', 'k', 'f', 'l', 'o', 'w',
0x00, 0x01, 0x6C, 'i', 'm', ' ', 'c', 'r', 'e', 'e', 'k', 'f', 'l', 'o', 'w',
0x00, 0x02, 0x23, 0x00, 0x00, 0x38, 0x94,
))
`)
}

View File

@ -47,7 +47,7 @@ func testGenerateRun(test *testing.T, protocol *Protocol, imports string, testCa
if err != nil { log.Fatalf("at %d: %v\n", n, err) }
encoder.Flush()
got := buffer.Bytes()
log.Println("got: ", tu.HexBytes(got))
log.Printf("got: [%s]", tu.HexBytes(got))
log.Println("correct:", correct)
if n != len(got) {
log.Fatalf("n incorrect: %d != %d\n", n, len(got))