Skip to content

Commit

Permalink
atg. test of ZebraToMsgp2 in place
Browse files Browse the repository at this point in the history
  • Loading branch information
glycerine committed Jan 1, 2017
1 parent cd9f458 commit a9621b7
Show file tree
Hide file tree
Showing 9 changed files with 937 additions and 891 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func handleSchemaToGo(c *cfg.ZebraConfig) {
c.SchemaToGo, err)
os.Exit(1)
}
err = sch.WriteToGo(os.Stdout, c.SchemaToGo)
err = sch.WriteToGo(os.Stdout, c.SchemaToGo, "main")
if err != nil {
fmt.Fprintf(os.Stderr, "error: -schema-to-go '%s' produced error on UnmarshalMsg: %v\n",
c.SchemaToGo, err)
Expand Down
2 changes: 1 addition & 1 deletion msgp/write_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,5 +413,5 @@ func AppendIntf(b []byte, i interface{}) ([]byte, error) {
// AppendNegativeOneAndString is a helper for runtime struct id
func AppendNegativeOneAndStringAsBytes(b []byte, str []byte) []byte {
o := AppendInt64(b, -1)
return AppendBytes(o, str)
return AppendStringFromBytes(o, str)
}
6 changes: 4 additions & 2 deletions testdata/my.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main
package testdata

import (
"github.com/glycerine/zebrapack/msgp"
"time"
)

//go:generate zebrapack

const zebraSchemaId64 = 0x6eb25cc0f9a3e

func main() {}
//func main() {}

type S struct{}

Expand Down
Binary file modified testdata/my.z
Binary file not shown.
194 changes: 97 additions & 97 deletions testdata/my.z.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,104 @@
{
"SourcePath": "testdata/my.go",
"SourcePackage": "main",
"SourcePackage": "testdata",
"ZebraSchemaId": 1947397430155838,
"Structs": {
"A": {
"StructName": "A",
"Fields": [
{
"Zid": 0,
"FieldGoName": "Name",
"FieldTagName": "name",
"FieldTypeStr": "string",
"FieldCategory": 23,
"FieldPrimitive": 2,
"FieldFullType": {
"Kind": 2,
"Str": "string"
}
},
{
"Zid": 1,
"FieldGoName": "Bday",
"FieldTagName": "Bday",
"FieldTypeStr": "time.Time",
"FieldCategory": 23,
"FieldPrimitive": 20,
"FieldFullType": {
"Kind": 20,
"Str": "Time"
}
},
{
"Zid": 2,
"FieldGoName": "Phone",
"FieldTagName": "phone",
"FieldTypeStr": "string",
"FieldCategory": 23,
"FieldPrimitive": 2,
"FieldFullType": {
"Kind": 2,
"Str": "string"
},
"OmitEmpty": true
},
{
"Zid": 3,
"FieldGoName": "Sibs",
"FieldTagName": "Sibs",
"FieldTypeStr": "int",
"FieldCategory": 23,
"FieldPrimitive": 13,
"FieldFullType": {
"Kind": 13,
"Str": "int"
},
"OmitEmpty": true
},
{
"Zid": 4,
"FieldGoName": "GPA",
"FieldTagName": "GPA",
"FieldTypeStr": "float64",
"FieldCategory": 23,
"FieldPrimitive": 4,
"FieldFullType": {
"Kind": 4,
"Str": "float64"
}
},
{
"Zid": 5,
"FieldGoName": "Friend",
"FieldTagName": "Friend",
"FieldTypeStr": "bool",
"FieldCategory": 23,
"FieldPrimitive": 18,
"FieldFullType": {
"Kind": 18,
"Str": "bool"
}
}
]
},
"Sys": {
"StructName": "Sys",
"Fields": [
{
"Zid": 0,
"FieldGoName": "F",
"FieldTagName": "F",
"FieldTypeStr": "interface{}",
"FieldCategory": 23,
"FieldPrimitive": 19,
"FieldFullType": {
"Kind": 19,
"Str": "Intf"
}
}
]
},
"S2": {
"StructName": "S2",
"Fields": [
Expand Down Expand Up @@ -192,102 +288,6 @@
}
}
]
},
"A": {
"StructName": "A",
"Fields": [
{
"Zid": 0,
"FieldGoName": "Name",
"FieldTagName": "name",
"FieldTypeStr": "string",
"FieldCategory": 23,
"FieldPrimitive": 2,
"FieldFullType": {
"Kind": 2,
"Str": "string"
}
},
{
"Zid": 1,
"FieldGoName": "Bday",
"FieldTagName": "Bday",
"FieldTypeStr": "time.Time",
"FieldCategory": 23,
"FieldPrimitive": 20,
"FieldFullType": {
"Kind": 20,
"Str": "Time"
}
},
{
"Zid": 2,
"FieldGoName": "Phone",
"FieldTagName": "phone",
"FieldTypeStr": "string",
"FieldCategory": 23,
"FieldPrimitive": 2,
"FieldFullType": {
"Kind": 2,
"Str": "string"
},
"OmitEmpty": true
},
{
"Zid": 3,
"FieldGoName": "Sibs",
"FieldTagName": "Sibs",
"FieldTypeStr": "int",
"FieldCategory": 23,
"FieldPrimitive": 13,
"FieldFullType": {
"Kind": 13,
"Str": "int"
},
"OmitEmpty": true
},
{
"Zid": 4,
"FieldGoName": "GPA",
"FieldTagName": "GPA",
"FieldTypeStr": "float64",
"FieldCategory": 23,
"FieldPrimitive": 4,
"FieldFullType": {
"Kind": 4,
"Str": "float64"
}
},
{
"Zid": 5,
"FieldGoName": "Friend",
"FieldTagName": "Friend",
"FieldTypeStr": "bool",
"FieldCategory": 23,
"FieldPrimitive": 18,
"FieldFullType": {
"Kind": 18,
"Str": "bool"
}
}
]
},
"Sys": {
"StructName": "Sys",
"Fields": [
{
"Zid": 0,
"FieldGoName": "F",
"FieldTagName": "F",
"FieldTypeStr": "interface{}",
"FieldCategory": 23,
"FieldPrimitive": 19,
"FieldFullType": {
"Kind": 19,
"Str": "Intf"
}
}
]
}
},
"Imports": [
Expand Down
6 changes: 1 addition & 5 deletions testdata/struct_reuse_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
package main

// NOTE: THIS FILE WAS PRODUCED BY THE
// ZEBRAPACK CODE GENERATION TOOL (github.com/glycerine/zebrapack)
// DO NOT EDIT
package testdata

import (
//"fmt"
Expand Down
44 changes: 44 additions & 0 deletions testdata/tomsgp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package testdata

import (
"bytes"
"testing"

"github.com/glycerine/zebrapack/msgp"
"github.com/glycerine/zebrapack/zebra"

cv "github.com/glycerine/goconvey/convey"
)

func Test060ConvertZebraPackToMsgpack2(t *testing.T) {

cv.Convey("ZebraPack + schema can be converted to msgpack2 via zSchema.ZebraToMsgp2()\n", t, func() {
// read the schema
msgp2schema := ZebraSchemaInMsgpack2Format()
var zSchema zebra.Schema
_, err := zSchema.UnmarshalMsg(msgp2schema)
panicOn(err)

// generate data
var e S2
e.B = "hello"
e.P = 43
data, err := e.MarshalMsg(nil)
panicOn(err)

// turn it into msgpack2 and display as json

m2, _, err := zSchema.ZebraToMsgp2(data)
panicOn(err)
var json bytes.Buffer
_, err = msgp.CopyToJSON(&json, bytes.NewBuffer(m2))
panicOn(err)
cv.So(string(json.Bytes()), cv.ShouldEqual, `{"beta":"hello","P":43,"arr":[0,0,0,0,0,0]}`)
})
}

func panicOn(err error) {
if err != nil {
panic(err)
}
}
10 changes: 7 additions & 3 deletions zebra/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,12 @@ func (i Zkind) String() string {
}

// WriteToGo writes the zebrapack schema to w as a Go source file.
func (s *Schema) WriteToGo(w io.Writer, path string) (err error) {
fmt.Fprintf(w, "\npackage %s\n\n", s.SourcePackage)

func (s *Schema) WriteToGo(w io.Writer, path string, pkg string) (err error) {
if pkg == "" {
fmt.Fprintf(w, "\npackage %s\n\n", s.SourcePackage)
} else {
fmt.Fprintf(w, "\npackage %s\n\n", pkg)
}
fmt.Fprintf(w, "// File re-generated by: 'zebrapack -write-to-go %s'.\n", path)
fmt.Fprintf(w, "// The '%s' schema was originally created from: '%s'.\n\n", path, s.SourcePath)

Expand Down Expand Up @@ -220,6 +223,7 @@ findMinusOneLoop:
fnum, bts, err = nbs.ReadIntBytes(bts)
if fnum == -1 {
name, bts, err = nbs.ReadStringBytes(bts)
//fmt.Printf("\n found name = '%#v'\n", name)
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit a9621b7

Please sign in to comment.